Manually create MySQL connection

Documentation is good if using the rest of Vapor, but we should also point folks in the right direction if they are just using this for simpler DB automation.
This commit is contained in:
Joe Smith 2018-03-25 12:11:34 -07:00
parent 3f0a6455e5
commit fb3dd9e89f
1 changed files with 11 additions and 1 deletions

View File

@ -94,7 +94,7 @@ Visiting this route should display your MySQL version.
A `MySQLConnection` is normally created using the `Request` container and can perform two different types of queries.
### Create
### Create (with Request)
There are two methods for creating a `MySQLConnection`.
@ -109,6 +109,16 @@ return req.withConnection(to: .mysql) { conn in
As the names imply, `withPooledConnection(to:)` utilizes a connection pool. `withConnection(to:)` does not. Connection pooling is a great way to ensure your application does not exceed the limits of your database, even under peak load.
### Create (manually)
If you are writing a simple tool and would like to use the `MySQL` wrapper, it is also quite simple. You need to create a SwiftNIO [`EventGroup`](https://github.com/apple/swift-nio/blob/master/README.md#eventloops-and-eventloopgroups) that can power the connection:
```
let database = MySQLDatabase(config: config)
let worker = MultiThreadedEventLoopGroup(numThreads: System.coreCount)
let futureConnection = database.makeConnection(on: worker)
```
### Simply Query
Use `.simpleQuery(_:)` to perform a query on your MySQL database that does not bind any parameters. Some queries you send to MySQL may actually require that you use the `simpleQuery(_:)` method instead of the parameterized method.