Skip to content

Commit 55186d1

Browse files
authored
Documentation fixes & additions (porsager#699)
* Update README.md Add missing awaits and describe dynamic password support. * Add ESM dynamic imports to docs * Update docs transaction example * Minor doc formatting fix
1 parent 09441e7 commit 55186d1

File tree

1 file changed

+47
-26
lines changed

1 file changed

+47
-26
lines changed

README.md

+47-26
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ async function insertUser({ name, age }) {
6262
}
6363
```
6464

65+
#### ESM dynamic imports
66+
67+
The library can be used with ESM dynamic imports as well as shown here.
68+
69+
```js
70+
const { default: postgres } = await import('postgres')
71+
```
72+
6573
## Table of Contents
6674

6775
* [Connection](#connection)
@@ -158,7 +166,7 @@ const users = await sql`
158166
```js
159167
const columns = ['name', 'age']
160168

161-
sql`
169+
await sql`
162170
select
163171
${ sql(columns) }
164172
from users
@@ -211,13 +219,13 @@ const users = [{
211219
age: 80
212220
}]
213221

214-
sql`insert into users ${ sql(users, 'name', 'age') }`
222+
await sql`insert into users ${ sql(users, 'name', 'age') }`
215223

216224
// Is translated to:
217225
insert into users ("name", "age") values ($1, $2), ($3, $4)
218226

219227
// Here you can also omit column names which will use object keys as columns
220-
sql`insert into users ${ sql(users) }`
228+
await sql`insert into users ${ sql(users) }`
221229

222230
// Which results in:
223231
insert into users ("name", "age") values ($1, $2), ($3, $4)
@@ -261,7 +269,7 @@ const users = [
261269
[2, 'Jane', 27],
262270
]
263271

264-
sql`
272+
await sql`
265273
update users set name = update_data.name, (age = update_data.age)::int
266274
from (values ${sql(users)}) as update_data (id, name, age)
267275
where users.id = (update_data.id)::int
@@ -300,7 +308,7 @@ const olderThan = x => sql`and age > ${ x }`
300308

301309
const filterAge = true
302310

303-
sql`
311+
await sql`
304312
select
305313
*
306314
from users
@@ -318,7 +326,7 @@ select * from users where name is not null and age > 50
318326

319327
### Dynamic filters
320328
```js
321-
sql`
329+
await sql`
322330
select
323331
*
324332
from users ${
@@ -339,7 +347,7 @@ Using keywords or calling functions dynamically is also possible by using ``` sq
339347
```js
340348
const date = null
341349

342-
sql`
350+
await sql`
343351
update users set updated_at = ${ date || sql`now()` }
344352
`
345353

@@ -353,7 +361,7 @@ Dynamic identifiers like table names and column names is also supported like so:
353361
const table = 'users'
354362
, column = 'id'
355363

356-
sql`
364+
await sql`
357365
select ${ sql(column) } from ${ sql(table) }
358366
`
359367

@@ -367,10 +375,10 @@ Here's a quick oversight over all the ways to do interpolation in a query templa
367375

368376
| Interpolation syntax | Usage | Example |
369377
| ------------- | ------------- | ------------- |
370-
| `${ sql`` }` | for keywords or sql fragments | ``sql`SELECT * FROM users ${sql`order by age desc` }` `` |
371-
| `${ sql(string) }` | for identifiers | ``sql`SELECT * FROM ${sql('table_name')` `` |
372-
| `${ sql([] or {}, ...) }` | for helpers | ``sql`INSERT INTO users ${sql({ name: 'Peter'})}` `` |
373-
| `${ 'somevalue' }` | for values | ``sql`SELECT * FROM users WHERE age = ${42}` `` |
378+
| `${ sql`` }` | for keywords or sql fragments | ``await sql`SELECT * FROM users ${sql`order by age desc` }` `` |
379+
| `${ sql(string) }` | for identifiers | ``await sql`SELECT * FROM ${sql('table_name')` `` |
380+
| `${ sql([] or {}, ...) }` | for helpers | ``await sql`INSERT INTO users ${sql({ name: 'Peter'})}` `` |
381+
| `${ 'somevalue' }` | for values | ``await sql`SELECT * FROM users WHERE age = ${42}` `` |
374382

375383
## Advanced query methods
376384

@@ -450,7 +458,7 @@ await sql`
450458
Rather than executing a given query, `.describe` will return information utilized in the query process. This information can include the query identifier, column types, etc.
451459

452460
This is useful for debugging and analyzing your Postgres queries. Furthermore, **`.describe` will give you access to the final generated query string that would be executed.**
453-
461+
454462
### Rows as Array of Values
455463
#### ```sql``.values()```
456464

@@ -477,7 +485,7 @@ const result = await sql.file('query.sql', ['Murray', 68])
477485
### Multiple statements in one query
478486
#### ```await sql``.simple()```
479487

480-
The postgres wire protocol supports ["simple"](https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.6.7.4) and ["extended"](https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY) queries. "simple" queries supports multiple statements, but does not support any dynamic parameters. "extended" queries support parameters but only one statement. To use "simple" queries you can use
488+
The postgres wire protocol supports ["simple"](https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.6.7.4) and ["extended"](https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY) queries. "simple" queries supports multiple statements, but does not support any dynamic parameters. "extended" queries support parameters but only one statement. To use "simple" queries you can use
481489
```sql``.simple()```. That will create it as a simple query.
482490

483491
```js
@@ -519,8 +527,8 @@ await pipeline(readableStream, createWriteStream('output.tsv'))
519527
```js
520528
const readableStream = await sql`
521529
copy (
522-
select name, age
523-
from users
530+
select name, age
531+
from users
524532
where age = 68
525533
) to stdout
526534
`.readable()
@@ -559,7 +567,7 @@ If you know what you're doing, you can use `unsafe` to pass any string you'd lik
559567
```js
560568
sql.unsafe('select ' + danger + ' from users where id = ' + dragons)
561569
```
562-
570+
563571
You can also nest `sql.unsafe` within a safe `sql` expression. This is useful if only part of your fraction has unsafe elements.
564572

565573
```js
@@ -599,7 +607,7 @@ const [user, account] = await sql.begin(async sql => {
599607
) values (
600608
'Murray'
601609
)
602-
returning *
610+
returning *
603611
`
604612

605613
const [account] = await sql`
@@ -608,7 +616,7 @@ const [user, account] = await sql.begin(async sql => {
608616
) values (
609617
${ user.user_id }
610618
)
611-
returning *
619+
returning *
612620
`
613621

614622
return [user, account]
@@ -676,7 +684,7 @@ sql.begin('read write', async sql => {
676684
'Murray'
677685
)
678686
`
679-
687+
680688
await sql.prepare('tx1')
681689
})
682690
```
@@ -736,7 +744,7 @@ console.log(data) // [ { a_test: 1 } ]
736744
737745
### Transform `undefined` Values
738746

739-
By default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed
747+
By default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed
740748

741749
```js
742750
// Transform the column names to and from camel case
@@ -817,7 +825,7 @@ The optional `onlisten` method is great to use for a very simply queue mechanism
817825

818826
```js
819827
await sql.listen(
820-
'jobs',
828+
'jobs',
821829
(x) => run(JSON.parse(x)),
822830
( ) => sql`select unfinished_jobs()`.forEach(run)
823831
)
@@ -850,7 +858,7 @@ CREATE PUBLICATION alltables FOR ALL TABLES
850858
const sql = postgres({ publications: 'alltables' })
851859

852860
const { unsubscribe } = await sql.subscribe(
853-
'insert:events',
861+
'insert:events',
854862
(row, { command, relation, key, old }) => {
855863
// Callback function for each row change
856864
// tell about new event row over eg. websockets or do something else
@@ -986,6 +994,19 @@ const sql = postgres('postgres://username:password@host:port/database', {
986994

987995
Note that `max_lifetime = 60 * (30 + Math.random() * 30)` by default. This resolves to an interval between 45 and 90 minutes to optimize for the benefits of prepared statements **and** working nicely with Linux's OOM killer.
988996

997+
### Dynamic passwords
998+
999+
When clients need to use alternative authentication schemes such as access tokens or connections to databases with rotating passwords, provide either a synchronous or asynchronous function that will resolve the dynamic password value at connection time.
1000+
1001+
```js
1002+
const sql = postgres(url, {
1003+
// Other connection config
1004+
...
1005+
// Password function for the database user
1006+
password : async () => await signer.getAuthToken(),
1007+
})
1008+
```
1009+
9891010
### SSL
9901011

9911012
Although [vulnerable to MITM attacks](https://security.stackexchange.com/a/229297/174913), a common configuration for the `ssl` option for some cloud providers is to set `rejectUnauthorized` to `false` (if `NODE_ENV` is `production`):
@@ -1144,7 +1165,7 @@ const sql = postgres({
11441165
})
11451166

11461167
// Now you can use sql.typed.rect() as specified above
1147-
const [custom] = sql`
1168+
const [custom] = await sql`
11481169
insert into rectangles (
11491170
name,
11501171
rect
@@ -1174,8 +1195,8 @@ const sql = postgres({
11741195
const ssh = new ssh2.Client()
11751196
ssh
11761197
.on('error', reject)
1177-
.on('ready', () =>
1178-
ssh.forwardOut('127.0.0.1', 12345, host, port,
1198+
.on('ready', () =>
1199+
ssh.forwardOut('127.0.0.1', 12345, host, port,
11791200
(err, socket) => err ? reject(err) : resolve(socket)
11801201
)
11811202
)

0 commit comments

Comments
 (0)