Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add onquery callback. #1051

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cf/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
: (query = q, query.active = true)

build(q)
q.onquery && (q.onquery = q.onquery(q))
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
Expand Down
4 changes: 3 additions & 1 deletion cf/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function Postgres(a, b) {

function Sql(handler) {
handler.debug = options.debug
handler.onquery = options.onquery

Object.entries(options.types).reduce((acc, [name, type]) => {
acc[name] = (x) => new Parameter(x, type.to)
Expand Down Expand Up @@ -481,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), {})
},
Expand All @@ -491,6 +492,7 @@ function parseOptions(a, b) {
onnotify : o.onnotify,
onclose : o.onclose,
onparameter : o.onparameter,
onquery : o.onquery,
socket : o.socket,
transform : parseTransform(o.transform || { undefined: undefined }),
parameters : {},
Expand Down
27 changes: 23 additions & 4 deletions cf/src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export class Query extends Promise {
reject = b
})

this.resolver = resolve
this.rejecter = reject

this.tagged = Array.isArray(strings.raw)
this.strings = strings
this.args = args
Expand All @@ -23,19 +26,29 @@ export class Query extends Promise {
this.state = null
this.statement = null

this.resolve = x => (this.active = false, resolve(x))
this.reject = x => (this.active = false, reject(x))

this.active = false
this.cancelled = null
this.executed = false
this.signature = ''
this.onquery = this.handler.onquery

this[originError] = this.handler.debug
? new Error()
: this.tagged && cachedError(this.strings)
}

resolve(x) {
this.active = false
this.onquery && (this.onquery = this.onquery(x))
this.resolver(x)
}

reject(x) {
this.active = false
this.onquery && (this.onquery = this.onquery(x))
this.rejecter(x)
}

get origin() {
return (this.handler.debug
? this[originError].stack
Expand Down Expand Up @@ -137,7 +150,13 @@ export class Query extends Promise {
}

async handle() {
!this.executed && (this.executed = true) && await 1 && this.handler(this)
if (this.executed)
return

this.executed = true
await 1
this.onquery && (this.onquery = this.onquery(this))
this.handler(this)
}

execute() {
Expand Down
1 change: 1 addition & 0 deletions cjs/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
: (query = q, query.active = true)

build(q)
q.onquery && (q.onquery = q.onquery(q))
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
Expand Down
4 changes: 3 additions & 1 deletion cjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function Postgres(a, b) {

function Sql(handler) {
handler.debug = options.debug
handler.onquery = options.onquery

Object.entries(options.types).reduce((acc, [name, type]) => {
acc[name] = (x) => new Parameter(x, type.to)
Expand Down Expand Up @@ -480,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), {})
},
Expand All @@ -490,6 +491,7 @@ function parseOptions(a, b) {
onnotify : o.onnotify,
onclose : o.onclose,
onparameter : o.onparameter,
onquery : o.onquery,
socket : o.socket,
transform : parseTransform(o.transform || { undefined: undefined }),
parameters : {},
Expand Down
27 changes: 23 additions & 4 deletions cjs/src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const Query = module.exports.Query = class Query extends Promise {
reject = b
})

this.resolver = resolve
this.rejecter = reject

this.tagged = Array.isArray(strings.raw)
this.strings = strings
this.args = args
Expand All @@ -23,19 +26,29 @@ const Query = module.exports.Query = class Query extends Promise {
this.state = null
this.statement = null

this.resolve = x => (this.active = false, resolve(x))
this.reject = x => (this.active = false, reject(x))

this.active = false
this.cancelled = null
this.executed = false
this.signature = ''
this.onquery = this.handler.onquery

this[originError] = this.handler.debug
? new Error()
: this.tagged && cachedError(this.strings)
}

resolve(x) {
this.active = false
this.onquery && (this.onquery = this.onquery(x))
this.resolver(x)
}

reject(x) {
this.active = false
this.onquery && (this.onquery = this.onquery(x))
this.rejecter(x)
}

get origin() {
return (this.handler.debug
? this[originError].stack
Expand Down Expand Up @@ -137,7 +150,13 @@ const Query = module.exports.Query = class Query extends Promise {
}

async handle() {
!this.executed && (this.executed = true) && await 1 && this.handler(this)
if (this.executed)
return

this.executed = true
await 1
this.onquery && (this.onquery = this.onquery(this))
this.handler(this)
}

execute() {
Expand Down
27 changes: 16 additions & 11 deletions deno/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1121,20 +1121,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.)_
Expand Down Expand Up @@ -1294,8 +1299,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`.

Expand Down
1 change: 1 addition & 0 deletions deno/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
: (query = q, query.active = true)

build(q)
q.onquery && (q.onquery = q.onquery(q))
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
Expand Down
4 changes: 3 additions & 1 deletion deno/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function Postgres(a, b) {

function Sql(handler) {
handler.debug = options.debug
handler.onquery = options.onquery

Object.entries(options.types).reduce((acc, [name, type]) => {
acc[name] = (x) => new Parameter(x, type.to)
Expand Down Expand Up @@ -481,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), {})
},
Expand All @@ -491,6 +492,7 @@ function parseOptions(a, b) {
onnotify : o.onnotify,
onclose : o.onclose,
onparameter : o.onparameter,
onquery : o.onquery,
socket : o.socket,
transform : parseTransform(o.transform || { undefined: undefined }),
parameters : {},
Expand Down
27 changes: 23 additions & 4 deletions deno/src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export class Query extends Promise {
reject = b
})

this.resolver = resolve
this.rejecter = reject

this.tagged = Array.isArray(strings.raw)
this.strings = strings
this.args = args
Expand All @@ -23,19 +26,29 @@ export class Query extends Promise {
this.state = null
this.statement = null

this.resolve = x => (this.active = false, resolve(x))
this.reject = x => (this.active = false, reject(x))

this.active = false
this.cancelled = null
this.executed = false
this.signature = ''
this.onquery = this.handler.onquery

this[originError] = this.handler.debug
? new Error()
: this.tagged && cachedError(this.strings)
}

resolve(x) {
this.active = false
this.onquery && (this.onquery = this.onquery(x))
this.resolver(x)
}

reject(x) {
this.active = false
this.onquery && (this.onquery = this.onquery(x))
this.rejecter(x)
}

get origin() {
return (this.handler.debug
? this[originError].stack
Expand Down Expand Up @@ -137,7 +150,13 @@ export class Query extends Promise {
}

async handle() {
!this.executed && (this.executed = true) && await 1 && this.handler(this)
if (this.executed)
return

this.executed = true
await 1
this.onquery && (this.onquery = this.onquery(this))
this.handler(this)
}

execute() {
Expand Down
1 change: 1 addition & 0 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
: (query = q, query.active = true)

build(q)
q.onquery && (q.onquery = q.onquery(q))
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function Postgres(a, b) {

function Sql(handler) {
handler.debug = options.debug
handler.onquery = options.onquery

Object.entries(options.types).reduce((acc, [name, type]) => {
acc[name] = (x) => new Parameter(x, type.to)
Expand Down Expand Up @@ -490,6 +491,7 @@ function parseOptions(a, b) {
onnotify : o.onnotify,
onclose : o.onclose,
onparameter : o.onparameter,
onquery : o.onquery,
socket : o.socket,
transform : parseTransform(o.transform || { undefined: undefined }),
parameters : {},
Expand Down
Loading