Skip to content

Commit de0070a

Browse files
committed
Fix file stream
1 parent e02115b commit de0070a

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

cjs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "peegee",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "Fastest full featured PostgreSQL client for Node.js",
55
"main": "lib/index.js",
66
"type": "commonjs",

lib/index.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,10 @@ export default function Postgres(url, options) {
135135
}
136136
}
137137

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

142-
const query = { raw, simple }
143-
144142
const promise = new Promise((resolve, reject) => {
145143
query.resolve = resolve
146144
query.reject = reject
@@ -291,15 +289,18 @@ export default function Postgres(url, options) {
291289
if (typeof file === 'string')
292290
return query({ raw: true, simple: true }, connection || getConnection(), files[path])
293291

294-
const promise = (file || new Promise((resolve, reject) => fs.readFile(path, 'utf8', (err, str) => {
295-
if (err)
296-
return reject(err)
292+
const q = { raw: true, simple: true }
293+
const promise = (file || (files[path] = new Promise((resolve, reject) => {
294+
fs.readFile(path, 'utf8', (err, str) => {
295+
if (err)
296+
return reject(err)
297+
298+
files[path] = str
299+
resolve(str)
300+
})
301+
}))).then((str) => query(q, connection || getConnection(), str))
297302

298-
files[path] = str
299-
const q = query({ raw: true, simple: true }, connection || getConnection(), files[path])
300-
promise.stream = q.stream
301-
resolve(q)
302-
})))
303+
promise.stream = fn => (q.stream = fn, promise)
303304

304305
return promise
305306
}

tests/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ t('sql file', async() =>
238238
[1, (await sql.file('./select.sql'))[0].x]
239239
)
240240

241+
t('sql file can stream', async() => {
242+
let result
243+
await sql.file('./select.sql')
244+
.stream(({ x }) => result = x)
245+
246+
return [1, result]
247+
})
248+
241249
t('sql file throws', async() =>
242250
['ENOENT', (await sql.file('./selectomondo.sql').catch(x => x.code))]
243251
)

tests/test.js

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ async function test(o, name, fn, after) {
2525
process.stdout.write('✅')
2626
})
2727
.catch(err => {
28-
p('errorrrr')
2928
tests[line].failed = true
3029
tests[line].error = err instanceof Error ? err : new Error(util.inspect(err))
3130
})

0 commit comments

Comments
 (0)