Skip to content

Commit d8d2daf

Browse files
committed
feat: Add onquery callback.
1 parent 089214e commit d8d2daf

File tree

13 files changed

+123
-30
lines changed

13 files changed

+123
-30
lines changed

cf/src/connection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
167167
: (query = q, query.active = true)
168168

169169
build(q)
170+
q.onquery && (q.onquery = q.onquery(q))
170171
return write(toBuffer(q))
171172
&& !q.describeFirst
172173
&& !q.cursorFn

cf/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ function Postgres(a, b) {
8686

8787
function Sql(handler) {
8888
handler.debug = options.debug
89+
handler.onquery = options.onquery
8990

9091
Object.entries(options.types).reduce((acc, [name, type]) => {
9192
acc[name] = (x) => new Parameter(x, type.to)
@@ -481,7 +482,7 @@ function parseOptions(a, b) {
481482
{}
482483
),
483484
connection : {
484-
application_name: 'postgres.js',
485+
application_name: env.PGAPPNAME || 'postgres.js',
485486
...o.connection,
486487
...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})
487488
},
@@ -491,6 +492,7 @@ function parseOptions(a, b) {
491492
onnotify : o.onnotify,
492493
onclose : o.onclose,
493494
onparameter : o.onparameter,
495+
onquery : o.onquery,
494496
socket : o.socket,
495497
transform : parseTransform(o.transform || { undefined: undefined }),
496498
parameters : {},

cf/src/query.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export class Query extends Promise {
1313
reject = b
1414
})
1515

16+
this.resolver = resolve
17+
this.rejecter = reject
18+
1619
this.tagged = Array.isArray(strings.raw)
1720
this.strings = strings
1821
this.args = args
@@ -23,19 +26,29 @@ export class Query extends Promise {
2326
this.state = null
2427
this.statement = null
2528

26-
this.resolve = x => (this.active = false, resolve(x))
27-
this.reject = x => (this.active = false, reject(x))
28-
2929
this.active = false
3030
this.cancelled = null
3131
this.executed = false
3232
this.signature = ''
33+
this.onquery = this.handler.onquery
3334

3435
this[originError] = this.handler.debug
3536
? new Error()
3637
: this.tagged && cachedError(this.strings)
3738
}
3839

40+
resolve(x) {
41+
this.active = false
42+
this.onquery && (this.onquery = this.onquery(x))
43+
this.resolver(x)
44+
}
45+
46+
reject(x) {
47+
this.active = false
48+
this.onquery && (this.onquery = this.onquery(x))
49+
this.rejecter(x)
50+
}
51+
3952
get origin() {
4053
return (this.handler.debug
4154
? this[originError].stack
@@ -137,7 +150,13 @@ export class Query extends Promise {
137150
}
138151

139152
async handle() {
140-
!this.executed && (this.executed = true) && await 1 && this.handler(this)
153+
if (this.executed)
154+
return
155+
156+
this.executed = true
157+
await 1
158+
this.onquery && (this.onquery = this.onquery(this))
159+
this.handler(this)
141160
}
142161

143162
execute() {

cjs/src/connection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
165165
: (query = q, query.active = true)
166166

167167
build(q)
168+
q.onquery && (q.onquery = q.onquery(q))
168169
return write(toBuffer(q))
169170
&& !q.describeFirst
170171
&& !q.cursorFn

cjs/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ function Postgres(a, b) {
8585

8686
function Sql(handler) {
8787
handler.debug = options.debug
88+
handler.onquery = options.onquery
8889

8990
Object.entries(options.types).reduce((acc, [name, type]) => {
9091
acc[name] = (x) => new Parameter(x, type.to)
@@ -480,7 +481,7 @@ function parseOptions(a, b) {
480481
{}
481482
),
482483
connection : {
483-
application_name: 'postgres.js',
484+
application_name: env.PGAPPNAME || 'postgres.js',
484485
...o.connection,
485486
...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})
486487
},
@@ -490,6 +491,7 @@ function parseOptions(a, b) {
490491
onnotify : o.onnotify,
491492
onclose : o.onclose,
492493
onparameter : o.onparameter,
494+
onquery : o.onquery,
493495
socket : o.socket,
494496
transform : parseTransform(o.transform || { undefined: undefined }),
495497
parameters : {},

cjs/src/query.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const Query = module.exports.Query = class Query extends Promise {
1313
reject = b
1414
})
1515

16+
this.resolver = resolve
17+
this.rejecter = reject
18+
1619
this.tagged = Array.isArray(strings.raw)
1720
this.strings = strings
1821
this.args = args
@@ -23,19 +26,29 @@ const Query = module.exports.Query = class Query extends Promise {
2326
this.state = null
2427
this.statement = null
2528

26-
this.resolve = x => (this.active = false, resolve(x))
27-
this.reject = x => (this.active = false, reject(x))
28-
2929
this.active = false
3030
this.cancelled = null
3131
this.executed = false
3232
this.signature = ''
33+
this.onquery = this.handler.onquery
3334

3435
this[originError] = this.handler.debug
3536
? new Error()
3637
: this.tagged && cachedError(this.strings)
3738
}
3839

40+
resolve(x) {
41+
this.active = false
42+
this.onquery && (this.onquery = this.onquery(x))
43+
this.resolver(x)
44+
}
45+
46+
reject(x) {
47+
this.active = false
48+
this.onquery && (this.onquery = this.onquery(x))
49+
this.rejecter(x)
50+
}
51+
3952
get origin() {
4053
return (this.handler.debug
4154
? this[originError].stack
@@ -137,7 +150,13 @@ const Query = module.exports.Query = class Query extends Promise {
137150
}
138151

139152
async handle() {
140-
!this.executed && (this.executed = true) && await 1 && this.handler(this)
153+
if (this.executed)
154+
return
155+
156+
this.executed = true
157+
await 1
158+
this.onquery && (this.onquery = this.onquery(this))
159+
this.handler(this)
141160
}
142161

143162
execute() {

deno/README.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,20 +1121,25 @@ It is also possible to connect to the database without a connection string or an
11211121
const sql = postgres()
11221122
```
11231123

1124-
| Option | Environment Variables |
1125-
| ----------------- | ------------------------ |
1126-
| `host` | `PGHOST` |
1127-
| `port` | `PGPORT` |
1128-
| `database` | `PGDATABASE` |
1129-
| `username` | `PGUSERNAME` or `PGUSER` |
1130-
| `password` | `PGPASSWORD` |
1131-
| `idle_timeout` | `PGIDLE_TIMEOUT` |
1132-
| `connect_timeout` | `PGCONNECT_TIMEOUT` |
1124+
| Option | Environment Variables |
1125+
| ------------------ | ------------------------ |
1126+
| `host` | `PGHOST` |
1127+
| `port` | `PGPORT` |
1128+
| `database` | `PGDATABASE` |
1129+
| `username` | `PGUSERNAME` or `PGUSER` |
1130+
| `password` | `PGPASSWORD` |
1131+
| `application_name` | `PGAPPNAME` |
1132+
| `idle_timeout` | `PGIDLE_TIMEOUT` |
1133+
| `connect_timeout` | `PGCONNECT_TIMEOUT` |
11331134

11341135
### Prepared statements
11351136

11361137
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).
11371138

1139+
**update**: [since 1.21.0](https://www.pgbouncer.org/2023/10/pgbouncer-1-21-0)
1140+
PGBouncer supports protocol-level named prepared statements when [configured
1141+
properly](https://www.pgbouncer.org/config.html#max_prepared_statements)
1142+
11381143
## Custom Types
11391144

11401145
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.)_
@@ -1294,8 +1299,8 @@ This error is thrown if the user has called [`sql.end()`](#teardown--cleanup) an
12941299
12951300
This error is thrown for any queries that were pending when the timeout to [`sql.end({ timeout: X })`](#teardown--cleanup) was reached.
12961301

1297-
##### CONNECTION_CONNECT_TIMEOUT
1298-
> write CONNECTION_CONNECT_TIMEOUT host:port
1302+
##### CONNECT_TIMEOUT
1303+
> write CONNECT_TIMEOUT host:port
12991304
13001305
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`.
13011306

deno/src/connection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
168168
: (query = q, query.active = true)
169169

170170
build(q)
171+
q.onquery && (q.onquery = q.onquery(q))
171172
return write(toBuffer(q))
172173
&& !q.describeFirst
173174
&& !q.cursorFn

deno/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ function Postgres(a, b) {
8686

8787
function Sql(handler) {
8888
handler.debug = options.debug
89+
handler.onquery = options.onquery
8990

9091
Object.entries(options.types).reduce((acc, [name, type]) => {
9192
acc[name] = (x) => new Parameter(x, type.to)
@@ -481,7 +482,7 @@ function parseOptions(a, b) {
481482
{}
482483
),
483484
connection : {
484-
application_name: 'postgres.js',
485+
application_name: env.PGAPPNAME || 'postgres.js',
485486
...o.connection,
486487
...Object.entries(query).reduce((acc, [k, v]) => (k in defaults || (acc[k] = v), acc), {})
487488
},
@@ -491,6 +492,7 @@ function parseOptions(a, b) {
491492
onnotify : o.onnotify,
492493
onclose : o.onclose,
493494
onparameter : o.onparameter,
495+
onquery : o.onquery,
494496
socket : o.socket,
495497
transform : parseTransform(o.transform || { undefined: undefined }),
496498
parameters : {},

deno/src/query.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export class Query extends Promise {
1313
reject = b
1414
})
1515

16+
this.resolver = resolve
17+
this.rejecter = reject
18+
1619
this.tagged = Array.isArray(strings.raw)
1720
this.strings = strings
1821
this.args = args
@@ -23,19 +26,29 @@ export class Query extends Promise {
2326
this.state = null
2427
this.statement = null
2528

26-
this.resolve = x => (this.active = false, resolve(x))
27-
this.reject = x => (this.active = false, reject(x))
28-
2929
this.active = false
3030
this.cancelled = null
3131
this.executed = false
3232
this.signature = ''
33+
this.onquery = this.handler.onquery
3334

3435
this[originError] = this.handler.debug
3536
? new Error()
3637
: this.tagged && cachedError(this.strings)
3738
}
3839

40+
resolve(x) {
41+
this.active = false
42+
this.onquery && (this.onquery = this.onquery(x))
43+
this.resolver(x)
44+
}
45+
46+
reject(x) {
47+
this.active = false
48+
this.onquery && (this.onquery = this.onquery(x))
49+
this.rejecter(x)
50+
}
51+
3952
get origin() {
4053
return (this.handler.debug
4154
? this[originError].stack
@@ -137,7 +150,13 @@ export class Query extends Promise {
137150
}
138151

139152
async handle() {
140-
!this.executed && (this.executed = true) && await 1 && this.handler(this)
153+
if (this.executed)
154+
return
155+
156+
this.executed = true
157+
await 1
158+
this.onquery && (this.onquery = this.onquery(this))
159+
this.handler(this)
141160
}
142161

143162
execute() {

0 commit comments

Comments
 (0)