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
sql`insert into users ${sql(users, 'name', 'age') }`
222
+
awaitsql`insert into users ${sql(users, 'name', 'age') }`
215
223
216
224
// Is translated to:
217
225
insert into users ("name", "age") values ($1, $2), ($3, $4)
218
226
219
227
// Here you can also omit column names which will use object keys as columns
220
-
sql`insert into users ${sql(users) }`
228
+
awaitsql`insert into users ${sql(users) }`
221
229
222
230
// Which results in:
223
231
insert into users ("name", "age") values ($1, $2), ($3, $4)
@@ -261,7 +269,7 @@ const users = [
261
269
[2, 'Jane', 27],
262
270
]
263
271
264
-
sql`
272
+
awaitsql`
265
273
update users set name =update_data.name, (age =update_data.age)::int
266
274
from (values${sql(users)}) as update_data (id, name, age)
267
275
whereusers.id= (update_data.id)::int
@@ -300,7 +308,7 @@ const olderThan = x => sql`and age > ${ x }`
300
308
301
309
constfilterAge=true
302
310
303
-
sql`
311
+
awaitsql`
304
312
select
305
313
*
306
314
from users
@@ -318,7 +326,7 @@ select * from users where name is not null and age > 50
318
326
319
327
### Dynamic filters
320
328
```js
321
-
sql`
329
+
awaitsql`
322
330
select
323
331
*
324
332
from users ${
@@ -339,7 +347,7 @@ Using keywords or calling functions dynamically is also possible by using ``` sq
339
347
```js
340
348
constdate=null
341
349
342
-
sql`
350
+
awaitsql`
343
351
update users set updated_at =${ date ||sql`now()`}
344
352
`
345
353
@@ -353,7 +361,7 @@ Dynamic identifiers like table names and column names is also supported like so:
353
361
consttable='users'
354
362
, column ='id'
355
363
356
-
sql`
364
+
awaitsql`
357
365
select${sql(column) }from${sql(table) }
358
366
`
359
367
@@ -367,10 +375,10 @@ Here's a quick oversight over all the ways to do interpolation in a query templa
367
375
368
376
| Interpolation syntax | Usage | Example |
369
377
| ------------- | ------------- | ------------- |
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}` ``|
374
382
375
383
## Advanced query methods
376
384
@@ -450,7 +458,7 @@ await sql`
450
458
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.
451
459
452
460
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.**
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
481
489
```sql``.simple()```. That will create it as a simple query.
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.
988
996
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
+
constsql=postgres(url, {
1003
+
// Other connection config
1004
+
...
1005
+
// Password function for the database user
1006
+
password :async () =>awaitsigner.getAuthToken(),
1007
+
})
1008
+
```
1009
+
989
1010
### SSL
990
1011
991
1012
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({
1144
1165
})
1145
1166
1146
1167
// Now you can use sql.typed.rect() as specified above
0 commit comments