Skip to content

Commit dd2a825

Browse files
committed
Fix unsafe and file queries
1 parent 0a80cbc commit dd2a825

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

lib/index.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -250,21 +250,25 @@ function Postgres(url, options) {
250250
return sql`select pg_notify(${ channel }, ${ '' + payload })`
251251
}
252252

253-
function unsafe(xs, args, options = {}) {
253+
function unsafe(xs, args) {
254+
return query({ raw: true, simple: !args }, connection || getConnection(), xs, args || [])
255+
}
256+
257+
function file(path, args, options) {
254258
if (!Array.isArray(args)) {
255-
options = args || []
256-
args = []
259+
options = args || {}
260+
args = null
257261
}
258-
return query({ raw: true, simple: options.simple }, connection || getConnection(), xs, args)
259-
}
260262

261-
function file(path) {
263+
if ('cache' in options === false)
264+
options.cache = true
265+
262266
const file = files[path]
267+
const q = { raw: true, simple: !args }
263268

264-
if (typeof file === 'string')
265-
return query({ raw: true, simple: true }, connection || getConnection(), files[path])
269+
if (options.cache && typeof file === 'string')
270+
return query(q, connection || getConnection(), file, args || [])
266271

267-
const q = { raw: true, simple: true }
268272
const promise = (file || (files[path] = new Promise((resolve, reject) => {
269273
fs.readFile(path, 'utf8', (err, str) => {
270274
if (err)
@@ -273,7 +277,7 @@ function Postgres(url, options) {
273277
files[path] = str
274278
resolve(str)
275279
})
276-
}))).then((str) => query(q, connection || getConnection(), str))
280+
}))).then(str => query(q, connection || getConnection(), str, args || []))
277281

278282
promise.stream = fn => (q.stream = fn, promise)
279283

tests/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ t('unsafe', async() => {
337337
}, () => sql`drop table test`)
338338

339339
t('unsafe simple', async() => {
340-
return [1, (await sql.unsafe('select 1 as x', { simple: true }))[0].x]
340+
return [1, (await sql.unsafe('select 1 as x'))[0].x]
341341
})
342342

343343
t('listen and notify', async() => {
@@ -521,7 +521,7 @@ t('Multiple statements', async() =>
521521
[2, await sql.unsafe(`
522522
select 1 as x;
523523
select 2 as x;
524-
`, { simple: true }).then(([, x]) => x.x)]
524+
`).then(([, x]) => x.x)]
525525
)
526526

527527
t('throws correct error when authentication fails', async() => {

0 commit comments

Comments
 (0)