Skip to content

Commit e492370

Browse files
committed
Add simple
1 parent c7dff0c commit e492370

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

lib/connection.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ export default function Connection(options = {}) {
100100
? queries.push(query)
101101
: (backend.query = query)
102102

103-
const buffer = statements.has(sig)
104-
? prepared(statements.get(sig), args, query)
105-
: prepare(sig, str, args, query)
103+
const buffer = query.simple
104+
? frontend.Query(str)
105+
: statements.has(sig)
106+
? prepared(statements.get(sig), args, query)
107+
: prepare(sig, str, args, query)
106108

107109
connection.ready
108110
? socket.write(buffer)

lib/index.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function Postgres(url, options) {
3737
, typeArrayMap = {}
3838

3939
function postgres(xs, ...args) {
40-
return query(false, getConnection(), xs, args)
40+
return query({}, getConnection(), xs, args)
4141
}
4242

4343
Object.assign(postgres, {
@@ -93,7 +93,7 @@ export default function Postgres(url, options) {
9393
}, connection)
9494
})
9595

96-
query(true, connection, begin || savepoint)
96+
query({ raw: true }, connection, begin || savepoint)
9797
.then(() => {
9898
const result = fn(scoped)
9999
return Array.isArray(result)
@@ -106,7 +106,7 @@ export default function Postgres(url, options) {
106106
: resolve(x)
107107
)
108108
.catch((err) => {
109-
query(true, connection,
109+
query({ raw: true }, connection,
110110
begin
111111
? 'rollback'
112112
: 'rollback to ' + savepoint
@@ -119,7 +119,7 @@ export default function Postgres(url, options) {
119119
})
120120

121121
function scoped(xs, ...args) {
122-
return query(false, connection, xs, args).catch(reject)
122+
return query({}, connection, xs, args).catch(reject)
123123
}
124124
}
125125

@@ -133,11 +133,11 @@ export default function Postgres(url, options) {
133133
}
134134
}
135135

136-
function query(raw, connection, xs, args) {
136+
function query({ raw, simple }, connection, xs, args) {
137137
if (!raw && (!Array.isArray(xs) || !Array.isArray(xs.raw)))
138138
throw errors.generic({ message: 'Query not called as a tagged template literal', code: 'NOT_TAGGED_CALL' })
139139

140-
const query = { raw }
140+
const query = { raw, simple }
141141

142142
const promise = new Promise((resolve, reject) => {
143143
query.resolve = resolve
@@ -184,7 +184,7 @@ export default function Postgres(url, options) {
184184
const container = fn(scoped)
185185
function scoped(xs, ...args) {
186186
queries++
187-
const promise = query(false, connection, xs, args)
187+
const promise = query({}, connection, xs, args)
188188
promise.then(finished, finished)
189189
return promise
190190
}
@@ -255,14 +255,26 @@ export default function Postgres(url, options) {
255255

256256
function addTypes(sql, connection) {
257257
Object.assign(sql, {
258-
notify: (channel, payload) => sql`select pg_notify(${ channel }, ${ '' + payload })`,
259-
unsafe: (xs, args) => query(true, connection || getConnection(), xs, args),
258+
notify,
259+
unsafe,
260260
array,
261261
rows,
262262
row,
263263
json
264264
})
265265

266+
function notify(channel, payload) {
267+
return sql`select pg_notify(${ channel }, ${ '' + payload })`
268+
}
269+
270+
function unsafe(xs, args, options = {}) {
271+
if (!Array.isArray(args)) {
272+
options = args || []
273+
args = []
274+
}
275+
return query({ raw: true, simple: options.simple }, connection || getConnection(), xs, args)
276+
}
277+
266278
options.types && Object.entries(options.types).forEach(([name, type]) => {
267279
if (name in sql)
268280
throw errors.generic({ message: name + ' is a reserved method name', code: 'RESERVED_METHOD_NAME' })
@@ -279,7 +291,7 @@ export default function Postgres(url, options) {
279291
? listeners[x].push(fn)
280292
: (listeners[x] = [fn])
281293

282-
return query(true, getListener(), 'listen "' + x + '"').then(() => x)
294+
return query({ raw: true}, getListener(), 'listen "' + x + '"').then(() => x)
283295
}
284296

285297
function getListener() {

0 commit comments

Comments
 (0)