neon-postgres 3.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,1392 @@
1
+ <img align="left" width="440" height="180" alt="Fastest full PostgreSQL nodejs client" src="https://raw.githubusercontent.com/porsager/postgres/master/postgresjs.svg?sanitize=true">
2
+
3
+ - [🚀 Fastest full-featured node & deno client](https://github.com/porsager/postgres-benchmarks#results)
4
+ - 🏷 ES6 Tagged Template Strings at the core
5
+ - 🏄‍♀️ Simple surface API
6
+ - 🖊️ Dynamic query support
7
+ - 💬 Chat and help on [Gitter](https://gitter.im/porsager/postgres)
8
+ - 🐦 Follow on [Twitter](https://twitter.com/rporsager)
9
+
10
+ <br>
11
+
12
+ ## Getting started
13
+
14
+ <br>
15
+ <img height="220" width="458" alt="Good UX with Postgres.js" src="https://raw.githubusercontent.com/porsager/postgres/master/demo.gif">
16
+ <br>
17
+
18
+ ### Installation
19
+ ```bash
20
+ $ npm install postgres
21
+ ```
22
+
23
+ ### Usage
24
+ Create your `sql` database instance
25
+ ```js
26
+ // db.js
27
+ import postgres from 'postgres'
28
+
29
+ const sql = postgres({ /* options */ }) // will use psql environment variables
30
+
31
+ export default sql
32
+ ```
33
+
34
+ Simply import for use elsewhere
35
+ ```js
36
+ // users.js
37
+ import sql from './db.js'
38
+
39
+ async function getUsersOver(age) {
40
+ const users = await sql`
41
+ select
42
+ name,
43
+ age
44
+ from users
45
+ where age > ${ age }
46
+ `
47
+ // users = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...]
48
+ return users
49
+ }
50
+
51
+
52
+ async function insertUser({ name, age }) {
53
+ const users = await sql`
54
+ insert into users
55
+ (name, age)
56
+ values
57
+ (${ name }, ${ age })
58
+ returning name, age
59
+ `
60
+ // users = Result [{ name: "Murray", age: 68 }]
61
+ return users
62
+ }
63
+ ```
64
+
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
+
73
+ ## Table of Contents
74
+
75
+ * [Connection](#connection)
76
+ * [Queries](#queries)
77
+ * [Building queries](#building-queries)
78
+ * [Advanced query methods](#advanced-query-methods)
79
+ * [Transactions](#transactions)
80
+ * [Data Transformation](#data-transformation)
81
+ * [Listen & notify](#listen--notify)
82
+ * [Realtime subscribe](#realtime-subscribe)
83
+ * [Numbers, bigint, numeric](#numbers-bigint-numeric)
84
+ * [Result Array](#result-array)
85
+ * [Connection details](#connection-details)
86
+ * [Custom Types](#custom-types)
87
+ * [Teardown / Cleanup](#teardown--cleanup)
88
+ * [Error handling](#error-handling)
89
+ * [TypeScript support](#typescript-support)
90
+ * [Reserving connections](#reserving-connections)
91
+ * [Changelog](./CHANGELOG.md)
92
+
93
+
94
+ ## Connection
95
+
96
+ ### `postgres([url], [options])`
97
+
98
+ You can use either a `postgres://` url connection string or the options to define your database connection properties. Options in the object will override any present in the url. Options will fall back to the same environment variables as psql.
99
+
100
+ ```js
101
+ const sql = postgres('postgres://username:password@host:port/database', {
102
+ host : '', // Postgres ip address[s] or domain name[s]
103
+ port : 5432, // Postgres server port[s]
104
+ database : '', // Name of database to connect to
105
+ username : '', // Username of database user
106
+ password : '', // Password of database user
107
+ ...and more
108
+ })
109
+ ```
110
+
111
+ More options can be found in the [Connection details section](#connection-details).
112
+
113
+ ## Queries
114
+
115
+ ### ```await sql`...` -> Result[]```
116
+
117
+ Postgres.js utilizes [Tagged template functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) to process query parameters **before** interpolation. Using tagged template literals benefits developers by:
118
+
119
+ 1. **Enforcing** safe query generation
120
+ 2. Giving the ` sql`` ` function powerful [utility](#dynamic-inserts) and [query building](#building-queries) features.
121
+
122
+ Any generic value will be serialized according to an inferred type, and replaced by a PostgreSQL protocol placeholder `$1, $2, ...`. The parameters are then sent separately to the database which handles escaping & casting.
123
+
124
+ All queries will return a `Result` array, with objects mapping column names to each row.
125
+
126
+ ```js
127
+ const xs = await sql`
128
+ insert into users (
129
+ name, age
130
+ ) values (
131
+ 'Murray', 68
132
+ )
133
+
134
+ returning *
135
+ `
136
+
137
+ // xs = [{ user_id: 1, name: 'Murray', age: 68 }]
138
+ ```
139
+
140
+ > Please note that queries are first executed when `awaited` – or instantly by using [`.execute()`](#execute).
141
+
142
+ ### Query parameters
143
+
144
+ Parameters are automatically extracted and handled by the database so that SQL injection isn't possible. No special handling is necessary, simply use tagged template literals as usual.
145
+
146
+ ```js
147
+ const name = 'Mur'
148
+ , age = 60
149
+
150
+ const users = await sql`
151
+ select
152
+ name,
153
+ age
154
+ from users
155
+ where
156
+ name like ${ name + '%' }
157
+ and age > ${ age }
158
+ `
159
+ // users = [{ name: 'Murray', age: 68 }]
160
+ ```
161
+
162
+ > Be careful with quotation marks here. Because Postgres infers column types, you do not need to wrap your interpolated parameters in quotes like `'${name}'`. This will cause an error because the tagged template replaces `${name}` with `$1` in the query string, leaving Postgres to do the interpolation. If you wrap that in a string, Postgres will see `'$1'` and interpret it as a string as opposed to a parameter.
163
+
164
+ ### Dynamic column selection
165
+
166
+ ```js
167
+ const columns = ['name', 'age']
168
+
169
+ await sql`
170
+ select
171
+ ${ sql(columns) }
172
+ from users
173
+ `
174
+
175
+ // Which results in:
176
+ select "name", "age" from users
177
+ ```
178
+
179
+ ### Dynamic inserts
180
+
181
+ ```js
182
+ const user = {
183
+ name: 'Murray',
184
+ age: 68
185
+ }
186
+
187
+ await sql`
188
+ insert into users ${
189
+ sql(user, 'name', 'age')
190
+ }
191
+ `
192
+
193
+ // Which results in:
194
+ insert into users ("name", "age") values ($1, $2)
195
+
196
+ // The columns can also be given with an array
197
+ const columns = ['name', 'age']
198
+
199
+ await sql`
200
+ insert into users ${
201
+ sql(user, columns)
202
+ }
203
+ `
204
+ ```
205
+
206
+ **You can omit column names and simply execute `sql(user)` to get all the fields from the object as columns**. Be careful not to allow users to supply columns that you do not want to be inserted.
207
+
208
+ #### Multiple inserts in one query
209
+ If you need to insert multiple rows at the same time it's also much faster to do it with a single `insert`. Simply pass an array of objects to `sql()`.
210
+
211
+ ```js
212
+ const users = [{
213
+ name: 'Murray',
214
+ age: 68,
215
+ garbage: 'ignore'
216
+ },
217
+ {
218
+ name: 'Walter',
219
+ age: 80
220
+ }]
221
+
222
+ await sql`insert into users ${ sql(users, 'name', 'age') }`
223
+
224
+ // Is translated to:
225
+ insert into users ("name", "age") values ($1, $2), ($3, $4)
226
+
227
+ // Here you can also omit column names which will use object keys as columns
228
+ await sql`insert into users ${ sql(users) }`
229
+
230
+ // Which results in:
231
+ insert into users ("name", "age") values ($1, $2), ($3, $4)
232
+ ```
233
+
234
+ ### Dynamic columns in updates
235
+ This is also useful for update queries
236
+ ```js
237
+ const user = {
238
+ id: 1,
239
+ name: 'Murray',
240
+ age: 68
241
+ }
242
+
243
+ await sql`
244
+ update users set ${
245
+ sql(user, 'name', 'age')
246
+ }
247
+ where user_id = ${ user.id }
248
+ `
249
+
250
+ // Which results in:
251
+ update users set "name" = $1, "age" = $2 where user_id = $3
252
+
253
+ // The columns can also be given with an array
254
+ const columns = ['name', 'age']
255
+
256
+ await sql`
257
+ update users set ${
258
+ sql(user, columns)
259
+ }
260
+ where user_id = ${ user.id }
261
+ `
262
+ ```
263
+
264
+ ### Multiple updates in one query
265
+ To create multiple updates in a single query, it is necessary to use arrays instead of objects to ensure that the order of the items correspond with the column names.
266
+ ```js
267
+ const users = [
268
+ [1, 'John', 34],
269
+ [2, 'Jane', 27],
270
+ ]
271
+
272
+ await sql`
273
+ update users set name = update_data.name, age = (update_data.age)::int
274
+ from (values ${sql(users)}) as update_data (id, name, age)
275
+ where users.id = (update_data.id)::int
276
+ returning users.id, users.name, users.age
277
+ `
278
+ ```
279
+
280
+ ### Dynamic values and `where in`
281
+ Value lists can also be created dynamically, making `where in` queries simple too.
282
+ ```js
283
+ const users = await sql`
284
+ select
285
+ *
286
+ from users
287
+ where age in ${ sql([68, 75, 23]) }
288
+ `
289
+ ```
290
+
291
+ or
292
+ ```js
293
+ const [{ a, b, c }] = await sql`
294
+ select
295
+ *
296
+ from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)
297
+ `
298
+ ```
299
+
300
+ ## Building queries
301
+
302
+ Postgres.js features a simple dynamic query builder by conditionally appending/omitting query fragments.
303
+ It works by nesting ` sql`` ` fragments within other ` sql`` ` calls or fragments. This allows you to build dynamic queries safely without risking sql injections through usual string concatenation.
304
+
305
+ ### Partial queries
306
+ ```js
307
+ const olderThan = x => sql`and age > ${ x }`
308
+
309
+ const filterAge = true
310
+
311
+ await sql`
312
+ select
313
+ *
314
+ from users
315
+ where name is not null ${
316
+ filterAge
317
+ ? olderThan(50)
318
+ : sql``
319
+ }
320
+ `
321
+ // Which results in:
322
+ select * from users where name is not null
323
+ // Or
324
+ select * from users where name is not null and age > 50
325
+ ```
326
+
327
+ ### Dynamic filters
328
+ ```js
329
+ await sql`
330
+ select
331
+ *
332
+ from users ${
333
+ id
334
+ ? sql`where user_id = ${ id }`
335
+ : sql``
336
+ }
337
+ `
338
+
339
+ // Which results in:
340
+ select * from users
341
+ // Or
342
+ select * from users where user_id = $1
343
+ ```
344
+
345
+ ### Dynamic ordering
346
+
347
+ ```js
348
+ const id = 1
349
+ const order = {
350
+ username: 'asc'
351
+ created_at: 'desc'
352
+ }
353
+ await sql`
354
+ select
355
+ *
356
+ from ticket
357
+ where account = ${ id }
358
+ order by ${
359
+ Object.entries(order).flatMap(([column, order], i) =>
360
+ [i ? sql`,` : sql``, sql`${ sql(column) } ${ order === 'desc' ? sql`desc` : sql`asc` }`]
361
+ )
362
+ }
363
+ `
364
+ ```
365
+
366
+ ### SQL functions
367
+ Using keywords or calling functions dynamically is also possible by using ``` sql`` ``` fragments.
368
+ ```js
369
+ const date = null
370
+
371
+ await sql`
372
+ update users set updated_at = ${ date || sql`now()` }
373
+ `
374
+
375
+ // Which results in:
376
+ update users set updated_at = now()
377
+ ```
378
+
379
+ ### Table names
380
+ Dynamic identifiers like table names and column names is also supported like so:
381
+ ```js
382
+ const table = 'users'
383
+ , column = 'id'
384
+
385
+ await sql`
386
+ select ${ sql(column) } from ${ sql(table) }
387
+ `
388
+
389
+ // Which results in:
390
+ select "id" from "users"
391
+ ```
392
+
393
+ ### Quick primer on interpolation
394
+
395
+ Here's a quick oversight over all the ways to do interpolation in a query template string:
396
+
397
+ | Interpolation syntax | Usage | Example |
398
+ | ------------- | ------------- | ------------- |
399
+ | `${ sql`` }` | for keywords or sql fragments | ``await sql`SELECT * FROM users ${sql`order by age desc` }` `` |
400
+ | `${ sql(string) }` | for identifiers | ``await sql`SELECT * FROM ${sql('table_name')` `` |
401
+ | `${ sql([] or {}, ...) }` | for helpers | ``await sql`INSERT INTO users ${sql({ name: 'Peter'})}` `` |
402
+ | `${ 'somevalue' }` | for values | ``await sql`SELECT * FROM users WHERE age = ${42}` `` |
403
+
404
+ ## Advanced query methods
405
+
406
+ ### Cursors
407
+
408
+ #### ```await sql``.cursor([rows = 1], [fn])```
409
+
410
+ Use cursors if you need to throttle the amount of rows being returned from a query. You can use a cursor either as an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) or with a callback function. For a callback function new results won't be requested until the promise / async callback function has resolved.
411
+
412
+ ##### callback function
413
+ ```js
414
+ await sql`
415
+ select
416
+ *
417
+ from generate_series(1,4) as x
418
+ `.cursor(async([row]) => {
419
+ // row = { x: 1 }
420
+ await http.request('https://example.com/wat', { row })
421
+ })
422
+ ```
423
+
424
+ ##### for await...of
425
+ ```js
426
+ // for await...of
427
+ const cursor = sql`select * from generate_series(1,4) as x`.cursor()
428
+
429
+ for await (const [row] of cursor) {
430
+ // row = { x: 1 }
431
+ await http.request('https://example.com/wat', { row })
432
+ }
433
+ ```
434
+
435
+ A single row will be returned by default, but you can also request batches by setting the number of rows desired in each batch as the first argument to `.cursor`:
436
+ ```js
437
+ await sql`
438
+ select
439
+ *
440
+ from generate_series(1,1000) as x
441
+ `.cursor(10, async rows => {
442
+ // rows = [{ x: 1 }, { x: 2 }, ... ]
443
+ await Promise.all(rows.map(row =>
444
+ http.request('https://example.com/wat', { row })
445
+ ))
446
+ })
447
+ ```
448
+
449
+ If an error is thrown inside the callback function no more rows will be requested and the outer promise will reject with the thrown error.
450
+
451
+ You can close the cursor early either by calling `break` in the `for await...of` loop, or by returning the token `sql.CLOSE` from the callback function.
452
+
453
+ ```js
454
+ await sql`
455
+ select * from generate_series(1,1000) as x
456
+ `.cursor(row => {
457
+ return Math.random() > 0.9 && sql.CLOSE // or sql.END
458
+ })
459
+ ```
460
+
461
+ ### Instant iteration
462
+
463
+ #### ```await sql``.forEach(fn)```
464
+
465
+ If you want to handle rows returned by a query one by one, you can use `.forEach` which returns a promise that resolves once there are no more rows.
466
+ ```js
467
+ await sql`
468
+ select created_at, name from events
469
+ `.forEach(row => {
470
+ // row = { created_at: '2019-11-22T14:22:00Z', name: 'connected' }
471
+ })
472
+
473
+ // No more rows
474
+ ```
475
+
476
+ ### Query Descriptions
477
+ #### ```await sql``.describe() -> Result[]```
478
+
479
+ 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.
480
+
481
+ 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.**
482
+
483
+ ### Rows as Array of Values
484
+ #### ```sql``.values()```
485
+
486
+ Using `.values` will return rows as an array of values for each column, instead of objects.
487
+
488
+ This can be useful to receive identically named columns, or for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.
489
+
490
+ ### Rows as Raw Array of Buffers
491
+ #### ```sql``.raw()```
492
+
493
+ Using `.raw` will return rows as an array with `Buffer` values for each column, instead of objects.
494
+
495
+ This can be useful for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.
496
+
497
+ ### Queries in Files
498
+ #### `await sql.file(path, [args], [options]) -> Result[]`
499
+
500
+ Using a file for a query is also supported with optional parameters to use if the file includes `$1, $2, etc`
501
+
502
+ ```js
503
+ const result = await sql.file('query.sql', ['Murray', 68])
504
+ ```
505
+
506
+ ### Multiple statements in one query
507
+ #### ```await sql``.simple()```
508
+
509
+ 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
510
+ ```sql``.simple()```. That will create it as a simple query.
511
+
512
+ ```js
513
+ await sql`select 1; select 2;`.simple()
514
+ ```
515
+
516
+ ### Copy to/from as Streams
517
+
518
+ Postgres.js supports [`COPY ...`](https://www.postgresql.org/docs/14/sql-copy.html) queries, which are exposed as [Node.js streams](https://nodejs.org/api/stream.html).
519
+
520
+ #### ```await sql`copy ... from stdin`.writable() -> Writable```
521
+
522
+ ```js
523
+ import { pipeline } from 'node:stream/promises'
524
+
525
+ // Stream of users with the default tab delimitated cells and new-line delimitated rows
526
+ const userStream = Readable.from([
527
+ 'Murray\t68\n',
528
+ 'Walter\t80\n'
529
+ ])
530
+
531
+ const query = await sql`copy users (name, age) from stdin`.writable()
532
+ await pipeline(userStream, query);
533
+ ```
534
+
535
+ #### ```await sql`copy ... to stdout`.readable() -> Readable```
536
+
537
+ ##### Using Stream Pipeline
538
+ ```js
539
+ import { pipeline } from 'node:stream/promises'
540
+ import { createWriteStream } from 'node:fs'
541
+
542
+ const readableStream = await sql`copy users (name, age) to stdout`.readable()
543
+ await pipeline(readableStream, createWriteStream('output.tsv'))
544
+ // output.tsv content: `Murray\t68\nWalter\t80\n`
545
+ ```
546
+
547
+ ##### Using `for await...of`
548
+ ```js
549
+ const readableStream = await sql`
550
+ copy (
551
+ select name, age
552
+ from users
553
+ where age = 68
554
+ ) to stdout
555
+ `.readable()
556
+ for await (const chunk of readableStream) {
557
+ // chunk.toString() === `Murray\t68\n`
558
+ }
559
+ ```
560
+
561
+ > **NOTE** This is a low-level API which does not provide any type safety. To make this work, you must match your [`copy query` parameters](https://www.postgresql.org/docs/14/sql-copy.html) correctly to your [Node.js stream read or write](https://nodejs.org/api/stream.html) code. Ensure [Node.js stream backpressure](https://nodejs.org/en/learn/modules/backpressuring-in-streams) is handled correctly to avoid memory exhaustion.
562
+
563
+ ### Canceling Queries in Progress
564
+
565
+ Postgres.js supports, [canceling queries in progress](https://www.postgresql.org/docs/7.1/protocol-protocol.html#AEN39000). It works by opening a new connection with a protocol level startup message to cancel the current query running on a specific connection. That means there is no guarantee that the query will be canceled, and due to the possible race conditions it might even result in canceling another query. This is fine for long running queries, but in the case of high load and fast queries it might be better to simply ignore results instead of canceling.
566
+
567
+ ```js
568
+ const query = sql`select pg_sleep 100`.execute()
569
+ setTimeout(() => query.cancel(), 100)
570
+ const result = await query
571
+ ```
572
+
573
+ ### Execute
574
+
575
+ #### ```await sql``.execute()```
576
+
577
+ The lazy Promise implementation in Postgres.js is what allows it to distinguish [Nested Fragments](#building-queries) from the main outer query. This also means that queries are always executed at the earliest in the following tick. If you have a specific need to execute the query in the same tick, you can call `.execute()`
578
+
579
+ ### Unsafe raw string queries
580
+
581
+ <details>
582
+ <summary>Advanced unsafe use cases</summary>
583
+
584
+ ### `await sql.unsafe(query, [args], [options]) -> Result[]`
585
+
586
+ If you know what you're doing, you can use `unsafe` to pass any string you'd like to postgres. Please note that this can lead to SQL injection if you're not careful.
587
+
588
+ ```js
589
+ sql.unsafe('select ' + danger + ' from users where id = ' + dragons)
590
+ ```
591
+
592
+ By default, `sql.unsafe` assumes the `query` string is sufficiently dynamic that prepared statements do not make sense, and so defaults them to off. If you'd like to re-enable prepared statements, you can pass `{ prepare: true }`.
593
+
594
+ You can also nest `sql.unsafe` within a safe `sql` expression. This is useful if only part of your fraction has unsafe elements.
595
+
596
+ ```js
597
+ const triggerName = 'friend_created'
598
+ const triggerFnName = 'on_friend_created'
599
+ const eventType = 'insert'
600
+ const schema_name = 'app'
601
+ const table_name = 'friends'
602
+
603
+ await sql`
604
+ create or replace trigger ${sql(triggerName)}
605
+ after ${sql.unsafe(eventType)} on ${sql.unsafe(`${schema_name}.${table_name}`)}
606
+ for each row
607
+ execute function ${sql(triggerFnName)}()
608
+ `
609
+
610
+ await sql`
611
+ create role friend_service with login password ${sql.unsafe(`'${password}'`)}
612
+ `
613
+ ```
614
+
615
+ </details>
616
+
617
+ ## Transactions
618
+
619
+ #### BEGIN / COMMIT `await sql.begin([options = ''], fn) -> fn()`
620
+
621
+ Use `sql.begin` to start a new transaction. Postgres.js will reserve a connection for the transaction and supply a scoped `sql` instance for all transaction uses in the callback function. `sql.begin` will resolve with the returned value from the callback function.
622
+
623
+ `BEGIN` is automatically sent with the optional options, and if anything fails `ROLLBACK` will be called so the connection can be released and execution can continue.
624
+
625
+ ```js
626
+ const [user, account] = await sql.begin(async sql => {
627
+ const [user] = await sql`
628
+ insert into users (
629
+ name
630
+ ) values (
631
+ 'Murray'
632
+ )
633
+ returning *
634
+ `
635
+
636
+ const [account] = await sql`
637
+ insert into accounts (
638
+ user_id
639
+ ) values (
640
+ ${ user.user_id }
641
+ )
642
+ returning *
643
+ `
644
+
645
+ return [user, account]
646
+ })
647
+ ```
648
+
649
+ Do note that you can often achieve the same result using [`WITH` queries (Common Table Expressions)](https://www.postgresql.org/docs/current/queries-with.html) instead of using transactions.
650
+
651
+ It's also possible to pipeline the requests in a transaction if needed by returning an array with queries from the callback function like this:
652
+
653
+ ```js
654
+ const result = await sql.begin(sql => [
655
+ sql`update ...`,
656
+ sql`update ...`,
657
+ sql`insert ...`
658
+ ])
659
+ ```
660
+
661
+ #### SAVEPOINT `await sql.savepoint([name], fn) -> fn()`
662
+
663
+ ```js
664
+ sql.begin('read write', async sql => {
665
+ const [user] = await sql`
666
+ insert into users (
667
+ name
668
+ ) values (
669
+ 'Murray'
670
+ )
671
+ `
672
+
673
+ const [account] = (await sql.savepoint(sql =>
674
+ sql`
675
+ insert into accounts (
676
+ user_id
677
+ ) values (
678
+ ${ user.user_id }
679
+ )
680
+ `
681
+ ).catch(err => {
682
+ // Account could not be created. ROLLBACK SAVEPOINT is called because we caught the rejection.
683
+ })) || []
684
+
685
+ return [user, account]
686
+ })
687
+ .then(([user, account]) => {
688
+ // great success - COMMIT succeeded
689
+ })
690
+ .catch(() => {
691
+ // not so good - ROLLBACK was called
692
+ })
693
+ ```
694
+
695
+
696
+ #### PREPARE TRANSACTION `await sql.prepare([name]) -> fn()`
697
+
698
+ Indicates that the transactions should be prepared using the [`PREPARE TRANSACTION [NAME]`](https://www.postgresql.org/docs/current/sql-prepare-transaction.html) statement
699
+ instead of being committed.
700
+
701
+ ```js
702
+ sql.begin('read write', async sql => {
703
+ const [user] = await sql`
704
+ insert into users (
705
+ name
706
+ ) values (
707
+ 'Murray'
708
+ )
709
+ `
710
+
711
+ await sql.prepare('tx1')
712
+ })
713
+ ```
714
+
715
+ ## Data Transformation
716
+
717
+ Postgres.js allows for transformation of the data passed to or returned from a query by using the `transform` option.
718
+
719
+ Built in transformation functions are:
720
+
721
+ * For camelCase - `postgres.camel`, `postgres.toCamel`, `postgres.fromCamel`
722
+ * For PascalCase - `postgres.pascal`, `postgres.toPascal`, `postgres.fromPascal`
723
+ * For Kebab-Case - `postgres.kebab`, `postgres.toKebab`, `postgres.fromKebab`
724
+
725
+ These built in transformations will only convert to/from snake_case. For example, using `{ transform: postgres.toCamel }` will convert the column names to camelCase only if the column names are in snake_case to begin with. `{ transform: postgres.fromCamel }` will convert camelCase only to snake_case.
726
+
727
+ By default, using `postgres.camel`, `postgres.pascal` and `postgres.kebab` will perform a two-way transformation - both the data passed to the query and the data returned by the query will be transformed:
728
+
729
+ ```js
730
+ // Transform the column names to and from camel case
731
+ const sql = postgres({ transform: postgres.camel })
732
+
733
+ await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER, b_test TEXT)`
734
+ await sql`INSERT INTO camel_case ${ sql([{ aTest: 1, bTest: 1 }]) }`
735
+ const data = await sql`SELECT ${ sql('aTest', 'bTest') } FROM camel_case`
736
+
737
+ console.log(data) // [ { aTest: 1, bTest: '1' } ]
738
+ ```
739
+
740
+ To only perform half of the transformation (eg. only the transformation **to** or **from** camel case), use the other transformation functions:
741
+
742
+ ```js
743
+ // Transform the column names only to camel case
744
+ // (for the results that are returned from the query)
745
+ postgres({ transform: postgres.toCamel })
746
+
747
+ await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`
748
+ await sql`INSERT INTO camel_case ${ sql([{ a_test: 1 }]) }`
749
+ const data = await sql`SELECT a_test FROM camel_case`
750
+
751
+ console.log(data) // [ { aTest: 1 } ]
752
+ ```
753
+
754
+ ```js
755
+ // Transform the column names only from camel case
756
+ // (for interpolated inserts, updates, and selects)
757
+ const sql = postgres({ transform: postgres.fromCamel })
758
+
759
+ await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`
760
+ await sql`INSERT INTO camel_case ${ sql([{ aTest: 1 }]) }`
761
+ const data = await sql`SELECT ${ sql('aTest') } FROM camel_case`
762
+
763
+ console.log(data) // [ { a_test: 1 } ]
764
+ ```
765
+
766
+ > Note that Postgres.js does not rewrite the static parts of the tagged template strings. So to transform column names in your queries, the `sql()` helper must be used - eg. `${ sql('columnName') }` as in the examples above.
767
+
768
+ ### Transform `undefined` Values
769
+
770
+ By default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed
771
+
772
+ ```js
773
+ // Transform the column names to and from camel case
774
+ const sql = postgres({
775
+ transform: {
776
+ undefined: null
777
+ }
778
+ })
779
+
780
+ await sql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`
781
+ await sql`INSERT INTO transform_undefined ${ sql([{ a_test: undefined }]) }`
782
+ const data = await sql`SELECT a_test FROM transform_undefined`
783
+
784
+ console.log(data) // [ { a_test: null } ]
785
+ ```
786
+
787
+ To combine with the built in transform functions, spread the transform in the `transform` object:
788
+
789
+ ```js
790
+ // Transform the column names to and from camel case
791
+ const sql = postgres({
792
+ transform: {
793
+ ...postgres.camel,
794
+ undefined: null
795
+ }
796
+ })
797
+
798
+ await sql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`
799
+ await sql`INSERT INTO transform_undefined ${ sql([{ aTest: undefined }]) }`
800
+ const data = await sql`SELECT ${ sql('aTest') } FROM transform_undefined`
801
+
802
+ console.log(data) // [ { aTest: null } ]
803
+ ```
804
+
805
+ ### Custom Transform Functions
806
+
807
+ To specify your own transformation functions, you can use the `column`, `value` and `row` options inside of `transform`, each an object possibly including `to` and `from` keys:
808
+
809
+ * `to`: The function to transform the outgoing query column name to, i.e `SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
810
+ * `from`: The function to transform the incoming query result column name to, see example below.
811
+
812
+ > Both parameters are optional, if not provided, the default transformation function will be used.
813
+
814
+ ```js
815
+ // Implement your own functions, look at postgres.toCamel, etc
816
+ // as a reference:
817
+ // https://github.com/porsager/postgres/blob/4241824ffd7aa94ffb482e54ca9f585d9d0a4eea/src/types.js#L310-L328
818
+ function transformColumnToDatabase() { /* ... */ }
819
+ function transformColumnFromDatabase() { /* ... */ }
820
+
821
+ const sql = postgres({
822
+ transform: {
823
+ column: {
824
+ to: transformColumnToDatabase,
825
+ from: transformColumnFromDatabase,
826
+ },
827
+ value: { /* ... */ },
828
+ row: { /* ... */ }
829
+ }
830
+ })
831
+ ```
832
+
833
+ ## Listen & notify
834
+
835
+ When you call `.listen`, a dedicated connection will be created to ensure that you receive notifications instantly. This connection will be used for any further calls to `.listen`. The connection will automatically reconnect according to a backoff reconnection pattern to not overload the database server.
836
+
837
+ ### Listen `await sql.listen(channel, onnotify, [onlisten]) -> { state }`
838
+ `.listen` takes the channel name, a function to handle each notify, and an optional function to run every time listen is registered and ready (happens on initial connect and reconnects). It returns a promise which resolves once the `LISTEN` query to Postgres completes, or if there is already a listener active.
839
+
840
+ ```js
841
+ await sql.listen('news', payload => {
842
+ const json = JSON.parse(payload)
843
+ console.log(json.this) // logs 'is'
844
+ })
845
+ ```
846
+
847
+ The optional `onlisten` method is great to use for a very simply queue mechanism:
848
+
849
+ ```js
850
+ await sql.listen(
851
+ 'jobs',
852
+ (x) => run(JSON.parse(x)),
853
+ ( ) => sql`select unfinished_jobs()`.forEach(run)
854
+ )
855
+
856
+ function run(job) {
857
+ // And here you do the work you please
858
+ }
859
+ ```
860
+ ### Notify `await sql.notify(channel, payload) -> Result[]`
861
+ Notify can be done as usual in SQL, or by using the `sql.notify` method.
862
+ ```js
863
+ sql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))
864
+ ```
865
+
866
+ ## Realtime subscribe
867
+
868
+ Postgres.js implements the logical replication protocol of PostgreSQL to support subscription to real-time updates of `insert`, `update` and `delete` operations.
869
+
870
+ > **NOTE** To make this work you must [create the proper publications in your database](https://www.postgresql.org/docs/current/sql-createpublication.html), enable logical replication by setting `wal_level = logical` in `postgresql.conf` and connect using either a replication or superuser.
871
+
872
+ ### Quick start
873
+
874
+ #### Create a publication (eg. in migration)
875
+ ```sql
876
+ CREATE PUBLICATION alltables FOR ALL TABLES
877
+ ```
878
+
879
+ #### Subscribe to updates
880
+ ```js
881
+ const sql = postgres({ publications: 'alltables' })
882
+
883
+ const { unsubscribe } = await sql.subscribe(
884
+ 'insert:events',
885
+ (row, { command, relation, key, old }) => {
886
+ // Callback function for each row change
887
+ // tell about new event row over eg. websockets or do something else
888
+ },
889
+ () => {
890
+ // Callback on initial connect and potential reconnects
891
+ }
892
+ )
893
+ ```
894
+
895
+ ### Subscribe pattern
896
+
897
+ You can subscribe to specific operations, tables, or even rows with primary keys.
898
+
899
+ #### `operation` `:` `schema` `.` `table` `=` `primary_key`
900
+
901
+ **`operation`** is one of ``` * | insert | update | delete ``` and defaults to `*`
902
+
903
+ **`schema`** defaults to `public`
904
+
905
+ **`table`** is a specific table name and defaults to `*`
906
+
907
+ **`primary_key`** can be used to only subscribe to specific rows
908
+
909
+ ### Examples
910
+
911
+ ```js
912
+ sql.subscribe('*', () => /* everything */ )
913
+ sql.subscribe('insert', () => /* all inserts */ )
914
+ sql.subscribe('*:users', () => /* all operations on the public.users table */ )
915
+ sql.subscribe('delete:users', () => /* all deletes on the public.users table */ )
916
+ sql.subscribe('update:users=1', () => /* all updates on the users row with a primary key = 1 */ )
917
+ ```
918
+
919
+ ## Numbers, bigint, numeric
920
+
921
+ `Number` in javascript is only able to represent 2<sup>53</sup>-1 safely which means that types in PostgreSQLs like `bigint` and `numeric` won't fit into `Number`.
922
+
923
+ Since Node.js v10.4 we can use [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) to match the PostgreSQL type `bigint` which is returned for eg. `count(*)`. Unfortunately, it doesn't work with `JSON.stringify` out of the box, so Postgres.js will return it as a string.
924
+
925
+ If you want to use `BigInt` you can add this custom type:
926
+
927
+ ```js
928
+ const sql = postgres({
929
+ types: {
930
+ bigint: postgres.BigInt
931
+ }
932
+ })
933
+ ```
934
+
935
+ There is currently no guaranteed way to handle `numeric` / `decimal` types in native Javascript. **These [and similar] types will be returned as a `string`**. The best way in this case is to use [custom types](#custom-types).
936
+
937
+ ## Result Array
938
+
939
+ The `Result` Array returned from queries is a custom array allowing for easy destructuring or passing on directly to JSON.stringify or general Array usage. It includes the following properties.
940
+
941
+ ### .count
942
+
943
+ The `count` property is the number of affected rows returned by the database. This is useful for insert, update and delete operations to know the number of rows since .length will be 0 in these cases if not using `RETURNING ...`.
944
+
945
+ ### .command
946
+
947
+ The `command` run by the query - eg. one of `SELECT`, `UPDATE`, `INSERT`, `DELETE`
948
+
949
+ ### .columns
950
+
951
+ The `columns` returned by the query useful to determine types, or map to the result values when using `.values()`
952
+
953
+ ```js
954
+ {
955
+ name : String, // Column name,
956
+ type : oid, // PostgreSQL oid column type
957
+ parser: Function // The function used by Postgres.js for parsing
958
+ }
959
+ ```
960
+
961
+ ### .statement
962
+
963
+ The `statement` contains information about the statement implicitly created by Postgres.js.
964
+
965
+ ```js
966
+ {
967
+ name : String, // The auto generated statement name
968
+ string : String, // The actual query string executed
969
+ types : [oid], // An array of oid expected as input parameters
970
+ columns : [Column] // Array of columns - same as Result.columns
971
+ }
972
+ ```
973
+
974
+ ### .state
975
+
976
+ This is the state `{ pid, secret }` of the connection that executed the query.
977
+
978
+ ## Connection details
979
+
980
+ ### All Postgres options
981
+
982
+ ```js
983
+ const sql = postgres('postgres://username:password@host:port/database', {
984
+ host : '', // Postgres ip address[es] or domain name[s]
985
+ port : 5432, // Postgres server port[s]
986
+ path : '', // unix socket path (usually '/tmp')
987
+ database : '', // Name of database to connect to
988
+ username : '', // Username of database user
989
+ password : '', // Password of database user
990
+ ssl : false, // true, prefer, require, tls.connect options
991
+ sslnegotiation : null, // direct
992
+ max : 10, // Max number of connections
993
+ max_lifetime : null, // Max lifetime in seconds (more info below)
994
+ idle_timeout : 0, // Idle connection timeout in seconds
995
+ connect_timeout : 30, // Connect timeout in seconds
996
+ prepare : true, // Automatic creation of prepared statements
997
+ types : [], // Array of custom types, see more below
998
+ onnotice : fn, // Default console.log, set false to silence NOTICE
999
+ onparameter : fn, // (key, value) when server param change
1000
+ debug : fn, // Is called with (connection, query, params, types)
1001
+ socket : fn, // fn returning custom socket to use
1002
+ transform : {
1003
+ undefined : undefined, // Transforms undefined values (eg. to null)
1004
+ column : fn, // Transforms incoming column names
1005
+ value : fn, // Transforms incoming row values
1006
+ row : fn // Transforms entire rows
1007
+ },
1008
+ connection : {
1009
+ application_name : 'postgres.js', // Default application_name
1010
+ ... // Other connection parameters, see https://www.postgresql.org/docs/current/runtime-config-client.html
1011
+ },
1012
+ target_session_attrs : null, // Use 'read-write' with multiple hosts to
1013
+ // ensure only connecting to primary
1014
+ fetch_types : true, // Automatically fetches types on connect
1015
+ // on initial connection.
1016
+ })
1017
+ ```
1018
+
1019
+ Note that `max_lifetime = 60 * (30 + Math.random() * 30)` by default. This resolves to an interval between 30 and 60 minutes to optimize for the benefits of prepared statements **and** working nicely with Linux's OOM killer.
1020
+
1021
+ ### Dynamic passwords
1022
+
1023
+ 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.
1024
+
1025
+ ```js
1026
+ const sql = postgres(url, {
1027
+ // Other connection config
1028
+ ...
1029
+ // Password function for the database user
1030
+ password : async () => await signer.getAuthToken(),
1031
+ })
1032
+ ```
1033
+
1034
+ ### SSL
1035
+
1036
+ 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`):
1037
+
1038
+ ```js
1039
+ const sql =
1040
+ process.env.NODE_ENV === 'production'
1041
+ ? // "Unless you're using a Private or Shield Heroku Postgres database, Heroku Postgres does not currently support verifiable certificates"
1042
+ // https://help.heroku.com/3DELT3RK/why-can-t-my-third-party-utility-connect-to-heroku-postgres-with-ssl
1043
+ postgres({ ssl: { rejectUnauthorized: false } })
1044
+ : postgres()
1045
+ ```
1046
+
1047
+ For more information regarding `ssl` with `postgres`, check out the [Node.js documentation for tls](https://nodejs.org/dist/latest-v16.x/docs/api/tls.html#new-tlstlssocketsocket-options).
1048
+
1049
+
1050
+ ### Multi-host connections - High Availability (HA)
1051
+
1052
+ Multiple connection strings can be passed to `postgres()` in the form of `postgres('postgres://localhost:5432,localhost:5433', ...)`. This works the same as native the `psql` command. Read more at [multiple host URIs](https://www.postgresql.org/docs/13/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS).
1053
+
1054
+ Connections will be attempted in order of the specified hosts/ports. On a successful connection, all retries will be reset. This ensures that hosts can come up and down seamlessly.
1055
+
1056
+ If you specify `target_session_attrs: 'primary'` or `PGTARGETSESSIONATTRS=primary` Postgres.js will only connect to the primary host, allowing for zero downtime failovers.
1057
+
1058
+ ### The Connection Pool
1059
+
1060
+ Connections are created lazily once a query is created. This means that simply doing const `sql = postgres(...)` won't have any effect other than instantiating a new `sql` instance.
1061
+
1062
+ > No connection will be made until a query is made.
1063
+
1064
+ For example:
1065
+
1066
+ ```js
1067
+ const sql = postgres() // no connections are opened
1068
+
1069
+ await sql`...` // one connection is now opened
1070
+ await sql`...` // previous opened connection is reused
1071
+
1072
+ // two connections are opened now
1073
+ await Promise.all([
1074
+ sql`...`,
1075
+ sql`...`
1076
+ ])
1077
+ ```
1078
+
1079
+ > When there are high amount of concurrent queries, `postgres` will open as many connections as needed up until `max` number of connections is reached. By default `max` is 10. This can be changed by setting `max` in the `postgres()` call. Example - `postgres('connectionURL', { max: 20 })`.
1080
+
1081
+ This means that we get a much simpler story for error handling and reconnections. Queries will be sent over the wire immediately on the next available connection in the pool. Connections are automatically taken out of the pool if you start a transaction using `sql.begin()`, and automatically returned to the pool once your transaction is done.
1082
+
1083
+ Any query which was already sent over the wire will be rejected if the connection is lost. It'll automatically defer to the error handling you have for that query, and since connections are lazy it'll automatically try to reconnect the next time a query is made. The benefit of this is no weird generic "onerror" handler that tries to get things back to normal, and also simpler application code since you don't have to handle errors out of context.
1084
+
1085
+ There are no guarantees about queries executing in order unless using a transaction with `sql.begin()` or setting `max: 1`. Of course doing a series of queries, one awaiting the other will work as expected, but that's just due to the nature of js async/promise handling, so it's not necessary for this library to be concerned with ordering.
1086
+
1087
+ Since this library automatically creates prepared statements, it also has a default max lifetime for connections to prevent memory bloat on the database itself. This is a random interval for each connection between 45 and 90 minutes. This allows multiple connections to independently come up and down without affecting the service.
1088
+
1089
+ ### Connection timeout
1090
+
1091
+ By default, connections will not close until `.end()` is called. However, it may be useful to have them close automatically when:
1092
+
1093
+ - re-instantiating multiple ` sql`` ` instances
1094
+ - using Postgres.js in a Serverless environment (Lambda, etc.)
1095
+ - using Postgres.js with a database service that automatically closes connections after some time (see [`ECONNRESET` issue](https://github.com/porsager/postgres/issues/179))
1096
+
1097
+ This can be done using the `idle_timeout` or `max_lifetime` options. These configuration options specify the number of seconds to wait before automatically closing an idle connection and the maximum time a connection can exist, respectively.
1098
+
1099
+ For example, to close a connection that has either been idle for 20 seconds or existed for more than 30 minutes:
1100
+
1101
+ ```js
1102
+ const sql = postgres({
1103
+ idle_timeout: 20,
1104
+ max_lifetime: 60 * 30
1105
+ })
1106
+ ```
1107
+
1108
+ ### Cloudflare Workers support
1109
+
1110
+ Postgres.js has built-in support for the [TCP socket API](https://developers.cloudflare.com/workers/runtime-apis/tcp-sockets/) in Cloudflare Workers, which is [on-track](https://github.com/wintercg/proposal-sockets-api) to be standardized and adopted in Node.js and other JavaScript runtimes, such as Deno.
1111
+
1112
+ You can use Postgres.js directly in a Worker, or to benefit from connection pooling and query caching, via the [Hyperdrive](https://developers.cloudflare.com/hyperdrive/learning/connect-to-postgres/#driver-examples) service available to Workers by passing the Hyperdrive `connectionString` when creating a new `postgres` client as follows:
1113
+
1114
+ ```ts
1115
+ // Requires Postgres.js 3.4.0 or later
1116
+ import postgres from 'postgres'
1117
+
1118
+ interface Env {
1119
+ HYPERDRIVE: Hyperdrive;
1120
+ }
1121
+
1122
+ export default async fetch(req: Request, env: Env, ctx: ExecutionContext) {
1123
+ // The Postgres.js library accepts a connection string directly
1124
+ const sql = postgres(env.HYPERDRIVE.connectionString)
1125
+ const results = await sql`SELECT * FROM users LIMIT 10`
1126
+ return Response.json(results)
1127
+ }
1128
+ ```
1129
+
1130
+ In `wrangler.toml` you will need to enable the `nodejs_compat` compatibility flag to allow Postgres.js to operate in the Workers environment:
1131
+
1132
+ ```toml
1133
+ compatibility_flags = ["nodejs_compat"]
1134
+ ```
1135
+
1136
+ ### Auto fetching of array types
1137
+
1138
+ Postgres.js will automatically fetch table/array-type information when it first connects to a database.
1139
+
1140
+ If you have revoked access to `pg_catalog` this feature will no longer work and will need to be disabled.
1141
+
1142
+ You can disable this feature by setting `fetch_types` to `false`.
1143
+
1144
+ ### Environmental variables
1145
+
1146
+ It is also possible to connect to the database without a connection string or any options. Postgres.js will fall back to the common environment variables used by `psql` as in the table below:
1147
+
1148
+ ```js
1149
+ const sql = postgres()
1150
+ ```
1151
+
1152
+ | Option | Environment Variables |
1153
+ | ------------------ | ------------------------ |
1154
+ | `host` | `PGHOST` |
1155
+ | `port` | `PGPORT` |
1156
+ | `database` | `PGDATABASE` |
1157
+ | `username` | `PGUSERNAME` or `PGUSER` |
1158
+ | `password` | `PGPASSWORD` |
1159
+ | `application_name` | `PGAPPNAME` |
1160
+ | `idle_timeout` | `PGIDLE_TIMEOUT` |
1161
+ | `connect_timeout` | `PGCONNECT_TIMEOUT` |
1162
+
1163
+ ### Prepared statements
1164
+
1165
+ Prepared statements will automatically be created for any queries where it can be inferred that the query is static. This can be disabled by using the `prepare: false` option. For instance — this is useful when [using PGBouncer in `transaction mode`](https://github.com/porsager/postgres/issues/93#issuecomment-656290493).
1166
+
1167
+ **update**: [since 1.21.0](https://www.pgbouncer.org/2023/10/pgbouncer-1-21-0)
1168
+ PGBouncer supports protocol-level named prepared statements when [configured
1169
+ properly](https://www.pgbouncer.org/config.html#max_prepared_statements)
1170
+
1171
+ ## Custom Types
1172
+
1173
+ You can add ergonomic support for custom types, or simply use `sql.typed(value, type)` inline, where type is the PostgreSQL `oid` for the type and the correctly serialized string. _(`oid` values for types can be found in the `pg_catalog.pg_type` table.)_
1174
+
1175
+ Adding Query helpers is the cleanest approach which can be done like this:
1176
+
1177
+ ```js
1178
+ const sql = postgres({
1179
+ types: {
1180
+ rect: {
1181
+ // The pg_types oid to pass to the db along with the serialized value.
1182
+ to : 1337,
1183
+
1184
+ // An array of pg_types oids to handle when parsing values coming from the db.
1185
+ from : [1337],
1186
+
1187
+ //Function that transform values before sending them to the db.
1188
+ serialize : ({ x, y, width, height }) => [x, y, width, height],
1189
+
1190
+ // Function that transforms values coming from the db.
1191
+ parse : ([x, y, width, height]) => { x, y, width, height }
1192
+ }
1193
+ }
1194
+ })
1195
+
1196
+ // Now you can use sql.typed.rect() as specified above
1197
+ const [custom] = await sql`
1198
+ insert into rectangles (
1199
+ name,
1200
+ rect
1201
+ ) values (
1202
+ 'wat',
1203
+ ${ sql.typed.rect({ x: 13, y: 37, width: 42, height: 80 }) }
1204
+ )
1205
+ returning *
1206
+ `
1207
+
1208
+ // custom = { name: 'wat', rect: { x: 13, y: 37, width: 42, height: 80 } }
1209
+
1210
+ ```
1211
+
1212
+ ### Custom socket
1213
+
1214
+ Easily do in-process ssh tunneling to your database by providing a custom socket for Postgres.js to use. The function (optionally async) must return a socket-like duplex stream.
1215
+
1216
+ Here's a sample using [ssh2](https://github.com/mscdex/ssh2)
1217
+
1218
+ ```js
1219
+ import ssh2 from 'ssh2'
1220
+
1221
+ const sql = postgres({
1222
+ ...options,
1223
+ socket: ({ host: [host], port: [port] }) => new Promise((resolve, reject) => {
1224
+ const ssh = new ssh2.Client()
1225
+ ssh
1226
+ .on('error', reject)
1227
+ .on('ready', () =>
1228
+ ssh.forwardOut('127.0.0.1', 12345, host, port,
1229
+ (err, socket) => err ? reject(err) : resolve(socket)
1230
+ )
1231
+ )
1232
+ .connect(sshOptions)
1233
+ })
1234
+ })
1235
+ ```
1236
+
1237
+ ## Teardown / Cleanup
1238
+
1239
+ To ensure proper teardown and cleanup on server restarts use `await sql.end()` before `process.exit()`.
1240
+
1241
+ Calling `sql.end()` will reject new queries and return a Promise which resolves when all queries are finished and the underlying connections are closed. If a `{ timeout }` option is provided any pending queries will be rejected once the timeout (in seconds) is reached and the connections will be destroyed.
1242
+
1243
+ #### Sample shutdown using [Prexit](https://github.com/porsager/prexit)
1244
+
1245
+ ```js
1246
+ import prexit from 'prexit'
1247
+
1248
+ prexit(async () => {
1249
+ await sql.end({ timeout: 5 })
1250
+ await new Promise(r => server.close(r))
1251
+ })
1252
+ ```
1253
+
1254
+ ## Reserving connections
1255
+
1256
+ ### `await sql.reserve()`
1257
+
1258
+ The `reserve` method pulls out a connection from the pool, and returns a client that wraps the single connection. This can be used for running queries on an isolated connection.
1259
+
1260
+ ```ts
1261
+ const reserved = await sql.reserve()
1262
+ await reserved`select * from users`
1263
+ await reserved.release()
1264
+ ```
1265
+
1266
+ ### `reserved.release()`
1267
+
1268
+ Once you have finished with the reserved connection, call `release` to add it back to the pool.
1269
+
1270
+ ## Error handling
1271
+
1272
+ Errors are all thrown to related queries and never globally. Errors coming from database itself are always in the [native Postgres format](https://www.postgresql.org/docs/current/errcodes-appendix.html), and the same goes for any [Node.js errors](https://nodejs.org/api/errors.html#errors_common_system_errors) eg. coming from the underlying connection.
1273
+
1274
+ Query errors will contain a stored error with the origin of the query to aid in tracing errors.
1275
+
1276
+ Query errors will also contain the `query` string and the `parameters`. These are not enumerable to avoid accidentally leaking confidential information in logs. To log these it is required to specifically access `error.query` and `error.parameters`, or set `debug: true` in options.
1277
+
1278
+ There are also the following errors specifically for this library.
1279
+
1280
+ ##### UNSAFE_TRANSACTION
1281
+ > Only use sql.begin or max: 1
1282
+
1283
+ To ensure statements in a transaction runs on the same connection (which is required for them to run inside the transaction), you must use [`sql.begin(...)`](#transactions) or only allow a single connection in options (`max: 1`).
1284
+
1285
+ ##### UNDEFINED_VALUE
1286
+ > Undefined values are not allowed
1287
+
1288
+ Postgres.js won't accept `undefined` as values in tagged template queries since it becomes ambiguous what to do with the value. If you want to set something to null, use `null` explicitly.
1289
+
1290
+ ##### MESSAGE_NOT_SUPPORTED
1291
+ > X (X) is not supported
1292
+
1293
+ Whenever a message is received from Postgres which is not supported by this library. Feel free to file an issue if you think something is missing.
1294
+
1295
+ ##### MAX_PARAMETERS_EXCEEDED
1296
+ > Max number of parameters (65534) exceeded
1297
+
1298
+ The postgres protocol doesn't allow more than 65534 (16bit) parameters. If you run into this issue there are various workarounds such as using `sql([...])` to escape values instead of passing them as parameters.
1299
+
1300
+ ##### SASL_SIGNATURE_MISMATCH
1301
+ > Message type X not supported
1302
+
1303
+ When using SASL authentication the server responds with a signature at the end of the authentication flow which needs to match the one on the client. This is to avoid [man-in-the-middle attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack). If you receive this error the connection was canceled because the server did not reply with the expected signature.
1304
+
1305
+ ##### NOT_TAGGED_CALL
1306
+ > Query not called as a tagged template literal
1307
+
1308
+ Making queries has to be done using the sql function as a [tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates). This is to ensure parameters are serialized and passed to Postgres as query parameters with correct types and to avoid SQL injection.
1309
+
1310
+ ##### AUTH_TYPE_NOT_IMPLEMENTED
1311
+ > Auth type X not implemented
1312
+
1313
+ Postgres supports many different authentication types. This one is not supported.
1314
+
1315
+ ##### CONNECTION_CLOSED
1316
+ > write CONNECTION_CLOSED host:port
1317
+
1318
+ This error is thrown if the connection was closed without an error. This should not happen during normal operations, so please create an issue if this was unexpected.
1319
+
1320
+ ##### CONNECTION_ENDED
1321
+ > write CONNECTION_ENDED host:port
1322
+
1323
+ This error is thrown if the user has called [`sql.end()`](#teardown--cleanup) and performed a query afterward.
1324
+
1325
+ ##### CONNECTION_DESTROYED
1326
+ > write CONNECTION_DESTROYED host:port
1327
+
1328
+ This error is thrown for any queries that were pending when the timeout to [`sql.end({ timeout: X })`](#teardown--cleanup) was reached.
1329
+
1330
+ ##### CONNECT_TIMEOUT
1331
+ > write CONNECT_TIMEOUT host:port
1332
+
1333
+ This error is thrown if the startup phase of the connection (tcp, protocol negotiation, and auth) took more than the default 30 seconds or what was specified using `connect_timeout` or `PGCONNECT_TIMEOUT`.
1334
+
1335
+ ##### COPY_IN_PROGRESS
1336
+ > You cannot execute queries during copy
1337
+
1338
+ This error is thrown if trying to run a query during a copy operation (writable / readable).
1339
+
1340
+ ## TypeScript support
1341
+
1342
+ `postgres` has TypeScript support. You can pass a row list type for your queries in this way:
1343
+ ```ts
1344
+ interface User {
1345
+ id: number
1346
+ name: string
1347
+ }
1348
+
1349
+ const users = await sql<User[]>`SELECT * FROM users`
1350
+ users[0].id // ok => number
1351
+ users[1].name // ok => string
1352
+ users[0].invalid // fails: `invalid` does not exists on `User`
1353
+ ```
1354
+
1355
+ However, be sure to check the array length to avoid accessing properties of `undefined` rows:
1356
+ ```ts
1357
+ const users = await sql<User[]>`SELECT * FROM users WHERE id = ${id}`
1358
+ if (!users.length)
1359
+ throw new Error('Not found')
1360
+ return users[0]
1361
+ ```
1362
+
1363
+ You can also prefer destructuring when you only care about a fixed number of rows.
1364
+ In this case, we recommend you to prefer using tuples to handle `undefined` properly:
1365
+ ```ts
1366
+ const [user]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}`
1367
+ if (!user) // => User | undefined
1368
+ throw new Error('Not found')
1369
+ return user // => User
1370
+
1371
+ // NOTE:
1372
+ const [first, second]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}` // fails: `second` does not exist on `[User?]`
1373
+ const [first, second] = await sql<[User?]>`SELECT * FROM users WHERE id = ${id}` // don't fail : `second: User | undefined`
1374
+ ```
1375
+
1376
+ We do our best to type all the public API, however types are not always updated when features are added or changed. Feel free to open an issue if you have trouble with types.
1377
+
1378
+ ## Migration tools
1379
+
1380
+ Postgres.js doesn't come with any migration solution since it's way out of scope, but here are some modules that support Postgres.js for migrations:
1381
+
1382
+ - https://github.com/porsager/postgres-shift
1383
+ - https://github.com/lukeed/ley
1384
+ - https://github.com/JAForbes/pgmg
1385
+
1386
+ ## Thank you
1387
+
1388
+ A really big thank you to [@JAForbes](https://twitter.com/jmsfbs) who introduced me to Postgres and still holds my hand navigating all the great opportunities we have.
1389
+
1390
+ Thanks to [@ACXgit](https://twitter.com/andreacoiutti) for initial tests and dogfooding.
1391
+
1392
+ Also thanks to [Ryan Dahl](https://github.com/ry) for letting me have the `postgres` npm package name.