Skip to content

Commit 0a80cbc

Browse files
committed
Improve docs
1 parent 712088c commit 0a80cbc

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

README.md

+30-17
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ const something = await sql`
3434

3535
```
3636

37-
## Connection options
37+
## Connection options `postgres([url], [options])`
3838

39-
You can use either a postgres:// url connection string or the options to define your database connection properties.
39+
You can use either a `postgres://` url connection string or the options to define your database connection properties.
4040

4141
```js
4242

43-
const sql = postgres('postgres://user:pass@host:port/database', {
44-
host : '', // or hostname
43+
const sql = postgres('postgres://username:password@host:port/database', {
44+
host : '', // Postgres ip address or domain name
4545
port : 5432, // Postgres server port
4646
path : '', // unix socket path (usually '/tmp')
47-
database : '', // or db
48-
username : '', // or user
49-
password : '', // or pass
47+
database : '', // Name of database to connect to
48+
username : '', // Username of database user
49+
password : '', // Password of database user
5050
ssl : false, // True, or options for tls.connect
5151
max : 10, // Max number of connections
5252
timeout : 0, // Idle connection timeout in seconds
@@ -67,7 +67,9 @@ const sql = postgres('postgres://user:pass@host:port/database', {
6767

6868
```
6969

70-
## Query ```sql`...` -> Promise```
70+
More info for `ssl` can be found in the [Node.js docs for tls connect options](https://nodejs.org/dist/latest-v10.x/docs/api/tls.html#tls_new_tls_tlssocket_socket_options)
71+
72+
## Query ```sql` ` -> Promise```
7173

7274
A query will always return a `Promise` which resolves to either an array `[...]` or `null` depending on the type of query. Destructuring is great to immidiately access the first element.
7375

@@ -107,7 +109,7 @@ const users = await sql`
107109

108110
```
109111

110-
## Stream ```sql`...`.stream(fn) -> Promise```
112+
## Stream ```sql` `.stream(fn) -> Promise```
111113

112114
If you want to handle rows returned by a query one by one you can use `.stream` which returns a promise that resolves once there are no more rows.
113115
```js
@@ -142,7 +144,7 @@ sql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))
142144

143145
```
144146

145-
## Dynamic query helpers `sql()`
147+
## Dynamic query helpers `sql() inside tagged template`
146148

147149
Postgres.js has a safe, ergonomic way to aid you in writing queries. This makes it easier to write dynamic inserts, selects, updates and where queries.
148150

@@ -158,7 +160,7 @@ const user = {
158160

159161
sql`
160162
insert into users ${
161-
sql(data)
163+
sql(user)
162164
}
163165
`
164166

@@ -215,7 +217,7 @@ const types = sql`
215217

216218
```
217219

218-
#### JSON `sql.json()`
220+
#### JSON `sql.json(object)`
219221

220222
```js
221223

@@ -233,9 +235,18 @@ const [{ json }] = await sql`
233235
// json = { hello: 'postgres' }
234236
```
235237

236-
## File query
238+
## File query `sql.file(path, [args], [options]) -> Promise`
239+
240+
Using an `sql` file for a query. The contents will be cached in memory so that the file is only read once.
241+
242+
```js
237243

238-
Using a file to query
244+
sql.file(path.join(__dirname, 'query.sql'), [], {
245+
simple: true, // Default true — allows multiple statements, but no arguments
246+
cache: true // Default true - disable for single queries or memory reasons
247+
})
248+
249+
```
239250

240251
## Transactions
241252

@@ -361,13 +372,15 @@ prexit(async () => {
361372
```
362373

363374

364-
## Unsafe
375+
## sql.unsafe(query, [args], [options]) -> promise
365376

366377
If you know what you're doing, you can use `unsafe` to pass any string you'd like to postgres.
367378

368379
```js
369380

370-
sql
381+
sql.unsafe(danger + `
382+
select * from users where id = $1
383+
`, [user_id])
371384

372385
```
373386

@@ -433,4 +446,4 @@ nb. You can use [`onnotice`](#onnotice) to listen to any Postgres `NOTICE` sent
433446

434447
A really big thank you to @JAForbes who introduced me to Postgres and still holds my hand navigating all the great opportunities we have.
435448

436-
Thanks to @ACXGit for initial tests.
449+
Thanks to @ACXgit for initial tests.

0 commit comments

Comments
 (0)