Skip to content

Commit ab8643f

Browse files
committed
readme update + contributing.md
1 parent aa5c2df commit ab8643f

File tree

2 files changed

+55
-44
lines changed

2 files changed

+55
-44
lines changed

.github/contributing.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Contributing to PostgresNIO
2+
3+
👋 Welcome to the Vapor team!
4+
5+
## Testing
6+
7+
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).
28+
29+
— Thanks! 🙌

README.md

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ let conn = try PostgresConnection.connect(
9696
to: .makeAddressResolvingHost("my.psql.server", port: 5432),
9797
on: eventLoop
9898
).wait()
99+
defer { try! conn.close().wait() }
99100
```
100101

101102
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`.
111112
- `tlsConfiguration` An optional `TLSConfiguration` struct. If supplied, the PostgreSQL connection will be upgraded to use SSL.
112113
- `serverHostname` An optional `String` to use in conjunction with `tlsConfiguration` to specify the server's hostname.
113114

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.
115116

116-
### Client Protocol
117+
### Authentication
117118

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.
119120

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.
121136

122137
```swift
123138
import PostgresNIO
124139

125-
let client: PostgresClient = ...
140+
let db: PostgresDatabase = ...
126141
// now we can use client to do queries
127142
```
128143

@@ -135,11 +150,11 @@ These queries are most useful for schema or transactional queries, or simple sel
135150
`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.
136151

137152
```swift
138-
let rows = try client.simpleQuery("SELECT version()").wait()
139-
print(rows) // [["version": "11.0.0"]]
153+
let rows = try db.simpleQuery("SELECT version()").wait()
154+
print(rows) // [["version": "12.x.x"]]
140155

141-
try client.simpleQuery("SELECT version()") { row in
142-
print(row) // ["version": "11.0.0"]
156+
try db.simpleQuery("SELECT version()") { row in
157+
print(row) // ["version": "12.x.x"]
143158
}.wait()
144159
```
145160

@@ -152,10 +167,10 @@ These queries are most useful for selecting, inserting, and updating data. Data
152167
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.
153168

154169
```swift
155-
let rows = try client.query("SELECT * FROM planets WHERE name = $1", ["Earth"]).wait()
170+
let rows = try db.query("SELECT * FROM planets WHERE name = $1", ["Earth"]).wait()
156171
print(rows) // [["id": 42, "name": "Earth"]]
157172

158-
try client.query("SELECT * FROM planets WHERE name = $1", ["Earth"]) { row in
173+
try db.query("SELECT * FROM planets WHERE name = $1", ["Earth"]) { row in
159174
print(row) // ["id": 42, "name": "Earth"]
160175
}.wait()
161176
```
@@ -209,36 +224,3 @@ print(data.numeric) // PostgresNumeric?
209224
```
210225

211226
`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:
240-
241-
```
242-
$ docker-compose down --volumes
243-
```
244-

0 commit comments

Comments
 (0)