From c084a1cf0ffd5aeaf9388ef0c84d0da28fca24b5 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Thu, 2 Nov 2023 08:28:13 +0100 Subject: [PATCH 01/41] Ensure reserved connections are initialized properly - fixes #718 --- src/connection.js | 11 +++++++---- tests/index.js | 11 +++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/connection.js b/src/connection.js index a6825105..7d97a4b7 100644 --- a/src/connection.js +++ b/src/connection.js @@ -109,7 +109,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose queue: queues.closed, idleTimer, connect(query) { - initial = query + initial = query || true reconnect() }, terminate, @@ -533,11 +533,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose return terminate() } - if (needsTypes) + if (needsTypes) { + initial === true && (initial = null) return fetchArrayTypes() + } - execute(initial) - options.shared.retries = retries = initial = 0 + initial !== true && execute(initial) + options.shared.retries = retries = 0 + initial = null return } diff --git a/tests/index.js b/tests/index.js index e47cb534..cd08370a 100644 --- a/tests/index.js +++ b/tests/index.js @@ -2543,3 +2543,14 @@ t('reserve connection', async() => { xs.map(x => x.x).join('') ] }) + +t('arrays in reserved connection', async() => { + const reserved = await sql.reserve() + const [{ x }] = await reserved`select array[1, 2, 3] as x` + reserved.release() + + return [ + '123', + x.join('') + ] +}) From 6121a0afc100f968b50112b225a8e55660687160 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Thu, 2 Nov 2023 08:44:19 +0100 Subject: [PATCH 02/41] build --- cf/src/connection.js | 11 +++++++---- cjs/src/connection.js | 11 +++++++---- cjs/tests/index.js | 11 +++++++++++ deno/src/connection.js | 11 +++++++---- deno/tests/index.js | 11 +++++++++++ 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/cf/src/connection.js b/cf/src/connection.js index f06a5f8b..ab977ca8 100644 --- a/cf/src/connection.js +++ b/cf/src/connection.js @@ -111,7 +111,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose queue: queues.closed, idleTimer, connect(query) { - initial = query + initial = query || true reconnect() }, terminate, @@ -535,11 +535,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose return terminate() } - if (needsTypes) + if (needsTypes) { + initial === true && (initial = null) return fetchArrayTypes() + } - execute(initial) - options.shared.retries = retries = initial = 0 + initial !== true && execute(initial) + options.shared.retries = retries = 0 + initial = null return } diff --git a/cjs/src/connection.js b/cjs/src/connection.js index b295958a..425e91cd 100644 --- a/cjs/src/connection.js +++ b/cjs/src/connection.js @@ -109,7 +109,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose queue: queues.closed, idleTimer, connect(query) { - initial = query + initial = query || true reconnect() }, terminate, @@ -533,11 +533,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose return terminate() } - if (needsTypes) + if (needsTypes) { + initial === true && (initial = null) return fetchArrayTypes() + } - execute(initial) - options.shared.retries = retries = initial = 0 + initial !== true && execute(initial) + options.shared.retries = retries = 0 + initial = null return } diff --git a/cjs/tests/index.js b/cjs/tests/index.js index ef70c4ab..5aa0ae15 100644 --- a/cjs/tests/index.js +++ b/cjs/tests/index.js @@ -2543,3 +2543,14 @@ t('reserve connection', async() => { xs.map(x => x.x).join('') ] }) + +t('arrays in reserved connection', async() => { + const reserved = await sql.reserve() + const [{ x }] = await reserved`select array[1, 2, 3] as x` + reserved.release() + + return [ + '123', + x.join('') + ] +}) diff --git a/deno/src/connection.js b/deno/src/connection.js index bc4d231c..334b9722 100644 --- a/deno/src/connection.js +++ b/deno/src/connection.js @@ -112,7 +112,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose queue: queues.closed, idleTimer, connect(query) { - initial = query + initial = query || true reconnect() }, terminate, @@ -536,11 +536,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose return terminate() } - if (needsTypes) + if (needsTypes) { + initial === true && (initial = null) return fetchArrayTypes() + } - execute(initial) - options.shared.retries = retries = initial = 0 + initial !== true && execute(initial) + options.shared.retries = retries = 0 + initial = null return } diff --git a/deno/tests/index.js b/deno/tests/index.js index dc78c2c8..90d1feeb 100644 --- a/deno/tests/index.js +++ b/deno/tests/index.js @@ -2546,4 +2546,15 @@ t('reserve connection', async() => { ] }) +t('arrays in reserved connection', async() => { + const reserved = await sql.reserve() + const [{ x }] = await reserved`select array[1, 2, 3] as x` + reserved.release() + + return [ + '123', + x.join('') + ] +}) + ;window.addEventListener("unload", () => Deno.exit(process.exitCode)) \ No newline at end of file From 61c4d5b1d840ed1e3e0f8e84556544a33ee04149 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Thu, 2 Nov 2023 08:45:01 +0100 Subject: [PATCH 03/41] 3.4.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 34802d6c..ea500a80 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postgres", - "version": "3.4.2", + "version": "3.4.3", "description": "Fastest full featured PostgreSQL client for Node.js", "type": "module", "module": "src/index.js", From 6f20a4820c683b33e7670b606d8daf5670f4b973 Mon Sep 17 00:00:00 2001 From: Wack <135170502+wackfx@users.noreply.github.com> Date: Sun, 26 Nov 2023 10:01:37 +0100 Subject: [PATCH 04/41] Patch: Connection stuck after a while (#738) * Update connection.js --- src/connection.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/connection.js b/src/connection.js index 7d97a4b7..a5694183 100644 --- a/src/connection.js +++ b/src/connection.js @@ -429,10 +429,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose lifeTimer.cancel() connectTimer.cancel() - if (socket.encrypted) { - socket.removeAllListeners() - socket = null - } + socket.removeAllListeners() + socket = null if (initial) return reconnect() From 3623021f78b2c92d30f86ac96038941c51d93527 Mon Sep 17 00:00:00 2001 From: James Ross Date: Tue, 30 Jan 2024 20:15:35 +0000 Subject: [PATCH 05/41] docs: update Cloudflare Workers instructions --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index da002cca..c2202bcc 100644 --- a/README.md +++ b/README.md @@ -1103,10 +1103,10 @@ export default async fetch(req: Request, env: Env, ctx: ExecutionContext) { } ``` -In `wrangler.toml` you will need to enable `node_compat` to allow Postgres.js to operate in the Workers environment: +In `wrangler.toml` you will need to enable the `nodejs_compat` compatibility flag to allow Postgres.js to operate in the Workers environment: ```toml -node_compat = true # required for database drivers to function +compatibility_flags = ["nodejs_compat"] ``` ### Auto fetching of array types From cd02af83bdc6fd6d9801d793825f0bb0af36f074 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Sat, 17 Feb 2024 11:29:28 +0100 Subject: [PATCH 06/41] update to v4 actions --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6da2dbd1..aec631bf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,7 +25,7 @@ jobs: --health-timeout 5s --health-retries 5 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - run: | date sudo apt purge postgresql-14 @@ -48,7 +48,7 @@ jobs: - uses: denoland/setup-deno@v1 with: deno-version: v1.x - - uses: actions/setup-node@v3 + - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node }} - run: npm test From 3e3d5e894a86b03b5e6edac9f52bd7ca4abd2ce5 Mon Sep 17 00:00:00 2001 From: "louis.tian" Date: Wed, 24 Jan 2024 11:02:11 +1100 Subject: [PATCH 07/41] add handler --- src/subscribe.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/subscribe.js b/src/subscribe.js index 7a70842e..decb42c6 100644 --- a/src/subscribe.js +++ b/src/subscribe.js @@ -47,7 +47,7 @@ export default function Subscribe(postgres, options) { return subscribe - async function subscribe(event, fn, onsubscribe = noop) { + async function subscribe(event, fn, onsubscribe = noop, onerror = noop) { event = parseEvent(event) if (!connection) @@ -66,6 +66,7 @@ export default function Subscribe(postgres, options) { return connection.then(x => { connected(x) onsubscribe() + stream && stream.on('error', onerror) return { unsubscribe, state, sql } }) } From 5404fbd6bcd145e604bc309b2e1a7cb49ceaed25 Mon Sep 17 00:00:00 2001 From: "louis.tian" Date: Thu, 25 Jan 2024 09:25:10 +1100 Subject: [PATCH 08/41] update type definition --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 78d559ef..4e7b5653 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -690,7 +690,7 @@ declare namespace postgres { listen(channel: string, onnotify: (value: string) => void, onlisten?: (() => void) | undefined): ListenRequest; notify(channel: string, payload: string): PendingRequest; - subscribe(event: string, cb: (row: Row | null, info: ReplicationEvent) => void, onsubscribe?: (() => void) | undefined): Promise; + subscribe(event: string, cb: (row: Row | null, info: ReplicationEvent) => void, onsubscribe?: (() => void), onerror?: (() => any)): Promise; largeObject(oid?: number | undefined, /** @default 0x00020000 | 0x00040000 */ mode?: number | undefined): Promise; From a5cd8113cad622fafc1f6cfadccc11759ef36136 Mon Sep 17 00:00:00 2001 From: "louis.tian" Date: Mon, 29 Jan 2024 15:36:50 +1100 Subject: [PATCH 09/41] update lsn on Primary Keep Alive Message --- src/subscribe.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/subscribe.js b/src/subscribe.js index decb42c6..3db2f43b 100644 --- a/src/subscribe.js +++ b/src/subscribe.js @@ -110,8 +110,10 @@ export default function Subscribe(postgres, options) { function data(x) { if (x[0] === 0x77) parse(x.subarray(25), state, sql.options.parsers, handle, options.transform) - else if (x[0] === 0x6b && x[17]) + else if (x[0] === 0x6b && x[17]) { + state.lsn = x.subarray(1, 9) pong() + } } function handle(a, b) { From 9b6fc89d8705bf430bfc3e7f900450293fcdb8bb Mon Sep 17 00:00:00 2001 From: CDT Date: Tue, 13 Feb 2024 12:21:36 +0800 Subject: [PATCH 10/41] Update README.md fixed typo --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c2202bcc..e6bf0ce8 100644 --- a/README.md +++ b/README.md @@ -270,7 +270,7 @@ const users = [ ] await sql` - update users set name = update_data.name, (age = update_data.age)::int + update users set name = update_data.name, age = (update_data.age)::int from (values ${sql(users)}) as update_data (id, name, age) where users.id = (update_data.id)::int returning users.id, users.name, users.age @@ -290,7 +290,7 @@ const users = await sql` or ```js -const [{ a, b, c }] => await sql` +const [{ a, b, c }] = await sql` select * from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c) From 4f648b3cfa5cb4bff7f0d0234929690f775e1801 Mon Sep 17 00:00:00 2001 From: Inklingboiii <69518450+Inklingboiii@users.noreply.github.com> Date: Mon, 4 Dec 2023 22:28:06 +0100 Subject: [PATCH 11/41] Fixed Typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e6bf0ce8..1b93c156 100644 --- a/README.md +++ b/README.md @@ -917,7 +917,7 @@ The `Result` Array returned from queries is a custom array allowing for easy des ### .count -The `count` property is the number of affected rows returned by the database. This is usefull for insert, update and delete operations to know the number of rows since .length will be 0 in these cases if not using `RETURNING ...`. +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 ...`. ### .command From 2b85ea7fb8b50f7c69232bd8074aa11c8cbe9d3a Mon Sep 17 00:00:00 2001 From: Ian Bytchek Date: Sat, 6 Jan 2024 10:28:31 +0000 Subject: [PATCH 12/41] Add `simple()` type definition Fixes #714. --- types/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/index.d.ts b/types/index.d.ts index 4e7b5653..8989ff47 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -599,6 +599,7 @@ declare namespace postgres { type RowList = T & Iterable> & ResultQueryMeta; interface PendingQueryModifiers { + simple(): this; readable(): Promise; writable(): Promise; From 3e28f3a596ccd1d309ac52972d6ef87a92bab26a Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Wed, 20 Mar 2024 10:32:00 +0100 Subject: [PATCH 13/41] Ensure retryRoutines are only used for prepared statements - fixes #830 --- src/connection.js | 2 +- tests/index.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/connection.js b/src/connection.js index a5694183..7f8ac5ea 100644 --- a/src/connection.js +++ b/src/connection.js @@ -788,7 +788,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose const error = Errors.postgres(parseError(x)) query && query.retried ? errored(query.retried) - : query && retryRoutines.has(error.routine) + : query && query.prepare && retryRoutines.has(error.routine) ? retry(query, error) : errored(error) } diff --git a/tests/index.js b/tests/index.js index cd08370a..13734239 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1789,6 +1789,21 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => { ] }) +t('Properly throws routing error on not prepared statements', async() => { + await sql`create table x (x text[])` + const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e) + + return ['transformAssignedExpr', routine, await sql`drop table x`] +}) + +t('Properly throws routing error on not prepared statements in transaction', async() => { + const { routine } = await sql.begin(sql => [ + sql`create table x (x text[])`, + sql`insert into x(x) values (('a', 'b'))`, + ]).catch(e => e) + + return ['transformAssignedExpr', routine] +}) t('Catches connection config errors', async() => { const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' }) From 6f20f3fe4510e25150e05306596f46e2688dc7f9 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Wed, 20 Mar 2024 19:28:27 +0100 Subject: [PATCH 14/41] build --- cjs/src/connection.js | 8 +++----- cjs/src/subscribe.js | 7 +++++-- cjs/tests/index.js | 15 +++++++++++++++ deno/README.md | 10 +++++----- deno/src/connection.js | 8 +++----- deno/src/subscribe.js | 7 +++++-- deno/tests/index.js | 15 +++++++++++++++ deno/types/index.d.ts | 3 ++- 8 files changed, 53 insertions(+), 20 deletions(-) diff --git a/cjs/src/connection.js b/cjs/src/connection.js index 425e91cd..9180693d 100644 --- a/cjs/src/connection.js +++ b/cjs/src/connection.js @@ -429,10 +429,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose lifeTimer.cancel() connectTimer.cancel() - if (socket.encrypted) { - socket.removeAllListeners() - socket = null - } + socket.removeAllListeners() + socket = null if (initial) return reconnect() @@ -790,7 +788,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose const error = Errors.postgres(parseError(x)) query && query.retried ? errored(query.retried) - : query && retryRoutines.has(error.routine) + : query && query.prepare && retryRoutines.has(error.routine) ? retry(query, error) : errored(error) } diff --git a/cjs/src/subscribe.js b/cjs/src/subscribe.js index 34d99e9f..e450071e 100644 --- a/cjs/src/subscribe.js +++ b/cjs/src/subscribe.js @@ -47,7 +47,7 @@ module.exports = Subscribe;function Subscribe(postgres, options) { return subscribe - async function subscribe(event, fn, onsubscribe = noop) { + async function subscribe(event, fn, onsubscribe = noop, onerror = noop) { event = parseEvent(event) if (!connection) @@ -66,6 +66,7 @@ module.exports = Subscribe;function Subscribe(postgres, options) { return connection.then(x => { connected(x) onsubscribe() + stream && stream.on('error', onerror) return { unsubscribe, state, sql } }) } @@ -109,8 +110,10 @@ module.exports = Subscribe;function Subscribe(postgres, options) { function data(x) { if (x[0] === 0x77) parse(x.subarray(25), state, sql.options.parsers, handle, options.transform) - else if (x[0] === 0x6b && x[17]) + else if (x[0] === 0x6b && x[17]) { + state.lsn = x.subarray(1, 9) pong() + } } function handle(a, b) { diff --git a/cjs/tests/index.js b/cjs/tests/index.js index 5aa0ae15..437ed2f9 100644 --- a/cjs/tests/index.js +++ b/cjs/tests/index.js @@ -1789,6 +1789,21 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => { ] }) +t('Properly throws routing error on not prepared statements', async() => { + await sql`create table x (x text[])` + const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e) + + return ['transformAssignedExpr', routine, await sql`drop table x`] +}) + +t('Properly throws routing error on not prepared statements in transaction', async() => { + const { routine } = await sql.begin(sql => [ + sql`create table x (x text[])`, + sql`insert into x(x) values (('a', 'b'))`, + ]).catch(e => e) + + return ['transformAssignedExpr', routine] +}) t('Catches connection config errors', async() => { const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' }) diff --git a/deno/README.md b/deno/README.md index 0fc569bb..94a05714 100644 --- a/deno/README.md +++ b/deno/README.md @@ -266,7 +266,7 @@ const users = [ ] await sql` - update users set name = update_data.name, (age = update_data.age)::int + update users set name = update_data.name, age = (update_data.age)::int from (values ${sql(users)}) as update_data (id, name, age) where users.id = (update_data.id)::int returning users.id, users.name, users.age @@ -286,7 +286,7 @@ const users = await sql` or ```js -const [{ a, b, c }] => await sql` +const [{ a, b, c }] = await sql` select * from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c) @@ -913,7 +913,7 @@ The `Result` Array returned from queries is a custom array allowing for easy des ### .count -The `count` property is the number of affected rows returned by the database. This is usefull for insert, update and delete operations to know the number of rows since .length will be 0 in these cases if not using `RETURNING ...`. +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 ...`. ### .command @@ -1099,10 +1099,10 @@ export default async fetch(req: Request, env: Env, ctx: ExecutionContext) { } ``` -In `wrangler.toml` you will need to enable `node_compat` to allow Postgres.js to operate in the Workers environment: +In `wrangler.toml` you will need to enable the `nodejs_compat` compatibility flag to allow Postgres.js to operate in the Workers environment: ```toml -node_compat = true # required for database drivers to function +compatibility_flags = ["nodejs_compat"] ``` ### Auto fetching of array types diff --git a/deno/src/connection.js b/deno/src/connection.js index 334b9722..2722095c 100644 --- a/deno/src/connection.js +++ b/deno/src/connection.js @@ -432,10 +432,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose lifeTimer.cancel() connectTimer.cancel() - if (socket.encrypted) { - socket.removeAllListeners() - socket = null - } + socket.removeAllListeners() + socket = null if (initial) return reconnect() @@ -793,7 +791,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose const error = Errors.postgres(parseError(x)) query && query.retried ? errored(query.retried) - : query && retryRoutines.has(error.routine) + : query && query.prepare && retryRoutines.has(error.routine) ? retry(query, error) : errored(error) } diff --git a/deno/src/subscribe.js b/deno/src/subscribe.js index dbb9b971..57316fa6 100644 --- a/deno/src/subscribe.js +++ b/deno/src/subscribe.js @@ -48,7 +48,7 @@ export default function Subscribe(postgres, options) { return subscribe - async function subscribe(event, fn, onsubscribe = noop) { + async function subscribe(event, fn, onsubscribe = noop, onerror = noop) { event = parseEvent(event) if (!connection) @@ -67,6 +67,7 @@ export default function Subscribe(postgres, options) { return connection.then(x => { connected(x) onsubscribe() + stream && stream.on('error', onerror) return { unsubscribe, state, sql } }) } @@ -110,8 +111,10 @@ export default function Subscribe(postgres, options) { function data(x) { if (x[0] === 0x77) parse(x.subarray(25), state, sql.options.parsers, handle, options.transform) - else if (x[0] === 0x6b && x[17]) + else if (x[0] === 0x6b && x[17]) { + state.lsn = x.subarray(1, 9) pong() + } } function handle(a, b) { diff --git a/deno/tests/index.js b/deno/tests/index.js index 90d1feeb..55581c42 100644 --- a/deno/tests/index.js +++ b/deno/tests/index.js @@ -1791,6 +1791,21 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => { ] }) +t('Properly throws routing error on not prepared statements', async() => { + await sql`create table x (x text[])` + const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e) + + return ['transformAssignedExpr', routine, await sql`drop table x`] +}) + +t('Properly throws routing error on not prepared statements in transaction', async() => { + const { routine } = await sql.begin(sql => [ + sql`create table x (x text[])`, + sql`insert into x(x) values (('a', 'b'))`, + ]).catch(e => e) + + return ['transformAssignedExpr', routine] +}) t('Catches connection config errors', async() => { const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' }) diff --git a/deno/types/index.d.ts b/deno/types/index.d.ts index 6f96fe97..2088662d 100644 --- a/deno/types/index.d.ts +++ b/deno/types/index.d.ts @@ -601,6 +601,7 @@ declare namespace postgres { type RowList = T & Iterable> & ResultQueryMeta; interface PendingQueryModifiers { + simple(): this; readable(): Promise; writable(): Promise; @@ -692,7 +693,7 @@ declare namespace postgres { listen(channel: string, onnotify: (value: string) => void, onlisten?: (() => void) | undefined): ListenRequest; notify(channel: string, payload: string): PendingRequest; - subscribe(event: string, cb: (row: Row | null, info: ReplicationEvent) => void, onsubscribe?: (() => void) | undefined): Promise; + subscribe(event: string, cb: (row: Row | null, info: ReplicationEvent) => void, onsubscribe?: (() => void), onerror?: (() => any)): Promise; largeObject(oid?: number | undefined, /** @default 0x00020000 | 0x00040000 */ mode?: number | undefined): Promise; From f82ca1b85345650d5063745d80a61ac207826de1 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Wed, 20 Mar 2024 21:02:54 +0100 Subject: [PATCH 15/41] Properly check if prepared --- cjs/src/connection.js | 2 +- cjs/tests/index.js | 13 +++++++++++-- deno/src/connection.js | 2 +- deno/tests/index.js | 13 +++++++++++-- src/connection.js | 2 +- tests/index.js | 13 +++++++++++-- 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/cjs/src/connection.js b/cjs/src/connection.js index 9180693d..10184ca3 100644 --- a/cjs/src/connection.js +++ b/cjs/src/connection.js @@ -788,7 +788,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose const error = Errors.postgres(parseError(x)) query && query.retried ? errored(query.retried) - : query && query.prepare && retryRoutines.has(error.routine) + : query && query.prepared && retryRoutines.has(error.routine) ? retry(query, error) : errored(error) } diff --git a/cjs/tests/index.js b/cjs/tests/index.js index 437ed2f9..d49c7dcf 100644 --- a/cjs/tests/index.js +++ b/cjs/tests/index.js @@ -1789,14 +1789,14 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => { ] }) -t('Properly throws routing error on not prepared statements', async() => { +t('Properly throws routine error on not prepared statements', async() => { await sql`create table x (x text[])` const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e) return ['transformAssignedExpr', routine, await sql`drop table x`] }) -t('Properly throws routing error on not prepared statements in transaction', async() => { +t('Properly throws routine error on not prepared statements in transaction', async() => { const { routine } = await sql.begin(sql => [ sql`create table x (x text[])`, sql`insert into x(x) values (('a', 'b'))`, @@ -1805,6 +1805,15 @@ t('Properly throws routing error on not prepared statements in transaction', asy return ['transformAssignedExpr', routine] }) +t('Properly throws routine error on not prepared statements using file', async() => { + const { routine } = await sql.unsafe(` + create table x (x text[]); + insert into x(x) values (('a', 'b')); + `, { prepare: true }).catch(e => e) + + return ['transformAssignedExpr', routine] +}) + t('Catches connection config errors', async() => { const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' }) diff --git a/deno/src/connection.js b/deno/src/connection.js index 2722095c..81f26c08 100644 --- a/deno/src/connection.js +++ b/deno/src/connection.js @@ -791,7 +791,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose const error = Errors.postgres(parseError(x)) query && query.retried ? errored(query.retried) - : query && query.prepare && retryRoutines.has(error.routine) + : query && query.prepared && retryRoutines.has(error.routine) ? retry(query, error) : errored(error) } diff --git a/deno/tests/index.js b/deno/tests/index.js index 55581c42..055f479b 100644 --- a/deno/tests/index.js +++ b/deno/tests/index.js @@ -1791,14 +1791,14 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => { ] }) -t('Properly throws routing error on not prepared statements', async() => { +t('Properly throws routine error on not prepared statements', async() => { await sql`create table x (x text[])` const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e) return ['transformAssignedExpr', routine, await sql`drop table x`] }) -t('Properly throws routing error on not prepared statements in transaction', async() => { +t('Properly throws routine error on not prepared statements in transaction', async() => { const { routine } = await sql.begin(sql => [ sql`create table x (x text[])`, sql`insert into x(x) values (('a', 'b'))`, @@ -1807,6 +1807,15 @@ t('Properly throws routing error on not prepared statements in transaction', asy return ['transformAssignedExpr', routine] }) +t('Properly throws routine error on not prepared statements using file', async() => { + const { routine } = await sql.unsafe(` + create table x (x text[]); + insert into x(x) values (('a', 'b')); + `, { prepare: true }).catch(e => e) + + return ['transformAssignedExpr', routine] +}) + t('Catches connection config errors', async() => { const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' }) diff --git a/src/connection.js b/src/connection.js index 7f8ac5ea..578a6a02 100644 --- a/src/connection.js +++ b/src/connection.js @@ -788,7 +788,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose const error = Errors.postgres(parseError(x)) query && query.retried ? errored(query.retried) - : query && query.prepare && retryRoutines.has(error.routine) + : query && query.prepared && retryRoutines.has(error.routine) ? retry(query, error) : errored(error) } diff --git a/tests/index.js b/tests/index.js index 13734239..dd8d55da 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1789,14 +1789,14 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => { ] }) -t('Properly throws routing error on not prepared statements', async() => { +t('Properly throws routine error on not prepared statements', async() => { await sql`create table x (x text[])` const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e) return ['transformAssignedExpr', routine, await sql`drop table x`] }) -t('Properly throws routing error on not prepared statements in transaction', async() => { +t('Properly throws routine error on not prepared statements in transaction', async() => { const { routine } = await sql.begin(sql => [ sql`create table x (x text[])`, sql`insert into x(x) values (('a', 'b'))`, @@ -1805,6 +1805,15 @@ t('Properly throws routing error on not prepared statements in transaction', asy return ['transformAssignedExpr', routine] }) +t('Properly throws routine error on not prepared statements using file', async() => { + const { routine } = await sql.unsafe(` + create table x (x text[]); + insert into x(x) values (('a', 'b')); + `, { prepare: true }).catch(e => e) + + return ['transformAssignedExpr', routine] +}) + t('Catches connection config errors', async() => { const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' }) From 2524083cfbc39efc989f38dd4752ff08caa48bd1 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Thu, 21 Mar 2024 21:35:49 +0100 Subject: [PATCH 16/41] build --- cf/src/connection.js | 8 +++----- cf/src/subscribe.js | 7 +++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cf/src/connection.js b/cf/src/connection.js index ab977ca8..c9231dc6 100644 --- a/cf/src/connection.js +++ b/cf/src/connection.js @@ -431,10 +431,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose lifeTimer.cancel() connectTimer.cancel() - if (socket.encrypted) { - socket.removeAllListeners() - socket = null - } + socket.removeAllListeners() + socket = null if (initial) return reconnect() @@ -792,7 +790,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose const error = Errors.postgres(parseError(x)) query && query.retried ? errored(query.retried) - : query && retryRoutines.has(error.routine) + : query && query.prepared && retryRoutines.has(error.routine) ? retry(query, error) : errored(error) } diff --git a/cf/src/subscribe.js b/cf/src/subscribe.js index 1ab8b0be..35a98d61 100644 --- a/cf/src/subscribe.js +++ b/cf/src/subscribe.js @@ -48,7 +48,7 @@ export default function Subscribe(postgres, options) { return subscribe - async function subscribe(event, fn, onsubscribe = noop) { + async function subscribe(event, fn, onsubscribe = noop, onerror = noop) { event = parseEvent(event) if (!connection) @@ -67,6 +67,7 @@ export default function Subscribe(postgres, options) { return connection.then(x => { connected(x) onsubscribe() + stream && stream.on('error', onerror) return { unsubscribe, state, sql } }) } @@ -110,8 +111,10 @@ export default function Subscribe(postgres, options) { function data(x) { if (x[0] === 0x77) parse(x.subarray(25), state, sql.options.parsers, handle, options.transform) - else if (x[0] === 0x6b && x[17]) + else if (x[0] === 0x6b && x[17]) { + state.lsn = x.subarray(1, 9) pong() + } } function handle(a, b) { From a42de3035848955b946b21ac108b164b6281f383 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Thu, 21 Mar 2024 21:48:18 +0100 Subject: [PATCH 17/41] please eslint --- cf/src/subscribe.js | 6 +++--- cjs/src/subscribe.js | 6 +++--- cjs/tests/index.js | 6 ++++-- deno/src/subscribe.js | 6 +++--- deno/tests/index.js | 6 ++++-- src/subscribe.js | 6 +++--- tests/index.js | 6 ++++-- 7 files changed, 24 insertions(+), 18 deletions(-) diff --git a/cf/src/subscribe.js b/cf/src/subscribe.js index 35a98d61..8716100e 100644 --- a/cf/src/subscribe.js +++ b/cf/src/subscribe.js @@ -105,13 +105,13 @@ export default function Subscribe(postgres, options) { return { stream, state: xs.state } function error(e) { - console.error('Unexpected error during logical streaming - reconnecting', e) + console.error('Unexpected error during logical streaming - reconnecting', e) // eslint-disable-line } function data(x) { - if (x[0] === 0x77) + if (x[0] === 0x77) { parse(x.subarray(25), state, sql.options.parsers, handle, options.transform) - else if (x[0] === 0x6b && x[17]) { + } else if (x[0] === 0x6b && x[17]) { state.lsn = x.subarray(1, 9) pong() } diff --git a/cjs/src/subscribe.js b/cjs/src/subscribe.js index e450071e..6aaa8962 100644 --- a/cjs/src/subscribe.js +++ b/cjs/src/subscribe.js @@ -104,13 +104,13 @@ module.exports = Subscribe;function Subscribe(postgres, options) { return { stream, state: xs.state } function error(e) { - console.error('Unexpected error during logical streaming - reconnecting', e) + console.error('Unexpected error during logical streaming - reconnecting', e) // eslint-disable-line } function data(x) { - if (x[0] === 0x77) + if (x[0] === 0x77) { parse(x.subarray(25), state, sql.options.parsers, handle, options.transform) - else if (x[0] === 0x6b && x[17]) { + } else if (x[0] === 0x6b && x[17]) { state.lsn = x.subarray(1, 9) pong() } diff --git a/cjs/tests/index.js b/cjs/tests/index.js index d49c7dcf..7d84ac67 100644 --- a/cjs/tests/index.js +++ b/cjs/tests/index.js @@ -1791,7 +1791,9 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => { t('Properly throws routine error on not prepared statements', async() => { await sql`create table x (x text[])` - const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e) + const { routine } = await sql.unsafe(` + insert into x(x) values (('a', 'b')) + `).catch(e => e) return ['transformAssignedExpr', routine, await sql`drop table x`] }) @@ -1799,7 +1801,7 @@ t('Properly throws routine error on not prepared statements', async() => { t('Properly throws routine error on not prepared statements in transaction', async() => { const { routine } = await sql.begin(sql => [ sql`create table x (x text[])`, - sql`insert into x(x) values (('a', 'b'))`, + sql`insert into x(x) values (('a', 'b'))` ]).catch(e => e) return ['transformAssignedExpr', routine] diff --git a/deno/src/subscribe.js b/deno/src/subscribe.js index 57316fa6..b20efb96 100644 --- a/deno/src/subscribe.js +++ b/deno/src/subscribe.js @@ -105,13 +105,13 @@ export default function Subscribe(postgres, options) { return { stream, state: xs.state } function error(e) { - console.error('Unexpected error during logical streaming - reconnecting', e) + console.error('Unexpected error during logical streaming - reconnecting', e) // eslint-disable-line } function data(x) { - if (x[0] === 0x77) + if (x[0] === 0x77) { parse(x.subarray(25), state, sql.options.parsers, handle, options.transform) - else if (x[0] === 0x6b && x[17]) { + } else if (x[0] === 0x6b && x[17]) { state.lsn = x.subarray(1, 9) pong() } diff --git a/deno/tests/index.js b/deno/tests/index.js index 055f479b..754eabd3 100644 --- a/deno/tests/index.js +++ b/deno/tests/index.js @@ -1793,7 +1793,9 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => { t('Properly throws routine error on not prepared statements', async() => { await sql`create table x (x text[])` - const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e) + const { routine } = await sql.unsafe(` + insert into x(x) values (('a', 'b')) + `).catch(e => e) return ['transformAssignedExpr', routine, await sql`drop table x`] }) @@ -1801,7 +1803,7 @@ t('Properly throws routine error on not prepared statements', async() => { t('Properly throws routine error on not prepared statements in transaction', async() => { const { routine } = await sql.begin(sql => [ sql`create table x (x text[])`, - sql`insert into x(x) values (('a', 'b'))`, + sql`insert into x(x) values (('a', 'b'))` ]).catch(e => e) return ['transformAssignedExpr', routine] diff --git a/src/subscribe.js b/src/subscribe.js index 3db2f43b..4f8934cc 100644 --- a/src/subscribe.js +++ b/src/subscribe.js @@ -104,13 +104,13 @@ export default function Subscribe(postgres, options) { return { stream, state: xs.state } function error(e) { - console.error('Unexpected error during logical streaming - reconnecting', e) + console.error('Unexpected error during logical streaming - reconnecting', e) // eslint-disable-line } function data(x) { - if (x[0] === 0x77) + if (x[0] === 0x77) { parse(x.subarray(25), state, sql.options.parsers, handle, options.transform) - else if (x[0] === 0x6b && x[17]) { + } else if (x[0] === 0x6b && x[17]) { state.lsn = x.subarray(1, 9) pong() } diff --git a/tests/index.js b/tests/index.js index dd8d55da..bf81b036 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1791,7 +1791,9 @@ t('Recreate prepared statements on RevalidateCachedQuery error', async() => { t('Properly throws routine error on not prepared statements', async() => { await sql`create table x (x text[])` - const { routine } = await sql.unsafe(`insert into x(x) values (('a', 'b'))`).catch(e => e) + const { routine } = await sql.unsafe(` + insert into x(x) values (('a', 'b')) + `).catch(e => e) return ['transformAssignedExpr', routine, await sql`drop table x`] }) @@ -1799,7 +1801,7 @@ t('Properly throws routine error on not prepared statements', async() => { t('Properly throws routine error on not prepared statements in transaction', async() => { const { routine } = await sql.begin(sql => [ sql`create table x (x text[])`, - sql`insert into x(x) values (('a', 'b'))`, + sql`insert into x(x) values (('a', 'b'))` ]).catch(e => e) return ['transformAssignedExpr', routine] From 3eb40995fe8d878b40a69ce75fedf55f7c298ce0 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Thu, 21 Mar 2024 21:51:08 +0100 Subject: [PATCH 18/41] 3.4.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ea500a80..4fb9a160 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postgres", - "version": "3.4.3", + "version": "3.4.4", "description": "Fastest full featured PostgreSQL client for Node.js", "type": "module", "module": "src/index.js", From b8fa8f465429bbc9f9d64894f7b7769bc92762eb Mon Sep 17 00:00:00 2001 From: Heb Date: Thu, 29 Feb 2024 01:05:31 +0700 Subject: [PATCH 19/41] chore: update export conditions Hello there ! The official runtime export key for cloudflare is `workerd` (not worker). I believe many apps out there might already be relying on `worker` so I propose to add it alongside it. Reference : - https://developers.cloudflare.com/workers/wrangler/bundling/#conditional-exports - https://runtime-keys.proposal.wintercg.org/#workerd --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 4fb9a160..cd1545b9 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "types": "./types/index.d.ts", "bun": "./src/index.js", "worker": "./cf/src/index.js", + "workerd": "./cf/src/index.js", "import": "./src/index.js", "default": "./cjs/src/index.js" }, From cc688c642fc98c4338523d3e281e03bf0c3417b8 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Tue, 9 Apr 2024 22:22:21 +0200 Subject: [PATCH 20/41] Remove "worker" export as we now have "workerd" for cloudflare --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index cd1545b9..47f3add2 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "exports": { "types": "./types/index.d.ts", "bun": "./src/index.js", - "worker": "./cf/src/index.js", "workerd": "./cf/src/index.js", "import": "./src/index.js", "default": "./cjs/src/index.js" From 6bed5c0975ad78400b5b3f09767b3ea908d3b808 Mon Sep 17 00:00:00 2001 From: oakgary <13177748+oakgary@users.noreply.github.com> Date: Wed, 15 May 2024 12:26:36 +0200 Subject: [PATCH 21/41] corrects explnation of default max_lifetime values in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1b93c156..421d19a0 100644 --- a/README.md +++ b/README.md @@ -992,7 +992,7 @@ const sql = postgres('postgres://username:password@host:port/database', { }) ``` -Note that `max_lifetime = 60 * (30 + Math.random() * 30)` by default. This resolves to an interval between 45 and 90 minutes to optimize for the benefits of prepared statements **and** working nicely with Linux's OOM killer. +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. ### Dynamic passwords From e05585bdbd020640a7ae19e08ff78b9aa66e1c66 Mon Sep 17 00:00:00 2001 From: oakgary <13177748+oakgary@users.noreply.github.com> Date: Wed, 15 May 2024 12:33:15 +0200 Subject: [PATCH 22/41] corrects explnation of default max_lifetime values in deno/README.md --- deno/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deno/README.md b/deno/README.md index 94a05714..31ea4aea 100644 --- a/deno/README.md +++ b/deno/README.md @@ -988,7 +988,7 @@ const sql = postgres('postgres://username:password@host:port/database', { }) ``` -Note that `max_lifetime = 60 * (30 + Math.random() * 30)` by default. This resolves to an interval between 45 and 90 minutes to optimize for the benefits of prepared statements **and** working nicely with Linux's OOM killer. +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. ### Dynamic passwords From f58cd4f3affd3e8ce8f53e42799672d86cd2c70b Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Tue, 30 Apr 2024 14:03:04 +0200 Subject: [PATCH 23/41] Don't reassign to errors --- cjs/src/connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cjs/src/connection.js b/cjs/src/connection.js index 10184ca3..3b913a47 100644 --- a/cjs/src/connection.js +++ b/cjs/src/connection.js @@ -385,7 +385,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - Object.defineProperties(err, { + 'parameters' in err || Object.defineProperties(err, { stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug }, query: { value: query.string, enumerable: options.debug }, parameters: { value: query.parameters, enumerable: options.debug }, From 4baef5e4c6fbf6e655da033bfde2a7193623329a Mon Sep 17 00:00:00 2001 From: "Ch3rry B@ry" Date: Thu, 8 Aug 2024 12:32:03 +0530 Subject: [PATCH 24/41] Don't reassign to errors --- src/connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connection.js b/src/connection.js index 578a6a02..97cc97e1 100644 --- a/src/connection.js +++ b/src/connection.js @@ -385,7 +385,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - Object.defineProperties(err, { + 'query' in err || 'parameters' in err || Object.defineProperties(err, { stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug }, query: { value: query.string, enumerable: options.debug }, parameters: { value: query.parameters, enumerable: options.debug }, From 18186998b89a8ec60b82fd7140783f8833810e2d Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Wed, 18 Sep 2024 10:15:01 +1000 Subject: [PATCH 25/41] Update README.md to fix broken link to Node.js stream backpressure documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 421d19a0..7c7b83f0 100644 --- a/README.md +++ b/README.md @@ -537,7 +537,7 @@ for await (const chunk of readableStream) { } ``` -> **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/docs/guides/backpressuring-in-streams/) is handled correctly to avoid memory exhaustion. +> **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. ### Canceling Queries in Progress From 75dab3771074cec8595c0a403d1e19218017415c Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 27 Sep 2024 12:30:54 +0200 Subject: [PATCH 26/41] Try postgres 17 (might be too soon) --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aec631bf..48948290 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ jobs: fail-fast: false matrix: node: ['12', '14', '16', '18', '20', '21'] - postgres: ['12', '13', '14', '15', '16'] + postgres: ['12', '13', '14', '15', '16', '17'] runs-on: ubuntu-latest services: postgres: From f84f21a282b7a15ccb5cba6bb772f815bf0467f5 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 27 Sep 2024 12:42:12 +0200 Subject: [PATCH 27/41] also node 22 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 48948290..bf65797a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: false matrix: - node: ['12', '14', '16', '18', '20', '21'] + node: ['12', '14', '16', '18', '20', '21', '22'] postgres: ['12', '13', '14', '15', '16', '17'] runs-on: ubuntu-latest services: From 5fb70c14c08c7f378562571ea66ee7a69f19bd17 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 25 Oct 2024 10:36:31 +0200 Subject: [PATCH 28/41] Fix for deno 2 --- transpile.deno.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transpile.deno.js b/transpile.deno.js index 923ac9af..f077677b 100644 --- a/transpile.deno.js +++ b/transpile.deno.js @@ -55,7 +55,7 @@ function transpile(x, name, folder) { .replace('{ spawnSync }', '{ spawn }') } if (name === 'index.js') - x += '\n;window.addEventListener("unload", () => Deno.exit(process.exitCode))' + x += '\n;globalThis.addEventListener("unload", () => Deno.exit(process.exitCode))' } const buffer = x.includes('Buffer') From 5974e7fcc171e456737e9eb34a90f0ba2ea6ef56 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 25 Oct 2024 10:36:45 +0200 Subject: [PATCH 29/41] build --- cf/src/connection.js | 2 +- cjs/src/connection.js | 2 +- deno/README.md | 2 +- deno/src/connection.js | 2 +- deno/tests/index.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cf/src/connection.js b/cf/src/connection.js index c9231dc6..ee8b1e69 100644 --- a/cf/src/connection.js +++ b/cf/src/connection.js @@ -387,7 +387,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - Object.defineProperties(err, { + 'query' in err || 'parameters' in err || Object.defineProperties(err, { stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug }, query: { value: query.string, enumerable: options.debug }, parameters: { value: query.parameters, enumerable: options.debug }, diff --git a/cjs/src/connection.js b/cjs/src/connection.js index 3b913a47..f7f58d14 100644 --- a/cjs/src/connection.js +++ b/cjs/src/connection.js @@ -385,7 +385,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - 'parameters' in err || Object.defineProperties(err, { + 'query' in err || 'parameters' in err || Object.defineProperties(err, { stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug }, query: { value: query.string, enumerable: options.debug }, parameters: { value: query.parameters, enumerable: options.debug }, diff --git a/deno/README.md b/deno/README.md index 31ea4aea..6f8085cf 100644 --- a/deno/README.md +++ b/deno/README.md @@ -533,7 +533,7 @@ for await (const chunk of readableStream) { } ``` -> **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/docs/guides/backpressuring-in-streams/) is handled correctly to avoid memory exhaustion. +> **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. ### Canceling Queries in Progress diff --git a/deno/src/connection.js b/deno/src/connection.js index 81f26c08..1726a9aa 100644 --- a/deno/src/connection.js +++ b/deno/src/connection.js @@ -388,7 +388,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - Object.defineProperties(err, { + 'query' in err || 'parameters' in err || Object.defineProperties(err, { stack: { value: err.stack + query.origin.replace(/.*\n/, '\n'), enumerable: options.debug }, query: { value: query.string, enumerable: options.debug }, parameters: { value: query.parameters, enumerable: options.debug }, diff --git a/deno/tests/index.js b/deno/tests/index.js index 754eabd3..5b5d6e57 100644 --- a/deno/tests/index.js +++ b/deno/tests/index.js @@ -2583,4 +2583,4 @@ t('arrays in reserved connection', async() => { ] }) -;window.addEventListener("unload", () => Deno.exit(process.exitCode)) \ No newline at end of file +;globalThis.addEventListener("unload", () => Deno.exit(process.exitCode)) \ No newline at end of file From b231b688489212e40ab54e9870f84f55f2be5dd0 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 25 Oct 2024 10:42:52 +0200 Subject: [PATCH 30/41] 3.4.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 47f3add2..d53fe2ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postgres", - "version": "3.4.4", + "version": "3.4.5", "description": "Fastest full featured PostgreSQL client for Node.js", "type": "module", "module": "src/index.js", From 9f38ea1c2e2ab88c4b1c207c32c68ee47c327e2a Mon Sep 17 00:00:00 2001 From: gimse <23360355+gimse@users.noreply.github.com> Date: Sun, 7 Jul 2024 10:07:18 +0200 Subject: [PATCH 31/41] adding env.PGUSERNAME || --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 0573e2bc..b6a9a9f7 100644 --- a/src/index.js +++ b/src/index.js @@ -480,7 +480,7 @@ function parseOptions(a, b) { {} ), connection : { - application_name: 'postgres.js', + application_name: env.PGUSERNAME || 'postgres.js', ...o.connection, ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {}) }, From be716e220066470436012d76eec850a37de2f077 Mon Sep 17 00:00:00 2001 From: gimse <23360355+gimse@users.noreply.github.com> Date: Sun, 7 Jul 2024 10:14:15 +0200 Subject: [PATCH 32/41] README --- README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7c7b83f0..8f59cef4 100644 --- a/README.md +++ b/README.md @@ -1125,15 +1125,16 @@ It is also possible to connect to the database without a connection string or an const sql = postgres() ``` -| Option | Environment Variables | -| ----------------- | ------------------------ | -| `host` | `PGHOST` | -| `port` | `PGPORT` | -| `database` | `PGDATABASE` | -| `username` | `PGUSERNAME` or `PGUSER` | -| `password` | `PGPASSWORD` | -| `idle_timeout` | `PGIDLE_TIMEOUT` | -| `connect_timeout` | `PGCONNECT_TIMEOUT` | +| Option | Environment Variables | +| ------------------ | ------------------------ | +| `host` | `PGHOST` | +| `port` | `PGPORT` | +| `database` | `PGDATABASE` | +| `username` | `PGUSERNAME` or `PGUSER` | +| `password` | `PGPASSWORD` | +| `application_name` | `PGAPPNAME` | +| `idle_timeout` | `PGIDLE_TIMEOUT` | +| `connect_timeout` | `PGCONNECT_TIMEOUT` | ### Prepared statements From ef7afdb817d00cc7208bd1cefa88f861bfc2cbde Mon Sep 17 00:00:00 2001 From: gimse <23360355+gimse@users.noreply.github.com> Date: Sun, 7 Jul 2024 10:14:47 +0200 Subject: [PATCH 33/41] env.PGAPPNAME || --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index b6a9a9f7..2dfd24e8 100644 --- a/src/index.js +++ b/src/index.js @@ -480,7 +480,7 @@ function parseOptions(a, b) { {} ), connection : { - application_name: env.PGUSERNAME || 'postgres.js', + application_name: env.PGAPPNAME || 'postgres.js', ...o.connection, ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {}) }, From 4099f3412bb1d9f58ef223e7e4444bc5e4a74a2d Mon Sep 17 00:00:00 2001 From: gimse <23360355+gimse@users.noreply.github.com> Date: Sun, 7 Jul 2024 10:35:18 +0200 Subject: [PATCH 34/41] changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8939f7c8..ed7ec4f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## v3.3.0 - 9 July 2024 +- Adding support for the PGAPPNAME environment variable + ## v3.2.4 - 25 May 2022 - Allow setting keep_alive: false bee62f3 - Fix support for null in arrays - fixes #371 b04c853 From a2c7de12b3bfc6809051d94ba6115150f80678e3 Mon Sep 17 00:00:00 2001 From: gimse <23360355+gimse@users.noreply.github.com> Date: Sun, 7 Jul 2024 15:01:06 +0200 Subject: [PATCH 35/41] removing change log --- CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed7ec4f8..8939f7c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,5 @@ # Changelog -## v3.3.0 - 9 July 2024 -- Adding support for the PGAPPNAME environment variable - ## v3.2.4 - 25 May 2022 - Allow setting keep_alive: false bee62f3 - Fix support for null in arrays - fixes #371 b04c853 From 6ec85a432b17661ccacbdf7f765c651e88969d36 Mon Sep 17 00:00:00 2001 From: mrl5 <31549762+mrl5@users.noreply.github.com> Date: Fri, 24 May 2024 19:34:59 +0200 Subject: [PATCH 36/41] docs(readme): mention pgbouncer supports protocol-level named prepared statements --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 8f59cef4..1dcdd668 100644 --- a/README.md +++ b/README.md @@ -1140,6 +1140,10 @@ const sql = postgres() 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). +**update**: [since 1.21.0](https://www.pgbouncer.org/2023/10/pgbouncer-1-21-0) +PGBouncer supports protocol-level named prepared statements when [configured +properly](https://www.pgbouncer.org/config.html#max_prepared_statements) + ## Custom Types 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.)_ From 93f5686ff9cd86ab0590e79b4d94f984a40183ad Mon Sep 17 00:00:00 2001 From: Valentinas Janeiko Date: Tue, 21 Jan 2025 19:51:25 +0000 Subject: [PATCH 37/41] chore: fix CI --- .github/workflows/test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bf65797a..8ae323dd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,7 +28,6 @@ jobs: - uses: actions/checkout@v4 - run: | date - sudo apt purge postgresql-14 sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update From 3374a8aeb681b9d573459f1f5897c854a367cc55 Mon Sep 17 00:00:00 2001 From: Valentinas Janeiko <2305836+valeneiko@users.noreply.github.com> Date: Tue, 21 Jan 2025 20:29:12 +0000 Subject: [PATCH 38/41] try purging PG16 instead? --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8ae323dd..af00f7e0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,6 +28,7 @@ jobs: - uses: actions/checkout@v4 - run: | date + sudo apt purge postgresql-16 sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update From 089214e85c23c90cf142d47fb30bd03f42874984 Mon Sep 17 00:00:00 2001 From: Louis Orleans Date: Tue, 21 Jan 2025 17:16:15 -0800 Subject: [PATCH 39/41] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20fix=20CONNECT=5FTIME?= =?UTF-8?q?OUT=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `CONNECT_TIMEOUT` error's name is `CONNECT_TIMEOUT`, not `CONNECTION_CONNECT_TIMEOUT`. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1dcdd668..b79c207f 100644 --- a/README.md +++ b/README.md @@ -1303,8 +1303,8 @@ This error is thrown if the user has called [`sql.end()`](#teardown--cleanup) an This error is thrown for any queries that were pending when the timeout to [`sql.end({ timeout: X })`](#teardown--cleanup) was reached. -##### CONNECTION_CONNECT_TIMEOUT -> write CONNECTION_CONNECT_TIMEOUT host:port +##### CONNECT_TIMEOUT +> write CONNECT_TIMEOUT host:port 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`. From ad0ed4476e09f41f147859cb5a42971d2b99e9c7 Mon Sep 17 00:00:00 2001 From: adrtivv Date: Fri, 11 Apr 2025 00:37:07 +0530 Subject: [PATCH 40/41] fixed typings for generic error code variants --- types/index.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 8989ff47..eb604918 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -456,7 +456,8 @@ declare namespace postgres { | 'NOT_TAGGED_CALL' | 'UNDEFINED_VALUE' | 'MAX_PARAMETERS_EXCEEDED' - | 'SASL_SIGNATURE_MISMATCH'; + | 'SASL_SIGNATURE_MISMATCH' + | 'UNSAFE_TRANSACTION'; message: string; } From b0d8c8f363e006a74472d76f859da60c52a80368 Mon Sep 17 00:00:00 2001 From: Stephen Haberman Date: Sat, 3 May 2025 21:23:22 -0500 Subject: [PATCH 41/41] docs: Add prepare: true to sql.unsafe docs. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b79c207f..c135cd17 100644 --- a/README.md +++ b/README.md @@ -568,6 +568,8 @@ If you know what you're doing, you can use `unsafe` to pass any string you'd lik sql.unsafe('select ' + danger + ' from users where id = ' + dragons) ``` +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 }`. + You can also nest `sql.unsafe` within a safe `sql` expression. This is useful if only part of your fraction has unsafe elements. ```js