You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To run this package's tests, you need to start a local Postgres database. The easiest way to do this is using Docker.
8
+
9
+
If you have Docker installed and running, you can use the `docker-compose` included with this package. The following command will download the required files and boot a local Postgres server:
10
+
11
+
```fish
12
+
docker-compose up psql-12
13
+
```
14
+
15
+
Run this in the project's root folder (where the `docker-compose.yml` file is). Check out that file to see the other versions of Postgres you can test against.
16
+
17
+
Once you have a server running, you can run the test suite from Xcode by hitting `CMD+u` or from the command line:
18
+
19
+
```fish
20
+
swift test
21
+
```
22
+
23
+
Make sure to add tests for any new code you write.
24
+
25
+
----------
26
+
27
+
Join us on Discord if you have any questions: [http://vapor.team](http://vapor.team).
Copy file name to clipboardExpand all lines: README.md
+26-44Lines changed: 26 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -96,6 +96,7 @@ let conn = try PostgresConnection.connect(
96
96
to: .makeAddressResolvingHost("my.psql.server", port: 5432),
97
97
on: eventLoop
98
98
).wait()
99
+
defer { try! conn.close().wait() }
99
100
```
100
101
101
102
Note: These examples will make use of `wait()` for simplicity. This is appropriate if you are using PostgresNIO on the main thread, like for a CLI tool or in tests. However, you should never use `wait()` on an event loop.
@@ -111,18 +112,32 @@ There are also some additional arguments you can supply to `connect`.
111
112
-`tlsConfiguration` An optional `TLSConfiguration` struct. If supplied, the PostgreSQL connection will be upgraded to use SSL.
112
113
-`serverHostname` An optional `String` to use in conjunction with `tlsConfiguration` to specify the server's hostname.
113
114
114
-
`connect` will return a future `PostgresConnection`, or an error if it could not connect.
115
+
`connect` will return a future `PostgresConnection`, or an error if it could not connect. Make sure you close the connection before it deinitializes.
115
116
116
-
### Client Protocol
117
+
### Authentication
117
118
118
-
Interaction with a server revolves around the `PostgresClient` protocol. This protocol includes methods like `query(_:)` for executing SQL queries and reading the resulting rows.
119
+
Once you have a connection, you will need to authenticate with the server using the `authenticate` method.
119
120
120
-
`PostgresConnection` is the default implementation of `PostgresClient` provided by this package. Assume the client here is the connection from the previous example.
121
+
```swift
122
+
try conn.authenticate(
123
+
username: "vapor_username",
124
+
database: "vapor_database",
125
+
password: "vapor_password"
126
+
).wait()
127
+
```
128
+
129
+
This requires a username. You may supply a database name and password if needed.
130
+
131
+
### Database Protocol
132
+
133
+
Interaction with a server revolves around the `PostgresDatabase` protocol. This protocol includes methods like `query(_:)` for executing SQL queries and reading the resulting rows.
134
+
135
+
`PostgresConnection` is the default implementation of `PostgresDatabase` provided by this package. Assume `db` here is the connection from the previous example.
121
136
122
137
```swift
123
138
importPostgresNIO
124
139
125
-
letclient: PostgresClient=...
140
+
letdb: PostgresDatabase=...
126
141
// now we can use client to do queries
127
142
```
128
143
@@ -135,11 +150,11 @@ These queries are most useful for schema or transactional queries, or simple sel
135
150
`simpleQuery` has two overloads, one that returns an array of rows, and one that accepts a closure for handling each row as it is returned.
136
151
137
152
```swift
138
-
let rows =tryclient.simpleQuery("SELECT version()").wait()
139
-
print(rows) // [["version": "11.0.0"]]
153
+
let rows =trydb.simpleQuery("SELECT version()").wait()
154
+
print(rows) // [["version": "12.x.x"]]
140
155
141
-
tryclient.simpleQuery("SELECT version()") { row in
142
-
print(row) // ["version": "11.0.0"]
156
+
trydb.simpleQuery("SELECT version()") { row in
157
+
print(row) // ["version": "12.x.x"]
143
158
}.wait()
144
159
```
145
160
@@ -152,10 +167,10 @@ These queries are most useful for selecting, inserting, and updating data. Data
152
167
Just like `simpleQuery`, `query` also offers two overloads. One that returns an array of rows, and one that accepts a closure for handling each row as it is returned.
153
168
154
169
```swift
155
-
let rows =tryclient.query("SELECT * FROM planets WHERE name = $1", ["Earth"]).wait()
170
+
let rows =trydb.query("SELECT * FROM planets WHERE name = $1", ["Earth"]).wait()
156
171
print(rows) // [["id": 42, "name": "Earth"]]
157
172
158
-
tryclient.query("SELECT * FROM planets WHERE name = $1", ["Earth"]) { row in
173
+
trydb.query("SELECT * FROM planets WHERE name = $1", ["Earth"]) { row in
`PostgresData` is also used for sending data _to_ the server via parameterized values. To create `PostgresData` from a Swift type, use the available intializer methods.
212
-
213
-
## Library development
214
-
215
-
If you want to contribute to the library development, here is how to get started.
216
-
217
-
### Testing
218
-
219
-
To run the test, you need to start a local PostgreSQL database using Docker.
220
-
221
-
If you have Docker installed and running, you can use Docker Compose to start PostgreSQL:
222
-
223
-
The following command will download the required containers to run the test and start them:
224
-
225
-
```
226
-
$ docker-compose up -d psql-11
227
-
```
228
-
229
-
You can choose to run one of the following PostgreSQL version: `psql-11`, `psql-10`, `psql-9`, `psql-ssl`.
230
-
231
-
From another console or from Xcode, you can then run the test:
232
-
233
-
```
234
-
$ swift test
235
-
```
236
-
237
-
You can check that the test are passing, before adding your own to the test suite.
238
-
239
-
Finally, you can shut down and clean up Docker test environment with:
0 commit comments