Skip to content

Commit a25d159

Browse files
committed
feat: Add onquery callback.
1 parent 94b7170 commit a25d159

File tree

12 files changed

+104
-16
lines changed

12 files changed

+104
-16
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: 2 additions & 0 deletions
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)
@@ -492,6 +493,7 @@ function parseOptions(a, b) {
492493
onnotify : o.onnotify,
493494
onclose : o.onclose,
494495
onparameter : o.onparameter,
496+
onquery : o.onquery,
495497
socket : o.socket,
496498
transform : parseTransform(o.transform || { undefined: undefined }),
497499
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: 2 additions & 0 deletions
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)
@@ -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 : {},

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/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: 2 additions & 0 deletions
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)
@@ -492,6 +493,7 @@ function parseOptions(a, b) {
492493
onnotify : o.onnotify,
493494
onclose : o.onclose,
494495
onparameter : o.onparameter,
496+
onquery : o.onquery,
495497
socket : o.socket,
496498
transform : parseTransform(o.transform || { undefined: undefined }),
497499
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() {

src/connection.js

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

171171
build(q)
172+
q.onquery && (q.onquery = q.onquery(q))
172173
return write(toBuffer(q))
173174
&& !q.describeFirst
174175
&& !q.cursorFn

0 commit comments

Comments
 (0)