@steadybrew/postgresjs 0.0.0-stage → 4.0.0-rc1

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 CHANGED
@@ -1,3 +1,1420 @@
1
- # Temporary Holding Version
1
+ # Postgres.js by Steady Brew
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ This is an independently maintained fork of [Postgres.js](https://github.com/porsager/postgres), the PostgreSQL client for Node.js and Deno created by [Rasmus Porsager](https://github.com/porsager). [Steady Brew](https://github.com/steadybrew) aims to fix bugs and improve the library through community contributions, regression tests, and careful architectural review, while preserving its simple API.
4
+
5
+ ---
6
+
7
+ <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">
8
+
9
+ - [🚀 Fastest full-featured node & deno client](https://github.com/porsager/postgres-benchmarks#results)
10
+ - 🏷 ES6 Tagged Template Strings at the core
11
+ - 🏄‍♀️ Simple surface API
12
+ - 🖊️ Dynamic query support
13
+
14
+ <br>
15
+
16
+ ## Compatibility policy
17
+
18
+ The next release raises the npm package's minimum Node.js version to **24**. This is a breaking compatibility change for applications using older Node versions. The minimum stays at Node 24 until an explicit policy change; newly maintained Node majors are tested before compatibility is claimed.
19
+
20
+ | Runtime | PostgreSQL target | CI jobs | Validation status |
21
+ | --- | --- | ---: | --- |
22
+ | Node 24, ESM and CJS | 15, 16, 17, 18 | 4 | Locally validated |
23
+ | Node 26, ESM and CJS | 15, 16, 17, 18 | 4 | Configured; local execution pending |
24
+ | Deno 1.46.3 | 17 | 1 | Validated legacy runtime; no Deno 2 claim |
25
+ | Cloudflare workerd, Wrangler 4.123.0 | 17 | 1 | Configured; startup/array, timer, `sql.end()` and TLS gates; #1202 "Stream was cancelled" still open |
26
+ | Bun export | No dedicated target | 0 | Retained as best effort; unverified, without dedicated CI |
27
+
28
+ The core Node matrix has eight jobs. Deno and Cloudflare run independently, so they do not multiply that matrix. PostgreSQL versions below 15 are outside the next release's supported matrix.
29
+
30
+ Supabase PostgreSQL and Supabase Edge are separate environments. Deno 1.46.3 coverage does not imply Deno 2 or hosted Supabase Edge certification.
31
+
32
+ ## Getting started
33
+
34
+ <br>
35
+ <img height="220" width="458" alt="Good UX with Postgres.js" src="https://raw.githubusercontent.com/porsager/postgres/master/demo.gif">
36
+ <br>
37
+
38
+ ### Installation
39
+ ```bash
40
+ $ npm install @steadybrew/postgresjs
41
+ ```
42
+
43
+ ### Usage
44
+ Create your `sql` database instance
45
+ ```js
46
+ // db.js
47
+ import postgres from '@steadybrew/postgresjs'
48
+
49
+ const sql = postgres({ /* options */ }) // will use psql environment variables
50
+
51
+ export default sql
52
+ ```
53
+
54
+ Simply import for use elsewhere
55
+ ```js
56
+ // users.js
57
+ import sql from './db.js'
58
+
59
+ async function getUsersOver(age) {
60
+ const users = await sql`
61
+ select
62
+ name,
63
+ age
64
+ from users
65
+ where age > ${ age }
66
+ `
67
+ // users = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...]
68
+ return users
69
+ }
70
+
71
+
72
+ async function insertUser({ name, age }) {
73
+ const users = await sql`
74
+ insert into users
75
+ (name, age)
76
+ values
77
+ (${ name }, ${ age })
78
+ returning name, age
79
+ `
80
+ // users = Result [{ name: "Murray", age: 68 }]
81
+ return users
82
+ }
83
+ ```
84
+
85
+ #### ESM dynamic imports
86
+
87
+ The library can be used with ESM dynamic imports as well as shown here.
88
+
89
+ ```js
90
+ const { default: postgres } = await import('@steadybrew/postgresjs')
91
+ ```
92
+
93
+ ## Table of Contents
94
+
95
+ * [Connection](#connection)
96
+ * [Queries](#queries)
97
+ * [Building queries](#building-queries)
98
+ * [Advanced query methods](#advanced-query-methods)
99
+ * [Transactions](#transactions)
100
+ * [Data Transformation](#data-transformation)
101
+ * [Listen & notify](#listen--notify)
102
+ * [Realtime subscribe](#realtime-subscribe)
103
+ * [Numbers, bigint, numeric](#numbers-bigint-numeric)
104
+ * [Result Array](#result-array)
105
+ * [Connection details](#connection-details)
106
+ * [Custom Types](#custom-types)
107
+ * [Teardown / Cleanup](#teardown--cleanup)
108
+ * [Error handling](#error-handling)
109
+ * [TypeScript support](#typescript-support)
110
+ * [Reserving connections](#reserving-connections)
111
+ * [Changelog](./CHANGELOG.md)
112
+
113
+
114
+ ## Connection
115
+
116
+ ### `postgres([url], [options])`
117
+
118
+ 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.
119
+
120
+ ```js
121
+ const sql = postgres('postgres://username:password@host:port/database', {
122
+ host : '', // Postgres ip address[s] or domain name[s]
123
+ port : 5432, // Postgres server port[s]
124
+ database : '', // Name of database to connect to
125
+ username : '', // Username of database user
126
+ password : '', // Password of database user
127
+ ...and more
128
+ })
129
+ ```
130
+
131
+ More options can be found in the [Connection details section](#connection-details).
132
+
133
+ ## Queries
134
+
135
+ ### ```await sql`...` -> Result[]```
136
+
137
+ 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:
138
+
139
+ 1. **Enforcing** safe query generation
140
+ 2. Giving the ` sql`` ` function powerful [utility](#dynamic-inserts) and [query building](#building-queries) features.
141
+
142
+ 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.
143
+
144
+ All queries will return a `Result` array, with objects mapping column names to each row.
145
+
146
+ ```js
147
+ const xs = await sql`
148
+ insert into users (
149
+ name, age
150
+ ) values (
151
+ 'Murray', 68
152
+ )
153
+
154
+ returning *
155
+ `
156
+
157
+ // xs = [{ user_id: 1, name: 'Murray', age: 68 }]
158
+ ```
159
+
160
+ > Please note that queries are first executed when `awaited` – or instantly by using [`.execute()`](#execute).
161
+
162
+ ### Query parameters
163
+
164
+ 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.
165
+
166
+ ```js
167
+ const name = 'Mur'
168
+ , age = 60
169
+
170
+ const users = await sql`
171
+ select
172
+ name,
173
+ age
174
+ from users
175
+ where
176
+ name like ${ name + '%' }
177
+ and age > ${ age }
178
+ `
179
+ // users = [{ name: 'Murray', age: 68 }]
180
+ ```
181
+
182
+ > 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.
183
+
184
+ ### Dynamic column selection
185
+
186
+ ```js
187
+ const columns = ['name', 'age']
188
+
189
+ await sql`
190
+ select
191
+ ${ sql(columns) }
192
+ from users
193
+ `
194
+
195
+ // Which results in:
196
+ select "name", "age" from users
197
+ ```
198
+
199
+ ### Dynamic inserts
200
+
201
+ ```js
202
+ const user = {
203
+ name: 'Murray',
204
+ age: 68
205
+ }
206
+
207
+ await sql`
208
+ insert into users ${
209
+ sql(user, 'name', 'age')
210
+ }
211
+ `
212
+
213
+ // Which results in:
214
+ insert into users ("name", "age") values ($1, $2)
215
+
216
+ // The columns can also be given with an array
217
+ const columns = ['name', 'age']
218
+
219
+ await sql`
220
+ insert into users ${
221
+ sql(user, columns)
222
+ }
223
+ `
224
+ ```
225
+
226
+ **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.
227
+
228
+ #### Multiple inserts in one query
229
+ 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()`.
230
+
231
+ ```js
232
+ const users = [{
233
+ name: 'Murray',
234
+ age: 68,
235
+ garbage: 'ignore'
236
+ },
237
+ {
238
+ name: 'Walter',
239
+ age: 80
240
+ }]
241
+
242
+ await sql`insert into users ${ sql(users, 'name', 'age') }`
243
+
244
+ // Is translated to:
245
+ insert into users ("name", "age") values ($1, $2), ($3, $4)
246
+
247
+ // Here you can also omit column names which will use object keys as columns
248
+ await sql`insert into users ${ sql(users) }`
249
+
250
+ // Which results in:
251
+ insert into users ("name", "age") values ($1, $2), ($3, $4)
252
+ ```
253
+
254
+ ### Dynamic columns in updates
255
+ This is also useful for update queries
256
+ ```js
257
+ const user = {
258
+ id: 1,
259
+ name: 'Murray',
260
+ age: 68
261
+ }
262
+
263
+ await sql`
264
+ update users set ${
265
+ sql(user, 'name', 'age')
266
+ }
267
+ where user_id = ${ user.id }
268
+ `
269
+
270
+ // Which results in:
271
+ update users set "name" = $1, "age" = $2 where user_id = $3
272
+
273
+ // The columns can also be given with an array
274
+ const columns = ['name', 'age']
275
+
276
+ await sql`
277
+ update users set ${
278
+ sql(user, columns)
279
+ }
280
+ where user_id = ${ user.id }
281
+ `
282
+ ```
283
+
284
+ ### Multiple updates in one query
285
+ 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.
286
+ ```js
287
+ const users = [
288
+ [1, 'John', 34],
289
+ [2, 'Jane', 27],
290
+ ]
291
+
292
+ await sql`
293
+ update users set name = update_data.name, age = (update_data.age)::int
294
+ from (values ${sql(users)}) as update_data (id, name, age)
295
+ where users.id = (update_data.id)::int
296
+ returning users.id, users.name, users.age
297
+ `
298
+ ```
299
+
300
+ ### Dynamic values and `where in`
301
+ Value lists can also be created dynamically, making `where in` queries simple too.
302
+ ```js
303
+ const users = await sql`
304
+ select
305
+ *
306
+ from users
307
+ where age in ${ sql([68, 75, 23]) }
308
+ `
309
+ ```
310
+
311
+ or
312
+ ```js
313
+ const [{ a, b, c }] = await sql`
314
+ select
315
+ *
316
+ from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)
317
+ `
318
+ ```
319
+
320
+ ## Building queries
321
+
322
+ Postgres.js features a simple dynamic query builder by conditionally appending/omitting query fragments.
323
+ 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.
324
+
325
+ ### Partial queries
326
+ ```js
327
+ const olderThan = x => sql`and age > ${ x }`
328
+
329
+ const filterAge = true
330
+
331
+ await sql`
332
+ select
333
+ *
334
+ from users
335
+ where name is not null ${
336
+ filterAge
337
+ ? olderThan(50)
338
+ : sql``
339
+ }
340
+ `
341
+ // Which results in:
342
+ select * from users where name is not null
343
+ // Or
344
+ select * from users where name is not null and age > 50
345
+ ```
346
+
347
+ ### Dynamic filters
348
+ ```js
349
+ await sql`
350
+ select
351
+ *
352
+ from users ${
353
+ id
354
+ ? sql`where user_id = ${ id }`
355
+ : sql``
356
+ }
357
+ `
358
+
359
+ // Which results in:
360
+ select * from users
361
+ // Or
362
+ select * from users where user_id = $1
363
+ ```
364
+
365
+ ### Dynamic ordering
366
+
367
+ ```js
368
+ const id = 1
369
+ const order = {
370
+ username: 'asc'
371
+ created_at: 'desc'
372
+ }
373
+ await sql`
374
+ select
375
+ *
376
+ from ticket
377
+ where account = ${ id }
378
+ order by ${
379
+ Object.entries(order).flatMap(([column, order], i) =>
380
+ [i ? sql`,` : sql``, sql`${ sql(column) } ${ order === 'desc' ? sql`desc` : sql`asc` }`]
381
+ )
382
+ }
383
+ `
384
+ ```
385
+
386
+ ### SQL functions
387
+ Using keywords or calling functions dynamically is also possible by using ``` sql`` ``` fragments.
388
+ ```js
389
+ const date = null
390
+
391
+ await sql`
392
+ update users set updated_at = ${ date || sql`now()` }
393
+ `
394
+
395
+ // Which results in:
396
+ update users set updated_at = now()
397
+ ```
398
+
399
+ ### Table names
400
+ Dynamic identifiers like table names and column names is also supported like so:
401
+ ```js
402
+ const table = 'users'
403
+ , column = 'id'
404
+
405
+ await sql`
406
+ select ${ sql(column) } from ${ sql(table) }
407
+ `
408
+
409
+ // Which results in:
410
+ select "id" from "users"
411
+ ```
412
+
413
+ ### Quick primer on interpolation
414
+
415
+ Here's a quick oversight over all the ways to do interpolation in a query template string:
416
+
417
+ | Interpolation syntax | Usage | Example |
418
+ | ------------- | ------------- | ------------- |
419
+ | `${ sql`` }` | for keywords or sql fragments | ``await sql`SELECT * FROM users ${sql`order by age desc` }` `` |
420
+ | `${ sql(string) }` | for identifiers | ``await sql`SELECT * FROM ${sql('table_name')` `` |
421
+ | `${ sql([] or {}, ...) }` | for helpers | ``await sql`INSERT INTO users ${sql({ name: 'Peter'})}` `` |
422
+ | `${ 'somevalue' }` | for values | ``await sql`SELECT * FROM users WHERE age = ${42}` `` |
423
+
424
+ ## Advanced query methods
425
+
426
+ ### Cursors
427
+
428
+ #### ```await sql``.cursor([rows = 1], [fn])```
429
+
430
+ 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.
431
+
432
+ ##### callback function
433
+ ```js
434
+ await sql`
435
+ select
436
+ *
437
+ from generate_series(1,4) as x
438
+ `.cursor(async([row]) => {
439
+ // row = { x: 1 }
440
+ await http.request('https://example.com/wat', { row })
441
+ })
442
+ ```
443
+
444
+ ##### for await...of
445
+ ```js
446
+ // for await...of
447
+ const cursor = sql`select * from generate_series(1,4) as x`.cursor()
448
+
449
+ for await (const [row] of cursor) {
450
+ // row = { x: 1 }
451
+ await http.request('https://example.com/wat', { row })
452
+ }
453
+ ```
454
+
455
+ 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`:
456
+ ```js
457
+ await sql`
458
+ select
459
+ *
460
+ from generate_series(1,1000) as x
461
+ `.cursor(10, async rows => {
462
+ // rows = [{ x: 1 }, { x: 2 }, ... ]
463
+ await Promise.all(rows.map(row =>
464
+ http.request('https://example.com/wat', { row })
465
+ ))
466
+ })
467
+ ```
468
+
469
+ 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.
470
+
471
+ 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.
472
+
473
+ ```js
474
+ await sql`
475
+ select * from generate_series(1,1000) as x
476
+ `.cursor(row => {
477
+ return Math.random() > 0.9 && sql.CLOSE // or sql.END
478
+ })
479
+ ```
480
+
481
+ ### Instant iteration
482
+
483
+ #### ```await sql``.forEach(fn)```
484
+
485
+ 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.
486
+ ```js
487
+ await sql`
488
+ select created_at, name from events
489
+ `.forEach(row => {
490
+ // row = { created_at: '2019-11-22T14:22:00Z', name: 'connected' }
491
+ })
492
+
493
+ // No more rows
494
+ ```
495
+
496
+ ### Query Descriptions
497
+ #### ```await sql``.describe() -> Result[]```
498
+
499
+ 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.
500
+
501
+ 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.**
502
+
503
+ ### Rows as Array of Values
504
+ #### ```sql``.values()```
505
+
506
+ Using `.values` will return rows as an array of values for each column, instead of objects.
507
+
508
+ 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.
509
+
510
+ ### Rows as Raw Array of Buffers
511
+ #### ```sql``.raw()```
512
+
513
+ Using `.raw` will return rows as an array with `Buffer` values for each column, instead of objects.
514
+
515
+ 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.
516
+
517
+ ### Queries in Files
518
+ #### `await sql.file(path, [args], [options]) -> Result[]`
519
+
520
+ Using a file for a query is also supported with optional parameters to use if the file includes `$1, $2, etc`
521
+
522
+ ```js
523
+ const result = await sql.file('query.sql', ['Murray', 68])
524
+ ```
525
+
526
+ ### Multiple statements in one query
527
+ #### ```await sql``.simple()```
528
+
529
+ 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
530
+ ```sql``.simple()```. That will create it as a simple query.
531
+
532
+ ```js
533
+ await sql`select 1; select 2;`.simple()
534
+ ```
535
+
536
+ ### Copy to/from as Streams
537
+
538
+ 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).
539
+
540
+ #### ```await sql`copy ... from stdin`.writable() -> Writable```
541
+
542
+ ```js
543
+ import { pipeline } from 'node:stream/promises'
544
+
545
+ // Stream of users with the default tab delimitated cells and new-line delimitated rows
546
+ const userStream = Readable.from([
547
+ 'Murray\t68\n',
548
+ 'Walter\t80\n'
549
+ ])
550
+
551
+ const query = await sql`copy users (name, age) from stdin`.writable()
552
+ await pipeline(userStream, query);
553
+ ```
554
+
555
+ #### ```await sql`copy ... to stdout`.readable() -> Readable```
556
+
557
+ ##### Using Stream Pipeline
558
+ ```js
559
+ import { pipeline } from 'node:stream/promises'
560
+ import { createWriteStream } from 'node:fs'
561
+
562
+ const readableStream = await sql`copy users (name, age) to stdout`.readable()
563
+ await pipeline(readableStream, createWriteStream('output.tsv'))
564
+ // output.tsv content: `Murray\t68\nWalter\t80\n`
565
+ ```
566
+
567
+ ##### Using `for await...of`
568
+ ```js
569
+ const readableStream = await sql`
570
+ copy (
571
+ select name, age
572
+ from users
573
+ where age = 68
574
+ ) to stdout
575
+ `.readable()
576
+ for await (const chunk of readableStream) {
577
+ // chunk.toString() === `Murray\t68\n`
578
+ }
579
+ ```
580
+
581
+ > **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.
582
+
583
+ ### Canceling Queries in Progress
584
+
585
+ 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.
586
+
587
+ ```js
588
+ const query = sql`select pg_sleep 100`.execute()
589
+ setTimeout(() => query.cancel(), 100)
590
+ const result = await query
591
+ ```
592
+
593
+ ### Execute
594
+
595
+ #### ```await sql``.execute()```
596
+
597
+ 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()`
598
+
599
+ ### Unsafe raw string queries
600
+
601
+ <details>
602
+ <summary>Advanced unsafe use cases</summary>
603
+
604
+ ### `await sql.unsafe(query, [args], [options]) -> Result[]`
605
+
606
+ 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.
607
+
608
+ ```js
609
+ sql.unsafe('select ' + danger + ' from users where id = ' + dragons)
610
+ ```
611
+
612
+ 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 }`.
613
+
614
+ You can also nest `sql.unsafe` within a safe `sql` expression. This is useful if only part of your fraction has unsafe elements.
615
+
616
+ ```js
617
+ const triggerName = 'friend_created'
618
+ const triggerFnName = 'on_friend_created'
619
+ const eventType = 'insert'
620
+ const schema_name = 'app'
621
+ const table_name = 'friends'
622
+
623
+ await sql`
624
+ create or replace trigger ${sql(triggerName)}
625
+ after ${sql.unsafe(eventType)} on ${sql.unsafe(`${schema_name}.${table_name}`)}
626
+ for each row
627
+ execute function ${sql(triggerFnName)}()
628
+ `
629
+
630
+ await sql`
631
+ create role friend_service with login password ${sql.unsafe(`'${password}'`)}
632
+ `
633
+ ```
634
+
635
+ </details>
636
+
637
+ ## Transactions
638
+
639
+ #### BEGIN / COMMIT `await sql.begin([options = ''], fn) -> fn()`
640
+
641
+ 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.
642
+
643
+ `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.
644
+
645
+ ```js
646
+ const [user, account] = await sql.begin(async sql => {
647
+ const [user] = await sql`
648
+ insert into users (
649
+ name
650
+ ) values (
651
+ 'Murray'
652
+ )
653
+ returning *
654
+ `
655
+
656
+ const [account] = await sql`
657
+ insert into accounts (
658
+ user_id
659
+ ) values (
660
+ ${ user.user_id }
661
+ )
662
+ returning *
663
+ `
664
+
665
+ return [user, account]
666
+ })
667
+ ```
668
+
669
+ 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.
670
+
671
+ 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:
672
+
673
+ ```js
674
+ const result = await sql.begin(sql => [
675
+ sql`update ...`,
676
+ sql`update ...`,
677
+ sql`insert ...`
678
+ ])
679
+ ```
680
+
681
+ #### SAVEPOINT `await sql.savepoint([name], fn) -> fn()`
682
+
683
+ ```js
684
+ sql.begin('read write', async sql => {
685
+ const [user] = await sql`
686
+ insert into users (
687
+ name
688
+ ) values (
689
+ 'Murray'
690
+ )
691
+ `
692
+
693
+ const [account] = (await sql.savepoint(sql =>
694
+ sql`
695
+ insert into accounts (
696
+ user_id
697
+ ) values (
698
+ ${ user.user_id }
699
+ )
700
+ `
701
+ ).catch(err => {
702
+ // Account could not be created. ROLLBACK SAVEPOINT is called because we caught the rejection.
703
+ })) || []
704
+
705
+ return [user, account]
706
+ })
707
+ .then(([user, account]) => {
708
+ // great success - COMMIT succeeded
709
+ })
710
+ .catch(() => {
711
+ // not so good - ROLLBACK was called
712
+ })
713
+ ```
714
+
715
+
716
+ #### PREPARE TRANSACTION `await sql.prepare([name]) -> fn()`
717
+
718
+ Indicates that the transactions should be prepared using the [`PREPARE TRANSACTION [NAME]`](https://www.postgresql.org/docs/current/sql-prepare-transaction.html) statement
719
+ instead of being committed.
720
+
721
+ ```js
722
+ sql.begin('read write', async sql => {
723
+ const [user] = await sql`
724
+ insert into users (
725
+ name
726
+ ) values (
727
+ 'Murray'
728
+ )
729
+ `
730
+
731
+ await sql.prepare('tx1')
732
+ })
733
+ ```
734
+
735
+ ## Data Transformation
736
+
737
+ Postgres.js allows for transformation of the data passed to or returned from a query by using the `transform` option.
738
+
739
+ Built in transformation functions are:
740
+
741
+ * For camelCase - `postgres.camel`, `postgres.toCamel`, `postgres.fromCamel`
742
+ * For PascalCase - `postgres.pascal`, `postgres.toPascal`, `postgres.fromPascal`
743
+ * For Kebab-Case - `postgres.kebab`, `postgres.toKebab`, `postgres.fromKebab`
744
+
745
+ 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.
746
+
747
+ 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:
748
+
749
+ ```js
750
+ // Transform the column names to and from camel case
751
+ const sql = postgres({ transform: postgres.camel })
752
+
753
+ await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER, b_test TEXT)`
754
+ await sql`INSERT INTO camel_case ${ sql([{ aTest: 1, bTest: 1 }]) }`
755
+ const data = await sql`SELECT ${ sql('aTest', 'bTest') } FROM camel_case`
756
+
757
+ console.log(data) // [ { aTest: 1, bTest: '1' } ]
758
+ ```
759
+
760
+ To only perform half of the transformation (eg. only the transformation **to** or **from** camel case), use the other transformation functions:
761
+
762
+ ```js
763
+ // Transform the column names only to camel case
764
+ // (for the results that are returned from the query)
765
+ postgres({ transform: postgres.toCamel })
766
+
767
+ await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`
768
+ await sql`INSERT INTO camel_case ${ sql([{ a_test: 1 }]) }`
769
+ const data = await sql`SELECT a_test FROM camel_case`
770
+
771
+ console.log(data) // [ { aTest: 1 } ]
772
+ ```
773
+
774
+ ```js
775
+ // Transform the column names only from camel case
776
+ // (for interpolated inserts, updates, and selects)
777
+ const sql = postgres({ transform: postgres.fromCamel })
778
+
779
+ await sql`CREATE TABLE IF NOT EXISTS camel_case (a_test INTEGER)`
780
+ await sql`INSERT INTO camel_case ${ sql([{ aTest: 1 }]) }`
781
+ const data = await sql`SELECT ${ sql('aTest') } FROM camel_case`
782
+
783
+ console.log(data) // [ { a_test: 1 } ]
784
+ ```
785
+
786
+ > 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.
787
+
788
+ ### Transform `undefined` Values
789
+
790
+ By default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed
791
+
792
+ ```js
793
+ // Transform the column names to and from camel case
794
+ const sql = postgres({
795
+ transform: {
796
+ undefined: null
797
+ }
798
+ })
799
+
800
+ await sql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`
801
+ await sql`INSERT INTO transform_undefined ${ sql([{ a_test: undefined }]) }`
802
+ const data = await sql`SELECT a_test FROM transform_undefined`
803
+
804
+ console.log(data) // [ { a_test: null } ]
805
+ ```
806
+
807
+ To combine with the built in transform functions, spread the transform in the `transform` object:
808
+
809
+ ```js
810
+ // Transform the column names to and from camel case
811
+ const sql = postgres({
812
+ transform: {
813
+ ...postgres.camel,
814
+ undefined: null
815
+ }
816
+ })
817
+
818
+ await sql`CREATE TABLE IF NOT EXISTS transform_undefined (a_test INTEGER)`
819
+ await sql`INSERT INTO transform_undefined ${ sql([{ aTest: undefined }]) }`
820
+ const data = await sql`SELECT ${ sql('aTest') } FROM transform_undefined`
821
+
822
+ console.log(data) // [ { aTest: null } ]
823
+ ```
824
+
825
+ ### Custom Transform Functions
826
+
827
+ 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:
828
+
829
+ * `to`: The function to transform the outgoing query column name to, i.e `SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
830
+ * `from`: The function to transform the incoming query result column name to, see example below.
831
+
832
+ > Both parameters are optional, if not provided, the default transformation function will be used.
833
+
834
+ ```js
835
+ // Implement your own functions, look at postgres.toCamel, etc
836
+ // as a reference:
837
+ // https://github.com/porsager/postgres/blob/4241824ffd7aa94ffb482e54ca9f585d9d0a4eea/src/types.js#L310-L328
838
+ function transformColumnToDatabase() { /* ... */ }
839
+ function transformColumnFromDatabase() { /* ... */ }
840
+
841
+ const sql = postgres({
842
+ transform: {
843
+ column: {
844
+ to: transformColumnToDatabase,
845
+ from: transformColumnFromDatabase,
846
+ },
847
+ value: { /* ... */ },
848
+ row: { /* ... */ }
849
+ }
850
+ })
851
+ ```
852
+
853
+ ## Listen & notify
854
+
855
+ 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.
856
+
857
+ ### Listen `await sql.listen(channel, onnotify, [onlisten]) -> { state }`
858
+ `.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.
859
+
860
+ ```js
861
+ await sql.listen('news', payload => {
862
+ const json = JSON.parse(payload)
863
+ console.log(json.this) // logs 'is'
864
+ })
865
+ ```
866
+
867
+ The optional `onlisten` method is great to use for a very simply queue mechanism:
868
+
869
+ ```js
870
+ await sql.listen(
871
+ 'jobs',
872
+ (x) => run(JSON.parse(x)),
873
+ ( ) => sql`select unfinished_jobs()`.forEach(run)
874
+ )
875
+
876
+ function run(job) {
877
+ // And here you do the work you please
878
+ }
879
+ ```
880
+ ### Notify `await sql.notify(channel, payload) -> Result[]`
881
+ Notify can be done as usual in SQL, or by using the `sql.notify` method.
882
+ ```js
883
+ sql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))
884
+ ```
885
+
886
+ ## Realtime subscribe
887
+
888
+ Postgres.js implements the logical replication protocol of PostgreSQL to support subscription to real-time updates of `insert`, `update` and `delete` operations.
889
+
890
+ > **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.
891
+
892
+ ### Quick start
893
+
894
+ #### Create a publication (eg. in migration)
895
+ ```sql
896
+ CREATE PUBLICATION alltables FOR ALL TABLES
897
+ ```
898
+
899
+ #### Subscribe to updates
900
+ ```js
901
+ const sql = postgres({ publications: 'alltables' })
902
+
903
+ const { unsubscribe } = await sql.subscribe(
904
+ 'insert:events',
905
+ (row, { command, relation, key, old }) => {
906
+ // Callback function for each row change
907
+ // tell about new event row over eg. websockets or do something else
908
+ },
909
+ () => {
910
+ // Callback on initial connect and potential reconnects
911
+ }
912
+ )
913
+ ```
914
+
915
+ ### Subscribe pattern
916
+
917
+ You can subscribe to specific operations, tables, or even rows with primary keys.
918
+
919
+ #### `operation` `:` `schema` `.` `table` `=` `primary_key`
920
+
921
+ **`operation`** is one of ``` * | insert | update | delete ``` and defaults to `*`
922
+
923
+ **`schema`** defaults to `public`
924
+
925
+ **`table`** is a specific table name and defaults to `*`
926
+
927
+ **`primary_key`** can be used to only subscribe to specific rows
928
+
929
+ ### Examples
930
+
931
+ ```js
932
+ sql.subscribe('*', () => /* everything */ )
933
+ sql.subscribe('insert', () => /* all inserts */ )
934
+ sql.subscribe('*:users', () => /* all operations on the public.users table */ )
935
+ sql.subscribe('delete:users', () => /* all deletes on the public.users table */ )
936
+ sql.subscribe('update:users=1', () => /* all updates on the users row with a primary key = 1 */ )
937
+ ```
938
+
939
+ ## Numbers, bigint, numeric
940
+
941
+ `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`.
942
+
943
+ 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.
944
+
945
+ If you want to use `BigInt` you can add this custom type:
946
+
947
+ ```js
948
+ const sql = postgres({
949
+ types: {
950
+ bigint: postgres.BigInt
951
+ }
952
+ })
953
+ ```
954
+
955
+ 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).
956
+
957
+ ## Result Array
958
+
959
+ 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.
960
+
961
+ ### .count
962
+
963
+ 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 ...`.
964
+
965
+ ### .command
966
+
967
+ The `command` run by the query - eg. one of `SELECT`, `UPDATE`, `INSERT`, `DELETE`
968
+
969
+ ### .columns
970
+
971
+ The `columns` returned by the query useful to determine types, or map to the result values when using `.values()`
972
+
973
+ ```js
974
+ {
975
+ name : String, // Column name,
976
+ type : oid, // PostgreSQL oid column type
977
+ parser: Function // The function used by Postgres.js for parsing
978
+ }
979
+ ```
980
+
981
+ ### .statement
982
+
983
+ The `statement` contains information about the statement implicitly created by Postgres.js.
984
+
985
+ ```js
986
+ {
987
+ name : String, // The auto generated statement name
988
+ string : String, // The actual query string executed
989
+ types : [oid], // An array of oid expected as input parameters
990
+ columns : [Column] // Array of columns - same as Result.columns
991
+ }
992
+ ```
993
+
994
+ ### .state
995
+
996
+ This is the state `{ pid, secret }` of the connection that executed the query.
997
+
998
+ ## Connection details
999
+
1000
+ ### All Postgres options
1001
+
1002
+ ```js
1003
+ const sql = postgres('postgres://username:password@host:port/database', {
1004
+ host : '', // Postgres ip address[es] or domain name[s]
1005
+ port : 5432, // Postgres server port[s]
1006
+ path : '', // unix socket path (usually '/tmp')
1007
+ database : '', // Name of database to connect to
1008
+ username : '', // Username of database user
1009
+ password : '', // Password of database user
1010
+ ssl : false, // true, prefer, require, tls.connect options
1011
+ sslnegotiation : null, // direct
1012
+ max : 10, // Max number of connections
1013
+ max_lifetime : null, // Max lifetime in seconds (more info below)
1014
+ idle_timeout : 0, // Idle connection timeout in seconds
1015
+ connect_timeout : 30, // Connect timeout in seconds
1016
+ prepare : true, // Automatic creation of prepared statements
1017
+ types : [], // Array of custom types, see more below
1018
+ onnotice : fn, // Default console.log, set false to silence NOTICE
1019
+ onparameter : fn, // (key, value) when server param change
1020
+ debug : fn, // Is called with (connection, query, params, types)
1021
+ socket : fn, // fn returning custom socket to use
1022
+ transform : {
1023
+ undefined : undefined, // Transforms undefined values (eg. to null)
1024
+ column : fn, // Transforms incoming column names
1025
+ value : fn, // Transforms incoming row values
1026
+ row : fn // Transforms entire rows
1027
+ },
1028
+ connection : {
1029
+ application_name : 'postgres.js', // Default application_name
1030
+ ... // Other connection parameters, see https://www.postgresql.org/docs/current/runtime-config-client.html
1031
+ },
1032
+ target_session_attrs : null, // Use 'read-write' with multiple hosts to
1033
+ // ensure only connecting to primary
1034
+ fetch_types : true, // Automatically fetches types on connect
1035
+ // on initial connection.
1036
+ })
1037
+ ```
1038
+
1039
+ 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.
1040
+
1041
+ ### Dynamic passwords
1042
+
1043
+ 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.
1044
+
1045
+ ```js
1046
+ const sql = postgres(url, {
1047
+ // Other connection config
1048
+ ...
1049
+ // Password function for the database user
1050
+ password : async () => await signer.getAuthToken(),
1051
+ })
1052
+ ```
1053
+
1054
+ ### SSL
1055
+
1056
+ 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`):
1057
+
1058
+ ```js
1059
+ const sql =
1060
+ process.env.NODE_ENV === 'production'
1061
+ ? // "Unless you're using a Private or Shield Heroku Postgres database, Heroku Postgres does not currently support verifiable certificates"
1062
+ // https://help.heroku.com/3DELT3RK/why-can-t-my-third-party-utility-connect-to-heroku-postgres-with-ssl
1063
+ postgres({ ssl: { rejectUnauthorized: false } })
1064
+ : postgres()
1065
+ ```
1066
+
1067
+ 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).
1068
+
1069
+
1070
+ ### Multi-host connections - High Availability (HA)
1071
+
1072
+ 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).
1073
+
1074
+ 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.
1075
+
1076
+ If you specify `target_session_attrs: 'primary'` or `PGTARGETSESSIONATTRS=primary` Postgres.js will only connect to the primary host, allowing for zero downtime failovers.
1077
+
1078
+ ### The Connection Pool
1079
+
1080
+ 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.
1081
+
1082
+ > No connection will be made until a query is made.
1083
+
1084
+ For example:
1085
+
1086
+ ```js
1087
+ const sql = postgres() // no connections are opened
1088
+
1089
+ await sql`...` // one connection is now opened
1090
+ await sql`...` // previous opened connection is reused
1091
+
1092
+ // two connections are opened now
1093
+ await Promise.all([
1094
+ sql`...`,
1095
+ sql`...`
1096
+ ])
1097
+ ```
1098
+
1099
+ > 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 })`.
1100
+
1101
+ 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.
1102
+
1103
+ 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.
1104
+
1105
+ 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.
1106
+
1107
+ 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.
1108
+
1109
+ ### Connection timeout
1110
+
1111
+ By default, connections will not close until `.end()` is called. However, it may be useful to have them close automatically when:
1112
+
1113
+ - re-instantiating multiple ` sql`` ` instances
1114
+ - using Postgres.js in a Serverless environment (Lambda, etc.)
1115
+ - using Postgres.js with a database service that automatically closes connections after some time (see [`ECONNRESET` issue](https://github.com/porsager/postgres/issues/179))
1116
+
1117
+ 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.
1118
+
1119
+ For example, to close a connection that has either been idle for 20 seconds or existed for more than 30 minutes:
1120
+
1121
+ ```js
1122
+ const sql = postgres({
1123
+ idle_timeout: 20,
1124
+ max_lifetime: 60 * 30
1125
+ })
1126
+ ```
1127
+
1128
+ ### Cloudflare Workers support
1129
+
1130
+ 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.
1131
+
1132
+ 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:
1133
+
1134
+ ```ts
1135
+ // Requires Postgres.js 3.4.0 or later
1136
+ import postgres from '@steadybrew/postgresjs'
1137
+
1138
+ interface Env {
1139
+ HYPERDRIVE: Hyperdrive;
1140
+ }
1141
+
1142
+ export default async fetch(req: Request, env: Env, ctx: ExecutionContext) {
1143
+ // The Postgres.js library accepts a connection string directly
1144
+ const sql = postgres(env.HYPERDRIVE.connectionString)
1145
+ const results = await sql`SELECT * FROM users LIMIT 10`
1146
+ return Response.json(results)
1147
+ }
1148
+ ```
1149
+
1150
+ In `wrangler.toml` you will need to enable the `nodejs_compat` compatibility flag to allow Postgres.js to operate in the Workers environment:
1151
+
1152
+ ```toml
1153
+ compatibility_flags = ["nodejs_compat"]
1154
+ ```
1155
+
1156
+ ### Auto fetching of array types
1157
+
1158
+ Postgres.js automatically fetches array-type information when each connection initializes.
1159
+
1160
+ If you have revoked access to `pg_catalog` this feature will no longer work and will need to be disabled.
1161
+
1162
+ You can disable catalog discovery by setting `fetch_types` to `false`. A fixed set of 22 common built-in array types still supports parsing and serialization, including text, numeric, boolean, JSON, date, UUID, and byte arrays. This changes earlier `fetch_types: false` behavior, which returned built-in arrays as raw strings. Other built-in, user-defined, and extension arrays still require discovery or explicit type handlers.
1163
+
1164
+ SQL `NULL` elements are returned as JavaScript `null`; a quoted text element `"NULL"` remains a string. Explicit array parsers and serializers are preserved independently. Catalog discovery with `fetch_types: true` remains per connection; this change does not cache catalog metadata.
1165
+
1166
+ Column, value, and row transforms apply to application query results; initialization metadata retains its original shape.
1167
+
1168
+ ### Environmental variables
1169
+
1170
+ 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:
1171
+
1172
+ ```js
1173
+ const sql = postgres()
1174
+ ```
1175
+
1176
+ | Option | Environment Variables |
1177
+ | ------------------ | ------------------------ |
1178
+ | `host` | `PGHOST` |
1179
+ | `port` | `PGPORT` |
1180
+ | `database` | `PGDATABASE` |
1181
+ | `username` | `PGUSERNAME` or `PGUSER` |
1182
+ | `password` | `PGPASSWORD` |
1183
+ | `application_name` | `PGAPPNAME` |
1184
+ | `idle_timeout` | `PGIDLE_TIMEOUT` |
1185
+ | `connect_timeout` | `PGCONNECT_TIMEOUT` |
1186
+
1187
+ ### Prepared statements
1188
+
1189
+ 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).
1190
+
1191
+ **update**: [since 1.21.0](https://www.pgbouncer.org/2023/10/pgbouncer-1-21-0)
1192
+ PGBouncer supports protocol-level named prepared statements when [configured
1193
+ properly](https://www.pgbouncer.org/config.html#max_prepared_statements)
1194
+
1195
+ ## Custom Types
1196
+
1197
+ 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.)_
1198
+
1199
+ Adding Query helpers is the cleanest approach which can be done like this:
1200
+
1201
+ ```js
1202
+ const sql = postgres({
1203
+ types: {
1204
+ rect: {
1205
+ // The pg_types oid to pass to the db along with the serialized value.
1206
+ to : 1337,
1207
+
1208
+ // An array of pg_types oids to handle when parsing values coming from the db.
1209
+ from : [1337],
1210
+
1211
+ //Function that transform values before sending them to the db.
1212
+ serialize : ({ x, y, width, height }) => [x, y, width, height],
1213
+
1214
+ // Function that transforms values coming from the db.
1215
+ parse : ([x, y, width, height]) => { x, y, width, height }
1216
+ }
1217
+ }
1218
+ })
1219
+
1220
+ // Now you can use sql.typed.rect() as specified above
1221
+ const [custom] = await sql`
1222
+ insert into rectangles (
1223
+ name,
1224
+ rect
1225
+ ) values (
1226
+ 'wat',
1227
+ ${ sql.typed.rect({ x: 13, y: 37, width: 42, height: 80 }) }
1228
+ )
1229
+ returning *
1230
+ `
1231
+
1232
+ // custom = { name: 'wat', rect: { x: 13, y: 37, width: 42, height: 80 } }
1233
+
1234
+ ```
1235
+
1236
+ ### Custom socket
1237
+
1238
+ 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.
1239
+
1240
+ Here's a sample using [ssh2](https://github.com/mscdex/ssh2)
1241
+
1242
+ ```js
1243
+ import ssh2 from 'ssh2'
1244
+
1245
+ const sql = postgres({
1246
+ ...options,
1247
+ socket: ({ host: [host], port: [port] }) => new Promise((resolve, reject) => {
1248
+ const ssh = new ssh2.Client()
1249
+ ssh
1250
+ .on('error', reject)
1251
+ .on('ready', () =>
1252
+ ssh.forwardOut('127.0.0.1', 12345, host, port,
1253
+ (err, socket) => err ? reject(err) : resolve(socket)
1254
+ )
1255
+ )
1256
+ .connect(sshOptions)
1257
+ })
1258
+ })
1259
+ ```
1260
+
1261
+ ## Teardown / Cleanup
1262
+
1263
+ To ensure proper teardown and cleanup on server restarts use `await sql.end()` before `process.exit()`.
1264
+
1265
+ 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.
1266
+
1267
+ #### Sample shutdown using [Prexit](https://github.com/porsager/prexit)
1268
+
1269
+ ```js
1270
+ import prexit from 'prexit'
1271
+
1272
+ prexit(async () => {
1273
+ await sql.end({ timeout: 5 })
1274
+ await new Promise(r => server.close(r))
1275
+ })
1276
+ ```
1277
+
1278
+ ## Reserving connections
1279
+
1280
+ ### `await sql.reserve()`
1281
+
1282
+ 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.
1283
+
1284
+ ```ts
1285
+ const reserved = await sql.reserve()
1286
+ await reserved`select * from users`
1287
+ await reserved.release()
1288
+ ```
1289
+
1290
+ ### `reserved.release()`
1291
+
1292
+ Once you have finished with the reserved connection, call `release` to add it back to the pool.
1293
+
1294
+ ## Error handling
1295
+
1296
+ 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.
1297
+
1298
+ Query errors will contain a stored error with the origin of the query to aid in tracing errors.
1299
+
1300
+ 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.
1301
+
1302
+ There are also the following errors specifically for this library.
1303
+
1304
+ ##### UNSAFE_TRANSACTION
1305
+ > Only use sql.begin or max: 1
1306
+
1307
+ 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`).
1308
+
1309
+ ##### UNDEFINED_VALUE
1310
+ > Undefined values are not allowed
1311
+
1312
+ 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.
1313
+
1314
+ ##### MESSAGE_NOT_SUPPORTED
1315
+ > X (X) is not supported
1316
+
1317
+ 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.
1318
+
1319
+ ##### MAX_PARAMETERS_EXCEEDED
1320
+ > Max number of parameters (65534) exceeded
1321
+
1322
+ 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.
1323
+
1324
+ ##### SASL_SIGNATURE_MISMATCH
1325
+ > Message type X not supported
1326
+
1327
+ 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.
1328
+
1329
+ ##### NOT_TAGGED_CALL
1330
+ > Query not called as a tagged template literal
1331
+
1332
+ 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.
1333
+
1334
+ ##### AUTH_TYPE_NOT_IMPLEMENTED
1335
+ > Auth type X not implemented
1336
+
1337
+ Postgres supports many different authentication types. This one is not supported.
1338
+
1339
+ ##### CONNECTION_CLOSED
1340
+ > write CONNECTION_CLOSED host:port
1341
+
1342
+ 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.
1343
+
1344
+ ##### CONNECTION_ENDED
1345
+ > write CONNECTION_ENDED host:port
1346
+
1347
+ This error is thrown if the user has called [`sql.end()`](#teardown--cleanup) and performed a query afterward.
1348
+
1349
+ A reserved `sql` used after `release()`, or a transaction `sql` used after `begin()` has committed, rolled back or prepared, also rejects with `CONNECTION_ENDED`.
1350
+
1351
+ ##### CONNECTION_DESTROYED
1352
+ > write CONNECTION_DESTROYED host:port
1353
+
1354
+ This error is thrown for any queries that were pending when the timeout to [`sql.end({ timeout: X })`](#teardown--cleanup) was reached.
1355
+
1356
+ ##### CONNECT_TIMEOUT
1357
+ > write CONNECT_TIMEOUT host:port
1358
+
1359
+ The timeout applies to each connection attempt and covers socket creation, TCP and TLS negotiation, authentication, session checks and type discovery. When an attempt times out, the next host is tried. The acquisition as a whole is bounded by `connect_timeout` multiplied by the number of hosts, measured from the first attempt; time spent waiting on an inherited reconnect delay is not counted. Set it with `connect_timeout` or `PGCONNECT_TIMEOUT` (30 seconds by default); zero disables both limits.
1360
+
1361
+ If the only attempt times out, the error is `CONNECT_TIMEOUT`. Once earlier attempts have failed, exhausting the overall budget reports the earlier error, such as the last server error or `CONNECTION_CLOSED`. With a single host, a socket or socket factory error rejects the query once; with several hosts, errors keep cycling through the hosts with backoff until the budget is spent. Queries already sent to the server are not replayed.
1362
+
1363
+ ##### COPY_IN_PROGRESS
1364
+ > You cannot execute queries during copy
1365
+
1366
+ This error is thrown if trying to run a query during a copy operation (writable / readable).
1367
+
1368
+ ## TypeScript support
1369
+
1370
+ `postgres` has TypeScript support. You can pass a row list type for your queries in this way:
1371
+ ```ts
1372
+ interface User {
1373
+ id: number
1374
+ name: string
1375
+ }
1376
+
1377
+ const users = await sql<User[]>`SELECT * FROM users`
1378
+ users[0].id // ok => number
1379
+ users[1].name // ok => string
1380
+ users[0].invalid // fails: `invalid` does not exists on `User`
1381
+ ```
1382
+
1383
+ However, be sure to check the array length to avoid accessing properties of `undefined` rows:
1384
+ ```ts
1385
+ const users = await sql<User[]>`SELECT * FROM users WHERE id = ${id}`
1386
+ if (!users.length)
1387
+ throw new Error('Not found')
1388
+ return users[0]
1389
+ ```
1390
+
1391
+ You can also prefer destructuring when you only care about a fixed number of rows.
1392
+ In this case, we recommend you to prefer using tuples to handle `undefined` properly:
1393
+ ```ts
1394
+ const [user]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}`
1395
+ if (!user) // => User | undefined
1396
+ throw new Error('Not found')
1397
+ return user // => User
1398
+
1399
+ // NOTE:
1400
+ const [first, second]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}` // fails: `second` does not exist on `[User?]`
1401
+ const [first, second] = await sql<[User?]>`SELECT * FROM users WHERE id = ${id}` // don't fail : `second: User | undefined`
1402
+ ```
1403
+
1404
+ 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.
1405
+
1406
+ ## Migration tools
1407
+
1408
+ 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:
1409
+
1410
+ - https://github.com/porsager/postgres-shift
1411
+ - https://github.com/lukeed/ley
1412
+ - https://github.com/JAForbes/pgmg
1413
+
1414
+ ## Thank you
1415
+
1416
+ 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.
1417
+
1418
+ Thanks to [@ACXgit](https://twitter.com/andreacoiutti) for initial tests and dogfooding.
1419
+
1420
+ Also thanks to [Ryan Dahl](https://github.com/ry) for letting me have the `postgres` npm package name.