Skip to content

Commit ce14949

Browse files
committed
Add no_prepare option - fixes #93 and #76
1 parent 806cc2c commit ce14949

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const sql = postgres('postgres://username:password@host:port/database', {
5454
max : 10, // Max number of connections
5555
idle_timeout : 0, // Idle connection timeout in seconds
5656
connect_timeout : 30, // Connect timeout in seconds
57+
no_prepare : false, // No automatic creation of prepared statements
5758
types : [], // Array of custom types, see more below
5859
onnotice : fn // Defaults to console.log
5960
onparameter : fn // (key, value) when server param change
@@ -532,6 +533,10 @@ Any query which was already sent over the wire will be rejected if the connectio
532533

533534
There are no guarantees about queries executing in order unless using a transaction with `sql.begin()` or setting `max: 1`. Of course doing a series of queries, one awaiting the other will work as expected, but that's just due to the nature of js async/promise handling, so it's not necessary for this library to be concerned with ordering.
534535

536+
## Prepared statements
537+
538+
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 `no_prepare` option. For instance — this is useful when [using PGBouncer in `transaction mode`](https://github.com/porsager/postgres/issues/93).
539+
535540
<details><summary><code>sql.unsafe</code> - Advanced unsafe use cases</summary>
536541

537542
### Unsafe queries `sql.unsafe(query, [args], [options]) -> promise`

lib/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ function Postgres(a, b) {
167167

168168
function query(query, connection, xs, args) {
169169
query.origin = options.debug ? new Error().stack : cachedError(xs)
170+
query.dynamic = query.dynamic || options.no_prepare
170171
if (!query.raw && (!Array.isArray(xs) || !Array.isArray(xs.raw)))
171172
return nested(xs, args)
172173

@@ -238,7 +239,7 @@ function Postgres(a, b) {
238239
function fetchArrayTypes(connection) {
239240
return arrayTypesPromise || (arrayTypesPromise =
240241
new Promise((resolve, reject) => {
241-
send(connection, { resolve, reject, raw: true }, `
242+
send(connection, { resolve, reject, raw: true, dynamic: true }, `
242243
select b.oid, b.typarray
243244
from pg_catalog.pg_type a
244245
left join pg_catalog.pg_type b on b.oid = a.typelem
@@ -528,10 +529,11 @@ function parseOptions(a, b) {
528529
ssl : o.ssl || url.ssl || false,
529530
idle_timeout : o.idle_timeout || url.query.idle_timeout || env.PGIDLE_TIMEOUT || warn(o.timeout),
530531
connect_timeout : o.connect_timeout || url.query.connect_timeout || env.PGCONNECT_TIMEOUT || 30,
532+
no_prepare : o.no_prepare,
531533
onnotice : o.onnotice,
532534
onparameter : o.onparameter,
533535
transform : Object.assign({}, o.transform),
534-
connection : Object.assign({ application_name: 'postgres.js' }, o.connection),
536+
connection : Object.assign({ application_name: 'postgres.js' }, o.connection),
535537
debug : o.debug
536538
},
537539
mergeUserTypes(o.types)

tests/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -1048,3 +1048,15 @@ t('Insert array in sql()', async() => {
10481048
await sql`drop table tester`
10491049
]
10501050
})
1051+
1052+
t('Automatically creates prepared statements', async() => {
1053+
const sql = postgres({ no_prepare: false })
1054+
const result = await sql`select * from pg_prepared_statements`
1055+
return [result[0].statement, 'select * from pg_prepared_statements']
1056+
})
1057+
1058+
t('no_prepare: true disables prepared transactions', async() => {
1059+
const sql = postgres({ no_prepare: true })
1060+
const result = await sql`select * from pg_prepared_statements`
1061+
return [0, result.count]
1062+
})

0 commit comments

Comments
 (0)