diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 85a859ff..970d2771 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,8 +8,8 @@ jobs: strategy: fail-fast: false matrix: - node: ['12', '14', '16', '18', '20'] - postgres: ['12', '13', '14', '15', '16'] + node: ['12', '14', '16', '18', '20', '21', '22', '23', '24'] + postgres: ['12', '13', '14', '15', '16', '17'] runs-on: ubuntu-latest services: postgres: @@ -25,10 +25,10 @@ jobs: --health-timeout 5s --health-retries 5 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - run: | date - sudo apt purge postgresql-14 + 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 @@ -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 diff --git a/README.md b/README.md index 39678273..b04ac21c 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,14 @@ async function insertUser({ name, age }) { } ``` +#### ESM dynamic imports + +The library can be used with ESM dynamic imports as well as shown here. + +```js +const { default: postgres } = await import('postgres') +``` + ## Table of Contents * [Connection](#connection) @@ -158,7 +166,7 @@ const users = await sql` ```js const columns = ['name', 'age'] -sql` +await sql` select ${ sql(columns) } from users @@ -211,13 +219,13 @@ const users = [{ age: 80 }] -sql`insert into users ${ sql(users, 'name', 'age') }` +await sql`insert into users ${ sql(users, 'name', 'age') }` // Is translated to: insert into users ("name", "age") values ($1, $2), ($3, $4) // Here you can also omit column names which will use object keys as columns -sql`insert into users ${ sql(users) }` +await sql`insert into users ${ sql(users) }` // Which results in: insert into users ("name", "age") values ($1, $2), ($3, $4) @@ -261,8 +269,8 @@ const users = [ [2, 'Jane', 27], ] -sql` - update users set name = update_data.name, (age = update_data.age)::int +await sql` + 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 @@ -282,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) @@ -300,7 +308,7 @@ const olderThan = x => sql`and age > ${ x }` const filterAge = true -sql` +await sql` select * from users @@ -318,7 +326,7 @@ select * from users where name is not null and age > 50 ### Dynamic filters ```js -sql` +await sql` select * from users ${ @@ -334,12 +342,33 @@ select * from users select * from users where user_id = $1 ``` +### Dynamic ordering + +```js +const id = 1 +const order = { + username: 'asc' + created_at: 'desc' +} +await sql` + select + * + from ticket + where account = ${ id } + order by ${ + Object.entries(order).flatMap(([column, order], i) => + [i ? sql`,` : sql``, sql`${ sql(column) } ${ order === 'desc' ? sql`desc` : sql`asc` }`] + ) + } +` +``` + ### SQL functions Using keywords or calling functions dynamically is also possible by using ``` sql`` ``` fragments. ```js const date = null -sql` +await sql` update users set updated_at = ${ date || sql`now()` } ` @@ -353,7 +382,7 @@ Dynamic identifiers like table names and column names is also supported like so: const table = 'users' , column = 'id' -sql` +await sql` select ${ sql(column) } from ${ sql(table) } ` @@ -367,10 +396,10 @@ Here's a quick oversight over all the ways to do interpolation in a query templa | Interpolation syntax | Usage | Example | | ------------- | ------------- | ------------- | -| `${ sql`` }` | for keywords or sql fragments | ``sql`SELECT * FROM users ${sql`order by age desc` }` `` | -| `${ sql(string) }` | for identifiers | ``sql`SELECT * FROM ${sql('table_name')` `` | -| `${ sql([] or {}, ...) }` | for helpers | ``sql`INSERT INTO users ${sql({ name: 'Peter'})}` `` | -| `${ 'somevalue' }` | for values | ``sql`SELECT * FROM users WHERE age = ${42}` `` | +| `${ sql`` }` | for keywords or sql fragments | ``await sql`SELECT * FROM users ${sql`order by age desc` }` `` | +| `${ sql(string) }` | for identifiers | ``await sql`SELECT * FROM ${sql('table_name')` `` | +| `${ sql([] or {}, ...) }` | for helpers | ``await sql`INSERT INTO users ${sql({ name: 'Peter'})}` `` | +| `${ 'somevalue' }` | for values | ``await sql`SELECT * FROM users WHERE age = ${42}` `` | ## Advanced query methods @@ -450,7 +479,7 @@ await sql` 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. 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.** - + ### Rows as Array of Values #### ```sql``.values()``` @@ -477,7 +506,7 @@ const result = await sql.file('query.sql', ['Murray', 68]) ### Multiple statements in one query #### ```await sql``.simple()``` -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 +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 ```sql``.simple()```. That will create it as a simple query. ```js @@ -519,8 +548,8 @@ await pipeline(readableStream, createWriteStream('output.tsv')) ```js const readableStream = await sql` copy ( - select name, age - from users + select name, age + from users where age = 68 ) to stdout `.readable() @@ -529,7 +558,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 @@ -559,7 +588,9 @@ If you know what you're doing, you can use `unsafe` to pass any string you'd lik ```js 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 @@ -599,7 +630,7 @@ const [user, account] = await sql.begin(async sql => { ) values ( 'Murray' ) - returning * + returning * ` const [account] = await sql` @@ -608,7 +639,7 @@ const [user, account] = await sql.begin(async sql => { ) values ( ${ user.user_id } ) - returning * + returning * ` return [user, account] @@ -676,7 +707,7 @@ sql.begin('read write', async sql => { 'Murray' ) ` - + await sql.prepare('tx1') }) ``` @@ -736,7 +767,7 @@ console.log(data) // [ { a_test: 1 } ] ### Transform `undefined` Values -By default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed +By default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed ```js // Transform the column names to and from camel case @@ -817,7 +848,7 @@ The optional `onlisten` method is great to use for a very simply queue mechanism ```js await sql.listen( - 'jobs', + 'jobs', (x) => run(JSON.parse(x)), ( ) => sql`select unfinished_jobs()`.forEach(run) ) @@ -850,7 +881,7 @@ CREATE PUBLICATION alltables FOR ALL TABLES const sql = postgres({ publications: 'alltables' }) const { unsubscribe } = await sql.subscribe( - 'insert:events', + 'insert:events', (row, { command, relation, key, old }) => { // Callback function for each row change // tell about new event row over eg. websockets or do something else @@ -909,7 +940,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 @@ -975,7 +1006,7 @@ const sql = postgres('postgres://username:password@host:port/database', { }, connection : { application_name : 'postgres.js', // Default application_name - ... // Other connection parameters + ... // Other connection parameters, see https://www.postgresql.org/docs/current/runtime-config-client.html }, target_session_attrs : null, // Use 'read-write' with multiple hosts to // ensure only connecting to primary @@ -984,7 +1015,20 @@ 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 + +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. + +```js +const sql = postgres(url, { + // Other connection config + ... + // Password function for the database user + password : async () => await signer.getAuthToken(), +}) +``` ### SSL @@ -1082,10 +1126,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 @@ -1104,20 +1148,25 @@ 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 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.)_ @@ -1144,7 +1193,7 @@ const sql = postgres({ }) // Now you can use sql.typed.rect() as specified above -const [custom] = sql` +const [custom] = await sql` insert into rectangles ( name, rect @@ -1174,8 +1223,8 @@ const sql = postgres({ const ssh = new ssh2.Client() ssh .on('error', reject) - .on('ready', () => - ssh.forwardOut('127.0.0.1', 12345, host, port, + .on('ready', () => + ssh.forwardOut('127.0.0.1', 12345, host, port, (err, socket) => err ? reject(err) : resolve(socket) ) ) @@ -1277,8 +1326,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`. diff --git a/cf/polyfills.js b/cf/polyfills.js index f7809003..53c5203d 100644 --- a/cf/polyfills.js +++ b/cf/polyfills.js @@ -47,12 +47,25 @@ export const crypto = { ), createHash: type => ({ update: x => ({ - digest: () => { - if (type !== 'sha256') - throw Error('createHash only supports sha256 in this environment.') - if (!(x instanceof Uint8Array)) + digest: encoding => { + if (!(x instanceof Uint8Array)) { x = textEncoder.encode(x) - return Crypto.subtle.digest('SHA-256', x) + } + let prom + if (type === 'sha256') { + prom = Crypto.subtle.digest('SHA-256', x) + } else if (type === 'md5') { + prom = Crypto.subtle.digest('md5', x) + } else { + throw Error('createHash only supports sha256 or md5 in this environment, not ${type}.') + } + if (encoding === 'hex') { + return prom.then((arrayBuf) => Buffer.from(arrayBuf).toString('hex')) + } else if (encoding) { + throw Error(`createHash only supports hex encoding or unencoded in this environment, not ${encoding}`) + } else { + return prom + } } }) }), diff --git a/cf/src/connection.js b/cf/src/connection.js index c09b2720..203af80d 100644 --- a/cf/src/connection.js +++ b/cf/src/connection.js @@ -295,7 +295,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose if (incomings) { incomings.push(x) remaining -= x.length - if (remaining >= 0) + if (remaining > 0) return } @@ -387,13 +387,20 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - query.reject(Object.create(err, { + if (query.reserve) + return query.reject(err) + + if (!err || typeof err !== 'object') + err = new Error(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 }, args: { value: query.args, enumerable: options.debug }, types: { value: query.statement && query.statement.types, enumerable: options.debug } - })) + }) + query.reject(err) } function end() { @@ -430,10 +437,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() @@ -442,7 +447,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose closedDate = performance.now() hadError && options.shared.retries++ delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000 - onclose(connection) + onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket)) } /* Handlers */ @@ -534,11 +539,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose return terminate() } - if (needsTypes) + if (needsTypes) { + initial.reserve && (initial = null) return fetchArrayTypes() + } - execute(initial) - options.shared.retries = retries = initial = 0 + initial && !initial.reserve && execute(initial) + options.shared.retries = retries = 0 + initial = null return } @@ -658,27 +666,30 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose /* c8 ignore next 5 */ async function AuthenticationCleartextPassword() { + const payload = await Pass() write( - b().p().str(await Pass()).z(1).end() + b().p().str(payload).z(1).end() ) } async function AuthenticationMD5Password(x) { - write( - b().p().str( - 'md5' + - (await md5(Buffer.concat([ + const payload = 'md5' + ( + await md5( + Buffer.concat([ Buffer.from(await md5((await Pass()) + user)), x.subarray(9) - ]))) - ).z(1).end() + ]) + ) + ) + write( + b().p().str(payload).z(1).end() ) } async function SASL() { + nonce = (await crypto.randomBytes(18)).toString('base64') b().p().str('SCRAM-SHA-256' + b.N) const i = b.i - nonce = (await crypto.randomBytes(18)).toString('base64') write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end()) } @@ -700,12 +711,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64') + const payload = 'c=biws,r=' + res.r + ',p=' + xor( + clientKey, Buffer.from(await hmac(await sha256(clientKey), auth)) + ).toString('base64') + write( - b().p().str( - 'c=biws,r=' + res.r + ',p=' + xor( - clientKey, Buffer.from(await hmac(await sha256(clientKey), auth)) - ).toString('base64') - ).end() + b().p().str(payload).end() ) } @@ -785,7 +796,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/index.js b/cf/src/index.js index 0c74f5cf..3ffb7e65 100644 --- a/cf/src/index.js +++ b/cf/src/index.js @@ -205,9 +205,10 @@ function Postgres(a, b) { const queue = Queue() const c = open.length ? open.shift() - : await new Promise(r => { - queries.push({ reserve: r }) - closed.length && connect(closed.shift()) + : await new Promise((resolve, reject) => { + const query = { reserve: resolve, reject } + queries.push(query) + closed.length && connect(closed.shift(), query) }) move(c, reserved) @@ -240,7 +241,10 @@ function Postgres(a, b) { try { await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute() - return await scope(connection, fn) + return await Promise.race([ + scope(connection, fn), + new Promise((_, reject) => connection.onclose = reject) + ]) } catch (error) { throw error } @@ -415,9 +419,10 @@ function Postgres(a, b) { : move(c, full) } - function onclose(c) { + function onclose(c, e) { move(c, closed) c.reserved = null + c.onclose && (c.onclose(e), c.onclose = null) options.onclose && options.onclose(c.id) queries.length && connect(c, queries.shift()) } @@ -438,6 +443,7 @@ function parseOptions(a, b) { o.no_prepare && (o.prepare = false) query.sslmode && (query.ssl = query.sslmode, delete query.sslmode) 'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line + query.sslrootcert === 'system' && (query.ssl = 'verify-full') const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive'] const defaults = { @@ -476,7 +482,7 @@ function parseOptions(a, b) { {} ), connection : { - application_name: 'postgres.js', + application_name: env.PGAPPNAME || 'postgres.js', ...o.connection, ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {}) }, diff --git a/cf/src/subscribe.js b/cf/src/subscribe.js index 1ab8b0be..8716100e 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 } }) } @@ -104,14 +105,16 @@ 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() + } } function handle(a, b) { diff --git a/cjs/src/connection.js b/cjs/src/connection.js index 5e3f26d0..589d3638 100644 --- a/cjs/src/connection.js +++ b/cjs/src/connection.js @@ -293,7 +293,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose if (incomings) { incomings.push(x) remaining -= x.length - if (remaining >= 0) + if (remaining > 0) return } @@ -385,13 +385,20 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - query.reject(Object.create(err, { + if (query.reserve) + return query.reject(err) + + if (!err || typeof err !== 'object') + err = new Error(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 }, args: { value: query.args, enumerable: options.debug }, types: { value: query.statement && query.statement.types, enumerable: options.debug } - })) + }) + query.reject(err) } function end() { @@ -428,10 +435,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() @@ -440,7 +445,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose closedDate = performance.now() hadError && options.shared.retries++ delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000 - onclose(connection) + onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket)) } /* Handlers */ @@ -532,11 +537,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose return terminate() } - if (needsTypes) + if (needsTypes) { + initial.reserve && (initial = null) return fetchArrayTypes() + } - execute(initial) - options.shared.retries = retries = initial = 0 + initial && !initial.reserve && execute(initial) + options.shared.retries = retries = 0 + initial = null return } @@ -656,27 +664,30 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose /* c8 ignore next 5 */ async function AuthenticationCleartextPassword() { + const payload = await Pass() write( - b().p().str(await Pass()).z(1).end() + b().p().str(payload).z(1).end() ) } async function AuthenticationMD5Password(x) { - write( - b().p().str( - 'md5' + - (await md5(Buffer.concat([ + const payload = 'md5' + ( + await md5( + Buffer.concat([ Buffer.from(await md5((await Pass()) + user)), x.subarray(9) - ]))) - ).z(1).end() + ]) + ) + ) + write( + b().p().str(payload).z(1).end() ) } async function SASL() { + nonce = (await crypto.randomBytes(18)).toString('base64') b().p().str('SCRAM-SHA-256' + b.N) const i = b.i - nonce = (await crypto.randomBytes(18)).toString('base64') write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end()) } @@ -698,12 +709,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64') + const payload = 'c=biws,r=' + res.r + ',p=' + xor( + clientKey, Buffer.from(await hmac(await sha256(clientKey), auth)) + ).toString('base64') + write( - b().p().str( - 'c=biws,r=' + res.r + ',p=' + xor( - clientKey, Buffer.from(await hmac(await sha256(clientKey), auth)) - ).toString('base64') - ).end() + b().p().str(payload).end() ) } @@ -783,7 +794,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/cjs/src/index.js b/cjs/src/index.js index 698b05d4..baf7e60a 100644 --- a/cjs/src/index.js +++ b/cjs/src/index.js @@ -204,9 +204,10 @@ function Postgres(a, b) { const queue = Queue() const c = open.length ? open.shift() - : await new Promise(r => { - queries.push({ reserve: r }) - closed.length && connect(closed.shift()) + : await new Promise((resolve, reject) => { + const query = { reserve: resolve, reject } + queries.push(query) + closed.length && connect(closed.shift(), query) }) move(c, reserved) @@ -239,7 +240,10 @@ function Postgres(a, b) { try { await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute() - return await scope(connection, fn) + return await Promise.race([ + scope(connection, fn), + new Promise((_, reject) => connection.onclose = reject) + ]) } catch (error) { throw error } @@ -414,9 +418,10 @@ function Postgres(a, b) { : move(c, full) } - function onclose(c) { + function onclose(c, e) { move(c, closed) c.reserved = null + c.onclose && (c.onclose(e), c.onclose = null) options.onclose && options.onclose(c.id) queries.length && connect(c, queries.shift()) } @@ -437,6 +442,7 @@ function parseOptions(a, b) { o.no_prepare && (o.prepare = false) query.sslmode && (query.ssl = query.sslmode, delete query.sslmode) 'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line + query.sslrootcert === 'system' && (query.ssl = 'verify-full') const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive'] const defaults = { @@ -475,7 +481,7 @@ function parseOptions(a, b) { {} ), connection : { - application_name: 'postgres.js', + application_name: env.PGAPPNAME || 'postgres.js', ...o.connection, ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {}) }, diff --git a/cjs/src/subscribe.js b/cjs/src/subscribe.js index 34d99e9f..6aaa8962 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 } }) } @@ -103,14 +104,16 @@ 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() + } } function handle(a, b) { diff --git a/cjs/tests/index.js b/cjs/tests/index.js index cb91c5c5..ec5222f7 100644 --- a/cjs/tests/index.js +++ b/cjs/tests/index.js @@ -429,6 +429,30 @@ t('Reconnect using SSL', { timeout: 2 }, async() => { return [1, (await sql`select 1 as x`)[0].x] }) +t('Proper handling of non object Errors', async() => { + const sql = postgres({ socket: () => { throw 'wat' } }) // eslint-disable-line + + return [ + 'wat', await sql`select 1 as x`.catch(e => e.message) + ] +}) + +t('Proper handling of null Errors', async() => { + const sql = postgres({ socket: () => { throw null } }) // eslint-disable-line + + return [ + 'null', await sql`select 1 as x`.catch(e => e.message) + ] +}) + +t('Ensure reserve on connection throws proper error', async() => { + const sql = postgres({ socket: () => { throw 'wat' }, idle_timeout }) // eslint-disable-line + + return [ + 'wat', await sql.reserve().catch(e => e) + ] +}) + t('Login without password', async() => { return [true, (await postgres({ ...options, ...login })`select true as x`)[0].x] }) @@ -1789,6 +1813,32 @@ 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) + + return ['transformAssignedExpr', routine, await sql`drop table x`] +}) + +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'))` + ]).catch(e => e) + + 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' }) @@ -2134,7 +2184,7 @@ t('Execute', async() => { t('Cancel running query', async() => { const query = sql`select pg_sleep(2)` - setTimeout(() => query.cancel(), 200) + setTimeout(() => query.cancel(), 500) const error = await query.catch(x => x) return ['57014', error.code] }) @@ -2348,6 +2398,17 @@ t('Ensure reconnect after max_lifetime with transactions', { timeout: 5 }, async return [true, true] }) + +t('Ensure transactions throw if connection is closed dwhile there is no query', async() => { + const sql = postgres(options) + const x = await sql.begin(async() => { + setTimeout(() => sql.end({ timeout: 0 }), 10) + await new Promise(r => setTimeout(r, 200)) + return sql`select 1` + }).catch(x => x) + return ['CONNECTION_CLOSED', x.code] +}) + t('Custom socket', {}, async() => { let result const sql = postgres({ @@ -2532,3 +2593,24 @@ 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('') + ] +}) + +t('Ensure reserve on query throws proper error', async() => { + const sql = postgres({ idle_timeout }) // eslint-disable-line + const reserved = await sql.reserve() + const [{ x }] = await reserved`select 'wat' as x` + + return [ + 'wat', x, reserved.release() + ] +}) diff --git a/deno/README.md b/deno/README.md index 19fd0993..b6ec85b7 100644 --- a/deno/README.md +++ b/deno/README.md @@ -58,6 +58,14 @@ async function insertUser({ name, age }) { } ``` +#### ESM dynamic imports + +The library can be used with ESM dynamic imports as well as shown here. + +```js +const { default: postgres } = await import('postgres') +``` + ## Table of Contents * [Connection](#connection) @@ -154,7 +162,7 @@ const users = await sql` ```js const columns = ['name', 'age'] -sql` +await sql` select ${ sql(columns) } from users @@ -207,13 +215,13 @@ const users = [{ age: 80 }] -sql`insert into users ${ sql(users, 'name', 'age') }` +await sql`insert into users ${ sql(users, 'name', 'age') }` // Is translated to: insert into users ("name", "age") values ($1, $2), ($3, $4) // Here you can also omit column names which will use object keys as columns -sql`insert into users ${ sql(users) }` +await sql`insert into users ${ sql(users) }` // Which results in: insert into users ("name", "age") values ($1, $2), ($3, $4) @@ -257,8 +265,8 @@ const users = [ [2, 'Jane', 27], ] -sql` - update users set name = update_data.name, (age = update_data.age)::int +await sql` + 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 @@ -278,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) @@ -296,7 +304,7 @@ const olderThan = x => sql`and age > ${ x }` const filterAge = true -sql` +await sql` select * from users @@ -314,7 +322,7 @@ select * from users where name is not null and age > 50 ### Dynamic filters ```js -sql` +await sql` select * from users ${ @@ -330,12 +338,33 @@ select * from users select * from users where user_id = $1 ``` +### Dynamic ordering + +```js +const id = 1 +const order = { + username: 'asc' + created_at: 'desc' +} +await sql` + select + * + from ticket + where account = ${ id } + order by ${ + Object.entries(order).flatMap(([column, order], i) => + [i ? sql`,` : sql``, sql`${ sql(column) } ${ order === 'desc' ? sql`desc` : sql`asc` }`] + ) + } +` +``` + ### SQL functions Using keywords or calling functions dynamically is also possible by using ``` sql`` ``` fragments. ```js const date = null -sql` +await sql` update users set updated_at = ${ date || sql`now()` } ` @@ -349,7 +378,7 @@ Dynamic identifiers like table names and column names is also supported like so: const table = 'users' , column = 'id' -sql` +await sql` select ${ sql(column) } from ${ sql(table) } ` @@ -363,10 +392,10 @@ Here's a quick oversight over all the ways to do interpolation in a query templa | Interpolation syntax | Usage | Example | | ------------- | ------------- | ------------- | -| `${ sql`` }` | for keywords or sql fragments | ``sql`SELECT * FROM users ${sql`order by age desc` }` `` | -| `${ sql(string) }` | for identifiers | ``sql`SELECT * FROM ${sql('table_name')` `` | -| `${ sql([] or {}, ...) }` | for helpers | ``sql`INSERT INTO users ${sql({ name: 'Peter'})}` `` | -| `${ 'somevalue' }` | for values | ``sql`SELECT * FROM users WHERE age = ${42}` `` | +| `${ sql`` }` | for keywords or sql fragments | ``await sql`SELECT * FROM users ${sql`order by age desc` }` `` | +| `${ sql(string) }` | for identifiers | ``await sql`SELECT * FROM ${sql('table_name')` `` | +| `${ sql([] or {}, ...) }` | for helpers | ``await sql`INSERT INTO users ${sql({ name: 'Peter'})}` `` | +| `${ 'somevalue' }` | for values | ``await sql`SELECT * FROM users WHERE age = ${42}` `` | ## Advanced query methods @@ -446,7 +475,7 @@ await sql` 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. 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.** - + ### Rows as Array of Values #### ```sql``.values()``` @@ -473,7 +502,7 @@ const result = await sql.file('query.sql', ['Murray', 68]) ### Multiple statements in one query #### ```await sql``.simple()``` -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 +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 ```sql``.simple()```. That will create it as a simple query. ```js @@ -515,8 +544,8 @@ await pipeline(readableStream, createWriteStream('output.tsv')) ```js const readableStream = await sql` copy ( - select name, age - from users + select name, age + from users where age = 68 ) to stdout `.readable() @@ -525,7 +554,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 @@ -555,7 +584,9 @@ If you know what you're doing, you can use `unsafe` to pass any string you'd lik ```js 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 @@ -595,7 +626,7 @@ const [user, account] = await sql.begin(async sql => { ) values ( 'Murray' ) - returning * + returning * ` const [account] = await sql` @@ -604,7 +635,7 @@ const [user, account] = await sql.begin(async sql => { ) values ( ${ user.user_id } ) - returning * + returning * ` return [user, account] @@ -672,7 +703,7 @@ sql.begin('read write', async sql => { 'Murray' ) ` - + await sql.prepare('tx1') }) ``` @@ -732,7 +763,7 @@ console.log(data) // [ { a_test: 1 } ] ### Transform `undefined` Values -By default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed +By default, Postgres.js will throw the error `UNDEFINED_VALUE: Undefined values are not allowed` when undefined values are passed ```js // Transform the column names to and from camel case @@ -813,7 +844,7 @@ The optional `onlisten` method is great to use for a very simply queue mechanism ```js await sql.listen( - 'jobs', + 'jobs', (x) => run(JSON.parse(x)), ( ) => sql`select unfinished_jobs()`.forEach(run) ) @@ -846,7 +877,7 @@ CREATE PUBLICATION alltables FOR ALL TABLES const sql = postgres({ publications: 'alltables' }) const { unsubscribe } = await sql.subscribe( - 'insert:events', + 'insert:events', (row, { command, relation, key, old }) => { // Callback function for each row change // tell about new event row over eg. websockets or do something else @@ -905,7 +936,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 @@ -971,7 +1002,7 @@ const sql = postgres('postgres://username:password@host:port/database', { }, connection : { application_name : 'postgres.js', // Default application_name - ... // Other connection parameters + ... // Other connection parameters, see https://www.postgresql.org/docs/current/runtime-config-client.html }, target_session_attrs : null, // Use 'read-write' with multiple hosts to // ensure only connecting to primary @@ -980,7 +1011,20 @@ 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 + +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. + +```js +const sql = postgres(url, { + // Other connection config + ... + // Password function for the database user + password : async () => await signer.getAuthToken(), +}) +``` ### SSL @@ -1078,10 +1122,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 @@ -1100,20 +1144,25 @@ 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 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.)_ @@ -1140,7 +1189,7 @@ const sql = postgres({ }) // Now you can use sql.typed.rect() as specified above -const [custom] = sql` +const [custom] = await sql` insert into rectangles ( name, rect @@ -1170,8 +1219,8 @@ const sql = postgres({ const ssh = new ssh2.Client() ssh .on('error', reject) - .on('ready', () => - ssh.forwardOut('127.0.0.1', 12345, host, port, + .on('ready', () => + ssh.forwardOut('127.0.0.1', 12345, host, port, (err, socket) => err ? reject(err) : resolve(socket) ) ) @@ -1273,8 +1322,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`. diff --git a/deno/src/connection.js b/deno/src/connection.js index 95e73dda..a3f43c48 100644 --- a/deno/src/connection.js +++ b/deno/src/connection.js @@ -296,7 +296,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose if (incomings) { incomings.push(x) remaining -= x.length - if (remaining >= 0) + if (remaining > 0) return } @@ -388,13 +388,20 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - query.reject(Object.create(err, { + if (query.reserve) + return query.reject(err) + + if (!err || typeof err !== 'object') + err = new Error(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 }, args: { value: query.args, enumerable: options.debug }, types: { value: query.statement && query.statement.types, enumerable: options.debug } - })) + }) + query.reject(err) } function end() { @@ -431,10 +438,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() @@ -443,7 +448,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose closedDate = performance.now() hadError && options.shared.retries++ delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000 - onclose(connection) + onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket)) } /* Handlers */ @@ -535,11 +540,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose return terminate() } - if (needsTypes) + if (needsTypes) { + initial.reserve && (initial = null) return fetchArrayTypes() + } - execute(initial) - options.shared.retries = retries = initial = 0 + initial && !initial.reserve && execute(initial) + options.shared.retries = retries = 0 + initial = null return } @@ -659,27 +667,30 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose /* c8 ignore next 5 */ async function AuthenticationCleartextPassword() { + const payload = await Pass() write( - b().p().str(await Pass()).z(1).end() + b().p().str(payload).z(1).end() ) } async function AuthenticationMD5Password(x) { - write( - b().p().str( - 'md5' + - (await md5(Buffer.concat([ + const payload = 'md5' + ( + await md5( + Buffer.concat([ Buffer.from(await md5((await Pass()) + user)), x.subarray(9) - ]))) - ).z(1).end() + ]) + ) + ) + write( + b().p().str(payload).z(1).end() ) } async function SASL() { + nonce = (await crypto.randomBytes(18)).toString('base64') b().p().str('SCRAM-SHA-256' + b.N) const i = b.i - nonce = (await crypto.randomBytes(18)).toString('base64') write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end()) } @@ -701,12 +712,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64') + const payload = 'c=biws,r=' + res.r + ',p=' + xor( + clientKey, Buffer.from(await hmac(await sha256(clientKey), auth)) + ).toString('base64') + write( - b().p().str( - 'c=biws,r=' + res.r + ',p=' + xor( - clientKey, Buffer.from(await hmac(await sha256(clientKey), auth)) - ).toString('base64') - ).end() + b().p().str(payload).end() ) } @@ -786,7 +797,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/deno/src/index.js b/deno/src/index.js index fada05ae..aa7a920f 100644 --- a/deno/src/index.js +++ b/deno/src/index.js @@ -205,9 +205,10 @@ function Postgres(a, b) { const queue = Queue() const c = open.length ? open.shift() - : await new Promise(r => { - queries.push({ reserve: r }) - closed.length && connect(closed.shift()) + : await new Promise((resolve, reject) => { + const query = { reserve: resolve, reject } + queries.push(query) + closed.length && connect(closed.shift(), query) }) move(c, reserved) @@ -240,7 +241,10 @@ function Postgres(a, b) { try { await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute() - return await scope(connection, fn) + return await Promise.race([ + scope(connection, fn), + new Promise((_, reject) => connection.onclose = reject) + ]) } catch (error) { throw error } @@ -415,9 +419,10 @@ function Postgres(a, b) { : move(c, full) } - function onclose(c) { + function onclose(c, e) { move(c, closed) c.reserved = null + c.onclose && (c.onclose(e), c.onclose = null) options.onclose && options.onclose(c.id) queries.length && connect(c, queries.shift()) } @@ -438,6 +443,7 @@ function parseOptions(a, b) { o.no_prepare && (o.prepare = false) query.sslmode && (query.ssl = query.sslmode, delete query.sslmode) 'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line + query.sslrootcert === 'system' && (query.ssl = 'verify-full') const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive'] const defaults = { @@ -476,8 +482,8 @@ function parseOptions(a, b) { {} ), connection : { - application_name: 'postgres.js', ...o.connection, + application_name: o.connection?.application_name ?? env.PGAPPNAME ?? 'postgres.js', ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {}) }, types : o.types || {}, diff --git a/deno/src/subscribe.js b/deno/src/subscribe.js index dbb9b971..b20efb96 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 } }) } @@ -104,14 +105,16 @@ 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() + } } function handle(a, b) { diff --git a/deno/tests/index.js b/deno/tests/index.js index 08a0c023..adedf1e0 100644 --- a/deno/tests/index.js +++ b/deno/tests/index.js @@ -431,6 +431,30 @@ t('Reconnect using SSL', { timeout: 2 }, async() => { return [1, (await sql`select 1 as x`)[0].x] }) +t('Proper handling of non object Errors', async() => { + const sql = postgres({ socket: () => { throw 'wat' } }) // eslint-disable-line + + return [ + 'wat', await sql`select 1 as x`.catch(e => e.message) + ] +}) + +t('Proper handling of null Errors', async() => { + const sql = postgres({ socket: () => { throw null } }) // eslint-disable-line + + return [ + 'null', await sql`select 1 as x`.catch(e => e.message) + ] +}) + +t('Ensure reserve on connection throws proper error', async() => { + const sql = postgres({ socket: () => { throw 'wat' }, idle_timeout }) // eslint-disable-line + + return [ + 'wat', await sql.reserve().catch(e => e) + ] +}) + t('Login without password', async() => { return [true, (await postgres({ ...options, ...login })`select true as x`)[0].x] }) @@ -1791,6 +1815,32 @@ 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) + + return ['transformAssignedExpr', routine, await sql`drop table x`] +}) + +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'))` + ]).catch(e => e) + + 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' }) @@ -2136,7 +2186,7 @@ t('Execute', async() => { t('Cancel running query', async() => { const query = sql`select pg_sleep(2)` - setTimeout(() => query.cancel(), 200) + setTimeout(() => query.cancel(), 500) const error = await query.catch(x => x) return ['57014', error.code] }) @@ -2350,6 +2400,17 @@ t('Ensure reconnect after max_lifetime with transactions', { timeout: 5 }, async return [true, true] }) + +t('Ensure transactions throw if connection is closed dwhile there is no query', async() => { + const sql = postgres(options) + const x = await sql.begin(async() => { + setTimeout(() => sql.end({ timeout: 0 }), 10) + await new Promise(r => setTimeout(r, 200)) + return sql`select 1` + }).catch(x => x) + return ['CONNECTION_CLOSED', x.code] +}) + t('Custom socket', {}, async() => { let result const sql = postgres({ @@ -2535,4 +2596,25 @@ t('reserve connection', async() => { ] }) -;window.addEventListener("unload", () => Deno.exit(process.exitCode)) \ No newline at end of file +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('') + ] +}) + +t('Ensure reserve on query throws proper error', async() => { + const sql = postgres({ idle_timeout }) // eslint-disable-line + const reserved = await sql.reserve() + const [{ x }] = await reserved`select 'wat' as x` + + return [ + 'wat', x, reserved.release() + ] +}) + +;globalThis.addEventListener("unload", () => Deno.exit(process.exitCode)) \ No newline at end of file diff --git a/deno/types/index.d.ts b/deno/types/index.d.ts index 215d5b62..44a07af0 100644 --- a/deno/types/index.d.ts +++ b/deno/types/index.d.ts @@ -331,8 +331,18 @@ declare namespace postgres { * @default 'postgres.js' */ application_name: string; + default_transaction_isolation: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable', + default_transaction_read_only: boolean, + default_transaction_deferrable: boolean, + statement_timeout: number, + lock_timeout: number, + idle_in_transaction_session_timeout: number, + idle_session_timeout: number, + DateStyle: string, + IntervalStyle: string, + TimeZone: string, /** Other connection parameters */ - [name: string]: string; + [name: string]: string | number | boolean; } interface Options> extends Partial> { @@ -448,7 +458,8 @@ declare namespace postgres { | 'NOT_TAGGED_CALL' | 'UNDEFINED_VALUE' | 'MAX_PARAMETERS_EXCEEDED' - | 'SASL_SIGNATURE_MISMATCH'; + | 'SASL_SIGNATURE_MISMATCH' + | 'UNSAFE_TRANSACTION'; message: string; } @@ -591,6 +602,7 @@ declare namespace postgres { type RowList = T & Iterable> & ResultQueryMeta; interface PendingQueryModifiers { + simple(): this; readable(): Promise; writable(): Promise; @@ -682,7 +694,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; diff --git a/package.json b/package.json index c7c8dcde..65157609 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,22 @@ { "name": "postgres", - "version": "3.4.0", + "version": "3.4.7", "description": "Fastest full featured PostgreSQL client for Node.js", "type": "module", "module": "src/index.js", "main": "cjs/src/index.js", "exports": { - "worker": "./cf/src/index.js", "types": "./types/index.d.ts", + "bun": "./src/index.js", + "workerd": "./cf/src/index.js", "import": "./src/index.js", "default": "./cjs/src/index.js" }, "types": "types/index.d.ts", "typings": "types/index.d.ts", + "engines": { + "node": ">=12" + }, "scripts": { "build": "npm run build:cjs && npm run build:deno && npm run build:cf", "build:cjs": "node transpile.cjs", @@ -21,7 +25,7 @@ "test": "npm run test:esm && npm run test:cjs && npm run test:deno", "test:esm": "node tests/index.js", "test:cjs": "npm run build:cjs && cd cjs/tests && node index.js && cd ../../", - "test:deno": "npm run build:deno && cd deno/tests && deno run --unstable --allow-all --unsafely-ignore-certificate-errors index.js && cd ../../", + "test:deno": "npm run build:deno && cd deno/tests && deno run --no-lock --allow-all --unsafely-ignore-certificate-errors index.js && cd ../../", "lint": "eslint src && eslint tests", "prepare": "npm run build", "prepublishOnly": "npm run lint" diff --git a/src/connection.js b/src/connection.js index e8e4881d..c3f554aa 100644 --- a/src/connection.js +++ b/src/connection.js @@ -293,7 +293,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose if (incomings) { incomings.push(x) remaining -= x.length - if (remaining >= 0) + if (remaining > 0) return } @@ -385,13 +385,20 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose } function queryError(query, err) { - query.reject(Object.create(err, { + if (query.reserve) + return query.reject(err) + + if (!err || typeof err !== 'object') + err = new Error(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 }, args: { value: query.args, enumerable: options.debug }, types: { value: query.statement && query.statement.types, enumerable: options.debug } - })) + }) + query.reject(err) } function end() { @@ -428,10 +435,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() @@ -440,7 +445,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose closedDate = performance.now() hadError && options.shared.retries++ delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000 - onclose(connection) + onclose(connection, Errors.connection('CONNECTION_CLOSED', options, socket)) } /* Handlers */ @@ -532,11 +537,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose return terminate() } - if (needsTypes) + if (needsTypes) { + initial.reserve && (initial = null) return fetchArrayTypes() + } - execute(initial) - options.shared.retries = retries = initial = 0 + initial && !initial.reserve && execute(initial) + options.shared.retries = retries = 0 + initial = null return } @@ -656,27 +664,30 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose /* c8 ignore next 5 */ async function AuthenticationCleartextPassword() { + const payload = await Pass() write( - b().p().str(await Pass()).z(1).end() + b().p().str(payload).z(1).end() ) } async function AuthenticationMD5Password(x) { - write( - b().p().str( - 'md5' + - (await md5(Buffer.concat([ + const payload = 'md5' + ( + await md5( + Buffer.concat([ Buffer.from(await md5((await Pass()) + user)), x.subarray(9) - ]))) - ).z(1).end() + ]) + ) + ) + write( + b().p().str(payload).z(1).end() ) } async function SASL() { + nonce = (await crypto.randomBytes(18)).toString('base64') b().p().str('SCRAM-SHA-256' + b.N) const i = b.i - nonce = (await crypto.randomBytes(18)).toString('base64') write(b.inc(4).str('n,,n=*,r=' + nonce).i32(b.i - i - 4, i).end()) } @@ -698,12 +709,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose serverSignature = (await hmac(await hmac(saltedPassword, 'Server Key'), auth)).toString('base64') + const payload = 'c=biws,r=' + res.r + ',p=' + xor( + clientKey, Buffer.from(await hmac(await sha256(clientKey), auth)) + ).toString('base64') + write( - b().p().str( - 'c=biws,r=' + res.r + ',p=' + xor( - clientKey, Buffer.from(await hmac(await sha256(clientKey), auth)) - ).toString('base64') - ).end() + b().p().str(payload).end() ) } @@ -783,7 +794,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/src/index.js b/src/index.js index ff990586..944d50cf 100644 --- a/src/index.js +++ b/src/index.js @@ -204,9 +204,10 @@ function Postgres(a, b) { const queue = Queue() const c = open.length ? open.shift() - : await new Promise(r => { - queries.push({ reserve: r }) - closed.length && connect(closed.shift()) + : await new Promise((resolve, reject) => { + const query = { reserve: resolve, reject } + queries.push(query) + closed.length && connect(closed.shift(), query) }) move(c, reserved) @@ -239,7 +240,10 @@ function Postgres(a, b) { try { await sql.unsafe('begin ' + options.replace(/[^a-z ]/ig, ''), [], { onexecute }).execute() - return await scope(connection, fn) + return await Promise.race([ + scope(connection, fn), + new Promise((_, reject) => connection.onclose = reject) + ]) } catch (error) { throw error } @@ -414,9 +418,10 @@ function Postgres(a, b) { : move(c, full) } - function onclose(c) { + function onclose(c, e) { move(c, closed) c.reserved = null + c.onclose && (c.onclose(e), c.onclose = null) options.onclose && options.onclose(c.id) queries.length && connect(c, queries.shift()) } @@ -437,6 +442,7 @@ function parseOptions(a, b) { o.no_prepare && (o.prepare = false) query.sslmode && (query.ssl = query.sslmode, delete query.sslmode) 'timeout' in o && (console.log('The timeout option is deprecated, use idle_timeout instead'), o.idle_timeout = o.timeout) // eslint-disable-line + query.sslrootcert === 'system' && (query.ssl = 'verify-full') const ints = ['idle_timeout', 'connect_timeout', 'max_lifetime', 'max_pipeline', 'backoff', 'keep_alive'] const defaults = { @@ -475,7 +481,7 @@ function parseOptions(a, b) { {} ), connection : { - application_name: 'postgres.js', + application_name: env.PGAPPNAME || 'postgres.js', ...o.connection, ...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {}) }, diff --git a/src/subscribe.js b/src/subscribe.js index 7a70842e..4f8934cc 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 } }) } @@ -103,14 +104,16 @@ 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() + } } function handle(a, b) { diff --git a/tests/index.js b/tests/index.js index 499b3fbd..07ff98ed 100644 --- a/tests/index.js +++ b/tests/index.js @@ -429,6 +429,30 @@ t('Reconnect using SSL', { timeout: 2 }, async() => { return [1, (await sql`select 1 as x`)[0].x] }) +t('Proper handling of non object Errors', async() => { + const sql = postgres({ socket: () => { throw 'wat' } }) // eslint-disable-line + + return [ + 'wat', await sql`select 1 as x`.catch(e => e.message) + ] +}) + +t('Proper handling of null Errors', async() => { + const sql = postgres({ socket: () => { throw null } }) // eslint-disable-line + + return [ + 'null', await sql`select 1 as x`.catch(e => e.message) + ] +}) + +t('Ensure reserve on connection throws proper error', async() => { + const sql = postgres({ socket: () => { throw 'wat' }, idle_timeout }) // eslint-disable-line + + return [ + 'wat', await sql.reserve().catch(e => e) + ] +}) + t('Login without password', async() => { return [true, (await postgres({ ...options, ...login })`select true as x`)[0].x] }) @@ -1789,6 +1813,32 @@ 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) + + return ['transformAssignedExpr', routine, await sql`drop table x`] +}) + +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'))` + ]).catch(e => e) + + 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' }) @@ -2134,7 +2184,7 @@ t('Execute', async() => { t('Cancel running query', async() => { const query = sql`select pg_sleep(2)` - setTimeout(() => query.cancel(), 200) + setTimeout(() => query.cancel(), 500) const error = await query.catch(x => x) return ['57014', error.code] }) @@ -2348,6 +2398,17 @@ t('Ensure reconnect after max_lifetime with transactions', { timeout: 5 }, async return [true, true] }) + +t('Ensure transactions throw if connection is closed dwhile there is no query', async() => { + const sql = postgres(options) + const x = await sql.begin(async() => { + setTimeout(() => sql.end({ timeout: 0 }), 10) + await new Promise(r => setTimeout(r, 200)) + return sql`select 1` + }).catch(x => x) + return ['CONNECTION_CLOSED', x.code] +}) + t('Custom socket', {}, async() => { let result const sql = postgres({ @@ -2532,3 +2593,24 @@ 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('') + ] +}) + +t('Ensure reserve on query throws proper error', async() => { + const sql = postgres({ idle_timeout }) // eslint-disable-line + const reserved = await sql.reserve() + const [{ x }] = await reserved`select 'wat' as x` + + return [ + 'wat', x, reserved.release() + ] +}) 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') diff --git a/types/index.d.ts b/types/index.d.ts index 8dacd9c4..eb604918 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -329,8 +329,18 @@ declare namespace postgres { * @default 'postgres.js' */ application_name: string; + default_transaction_isolation: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable', + default_transaction_read_only: boolean, + default_transaction_deferrable: boolean, + statement_timeout: number, + lock_timeout: number, + idle_in_transaction_session_timeout: number, + idle_session_timeout: number, + DateStyle: string, + IntervalStyle: string, + TimeZone: string, /** Other connection parameters */ - [name: string]: string; + [name: string]: string | number | boolean; } interface Options> extends Partial> { @@ -446,7 +456,8 @@ declare namespace postgres { | 'NOT_TAGGED_CALL' | 'UNDEFINED_VALUE' | 'MAX_PARAMETERS_EXCEEDED' - | 'SASL_SIGNATURE_MISMATCH'; + | 'SASL_SIGNATURE_MISMATCH' + | 'UNSAFE_TRANSACTION'; message: string; } @@ -589,6 +600,7 @@ declare namespace postgres { type RowList = T & Iterable> & ResultQueryMeta; interface PendingQueryModifiers { + simple(): this; readable(): Promise; writable(): Promise; @@ -680,7 +692,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;