1
1
import os from 'os'
2
+ import fs from 'fs'
2
3
import crypto from 'crypto'
3
4
import Url from 'url'
4
5
import Connection from './connection.js'
@@ -35,6 +36,7 @@ export default function Postgres(url, options) {
35
36
, queries = Queue ( )
36
37
, listeners = { }
37
38
, typeArrayMap = { }
39
+ , files = { }
38
40
39
41
function postgres ( xs , ...args ) {
40
42
return query ( { } , getConnection ( ) , xs , args )
@@ -142,7 +144,6 @@ export default function Postgres(url, options) {
142
144
const promise = new Promise ( ( resolve , reject ) => {
143
145
query . resolve = resolve
144
146
query . reject = reject
145
-
146
147
ended !== null
147
148
? reject ( errors . connection ( 'ENDED' , options ) )
148
149
: ready
@@ -266,9 +267,10 @@ export default function Postgres(url, options) {
266
267
notify,
267
268
unsafe,
268
269
array,
270
+ file,
271
+ json,
269
272
rows,
270
- row,
271
- json
273
+ row
272
274
} )
273
275
274
276
function notify ( channel , payload ) {
@@ -283,6 +285,25 @@ export default function Postgres(url, options) {
283
285
return query ( { raw : true , simple : options . simple } , connection || getConnection ( ) , xs , args )
284
286
}
285
287
288
+ function file ( path ) {
289
+ const file = files [ path ]
290
+
291
+ if ( typeof file === 'string' )
292
+ return query ( { raw : true , simple : true } , connection || getConnection ( ) , files [ path ] )
293
+
294
+ const promise = ( file || new Promise ( ( resolve , reject ) => fs . readFile ( path , 'utf8' , ( err , str ) => {
295
+ if ( err )
296
+ return reject ( err )
297
+
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
+
304
+ return promise
305
+ }
306
+
286
307
options . types && Object . entries ( options . types ) . forEach ( ( [ name , type ] ) => {
287
308
if ( name in sql )
288
309
throw errors . generic ( { message : name + ' is a reserved method name' , code : 'RESERVED_METHOD_NAME' } )
0 commit comments