diff --git a/cf/src/connection.js b/cf/src/connection.js index 203af80d..37e93020 100644 --- a/cf/src/connection.js +++ b/cf/src/connection.js @@ -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 diff --git a/cf/src/index.js b/cf/src/index.js index 3ffb7e65..23a523cc 100644 --- a/cf/src/index.js +++ b/cf/src/index.js @@ -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) @@ -492,6 +493,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 : {}, diff --git a/cf/src/query.js b/cf/src/query.js index 0d44a15c..877927c7 100644 --- a/cf/src/query.js +++ b/cf/src/query.js @@ -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 @@ -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 @@ -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() { diff --git a/cjs/src/connection.js b/cjs/src/connection.js index 589d3638..6ad50402 100644 --- a/cjs/src/connection.js +++ b/cjs/src/connection.js @@ -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 diff --git a/cjs/src/index.js b/cjs/src/index.js index baf7e60a..f52f7ab7 100644 --- a/cjs/src/index.js +++ b/cjs/src/index.js @@ -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) @@ -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 : {}, diff --git a/cjs/src/query.js b/cjs/src/query.js index 45327f2f..f6c0fc22 100644 --- a/cjs/src/query.js +++ b/cjs/src/query.js @@ -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 @@ -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 @@ -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() { diff --git a/deno/src/connection.js b/deno/src/connection.js index a3f43c48..3df6592f 100644 --- a/deno/src/connection.js +++ b/deno/src/connection.js @@ -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 diff --git a/deno/src/index.js b/deno/src/index.js index aa7a920f..55fe9a8c 100644 --- a/deno/src/index.js +++ b/deno/src/index.js @@ -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) @@ -492,6 +493,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 : {}, diff --git a/deno/src/query.js b/deno/src/query.js index 0d44a15c..877927c7 100644 --- a/deno/src/query.js +++ b/deno/src/query.js @@ -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 @@ -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 @@ -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() { diff --git a/src/connection.js b/src/connection.js index f110cfc1..82df9a56 100644 --- a/src/connection.js +++ b/src/connection.js @@ -169,6 +169,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 diff --git a/src/index.js b/src/index.js index c7fba3da..ff58d6ee 100644 --- a/src/index.js +++ b/src/index.js @@ -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) @@ -492,6 +493,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 : {}, diff --git a/src/query.js b/src/query.js index 0d44a15c..877927c7 100644 --- a/src/query.js +++ b/src/query.js @@ -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 @@ -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 @@ -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() {