@@ -41,8 +41,8 @@ cp.execSync('dropdb ' + options.db + ';createdb ' + options.db)
41
41
42
42
const sql = postgres ( options )
43
43
44
- t ( 'Result is array' ,
45
- async ( ) => [ true , Array . isArray ( await sql `select 1` ) ]
44
+ t ( 'Result is array' , async ( ) =>
45
+ [ true , Array . isArray ( await sql `select 1` ) ]
46
46
)
47
47
48
48
t ( 'Result has count' , async ( ) =>
@@ -55,11 +55,12 @@ t('Result has command', async() =>
55
55
56
56
t ( 'Create table' , async ( ) =>
57
57
[ 'CREATE TABLE' , ( await sql `create table test(int int)` ) . command ]
58
- )
58
+ , ( ) => sql `drop table test` )
59
59
60
- t ( 'Drop table' , async ( ) =>
61
- [ 'DROP TABLE' , ( await sql `drop table test` ) . command ]
62
- )
60
+ t ( 'Drop table' , async ( ) => {
61
+ await sql `create table test(int int)`
62
+ return [ 'DROP TABLE' , ( await sql `drop table test` ) . command ]
63
+ } )
63
64
64
65
t ( 'null' , async ( ) =>
65
66
[ null , ( await sql `select ${ null } as x` ) [ 0 ] . x ]
@@ -148,7 +149,6 @@ t('Transaction throws on uncaught savepoint', async() => {
148
149
throw new Error ( 'fail' )
149
150
} )
150
151
151
- /* c8 ignore next */
152
152
await sql `insert into test values(3)`
153
153
} ) . catch ( ( ) => 'fail' ) ) ]
154
154
} , ( ) => sql `drop table test` )
@@ -189,7 +189,6 @@ t('Fail with proper error on no host', async() =>
189
189
} ) ) . code ]
190
190
)
191
191
192
- // If your local db doesn't support ssl this will be tested in CI
193
192
t ( 'Connect using SSL' , async ( ) =>
194
193
[ true , ( await new Promise ( ( resolve , reject ) => {
195
194
postgres ( {
@@ -285,75 +284,50 @@ t('Message not supported', async() => {
285
284
return [ 'MESSAGE_NOT_SUPPORTED' , await sql `copy test to stdout` . catch ( x => x . code ) ]
286
285
} , ( ) => sql `drop table test` )
287
286
288
- t ( 'transform' , async ( ) => {
287
+ t ( 'transform column ' , async ( ) => {
289
288
const sql = postgres ( {
290
289
...options ,
291
- transform : x => x . split ( '' ) . reverse ( ) . join ( '' )
290
+ transform : { column : x => x . split ( '' ) . reverse ( ) . join ( '' ) }
292
291
} )
293
292
294
293
await sql `create table test (hello_world int)`
295
294
await sql `insert into test values (1)`
296
295
return [ 'dlrow_olleh' , Object . keys ( ( await sql `select * from test` ) [ 0 ] ) [ 0 ] ]
297
296
} , ( ) => sql `drop table test` )
298
297
299
- t ( 'toPascal' , async ( ) => {
298
+ t ( 'column toPascal' , async ( ) => {
300
299
const sql = postgres ( {
301
300
...options ,
302
- transform : postgres . toPascal
301
+ transform : { column : postgres . toPascal }
303
302
} )
304
303
305
304
await sql `create table test (hello_world int)`
306
305
await sql `insert into test values (1)`
307
306
return [ 'HelloWorld' , Object . keys ( ( await sql `select * from test` ) [ 0 ] ) [ 0 ] ]
308
307
} , ( ) => sql `drop table test` )
309
308
310
- t ( 'toCamel' , async ( ) => {
309
+ t ( 'column toCamel' , async ( ) => {
311
310
const sql = postgres ( {
312
311
...options ,
313
- transform : postgres . toCamel
312
+ transform : { column : postgres . toCamel }
314
313
} )
315
314
316
315
await sql `create table test (hello_world int)`
317
316
await sql `insert into test values (1)`
318
317
return [ 'helloWorld' , Object . keys ( ( await sql `select * from test` ) [ 0 ] ) [ 0 ] ]
319
318
} , ( ) => sql `drop table test` )
320
319
321
- t ( 'toKebab' , async ( ) => {
320
+ t ( 'column toKebab' , async ( ) => {
322
321
const sql = postgres ( {
323
322
...options ,
324
- transform : postgres . toKebab
323
+ transform : { column : postgres . toKebab }
325
324
} )
326
325
327
326
await sql `create table test (hello_world int)`
328
327
await sql `insert into test values (1)`
329
328
return [ 'hello-world' , Object . keys ( ( await sql `select * from test` ) [ 0 ] ) [ 0 ] ]
330
329
} , ( ) => sql `drop table test` )
331
330
332
- t ( 'row helper' , async ( ) => {
333
- const obj = { a : 1 , b : 'hello' , c : false }
334
- await sql `create table test (a int, b text, c bool)`
335
- await sql `insert into test (a, b, c) values ${ sql . row ( obj , 'a' , 'b' , 'c' ) } `
336
-
337
- const [ x ] = await sql `select * from test`
338
- return [ true , x . a === 1 && x . b === 'hello' && x . c === false ]
339
- } , ( ) => sql `drop table test` )
340
-
341
- t ( 'multi rows helper' , async ( ) => {
342
- const obj = { a : 1 , b : 'hello' , c : false }
343
- const arr = [ obj , obj ]
344
-
345
- await sql `create table test (a int, b text, c bool)`
346
- await sql `insert into test (a, b, c) values ${ sql . rows ( arr , 'a' , 'b' , 'c' ) } `
347
- await sql `insert into test (a, b, c) values ${ sql . rows ( arr , x => [ x . a , x . b , x . c ] ) } `
348
- await sql `insert into test (a, b, c) values ${ sql . rows ( arr . map ( x => [ x . a , x . b , x . c ] ) ) } `
349
-
350
- const x = await sql `select * from test`
351
- return [ true , x [ 0 ] . a === 1 && x [ 0 ] . b === 'hello' && x [ 0 ] . c === false &&
352
- x [ 1 ] . a === 1 && x [ 1 ] . b === 'hello' && x [ 1 ] . c === false &&
353
- x . count === 6
354
- ]
355
- } , ( ) => sql `drop table test` )
356
-
357
331
t ( 'unsafe' , async ( ) => {
358
332
await sql `create table test (x int)`
359
333
return [ 1 , ( await sql . unsafe ( 'insert into test values ($1) returning *' , [ 1 ] ) ) [ 0 ] . x ]
@@ -367,12 +341,10 @@ t('listen and notify', async() => {
367
341
const sql = postgres ( options )
368
342
369
343
return [ 'world' , await new Promise ( ( resolve , reject ) =>
370
- sql . listen ( 'hello' , x => {
371
- resolve ( x )
372
- sql . end ( )
373
- } )
344
+ sql . listen ( 'hello' , resolve )
374
345
. then ( ( ) => sql . notify ( 'hello' , 'world' ) )
375
346
. catch ( reject )
347
+ . then ( sql . end )
376
348
) ]
377
349
} )
378
350
@@ -386,7 +358,7 @@ t('responds with server parameters (application_name)', async() =>
386
358
t ( 'onconnect' , async ( ) => {
387
359
const sql = postgres ( {
388
360
...options ,
389
- onconnect : ( ) => 'something '
361
+ onconnect : ( ) => 'connected '
390
362
} )
391
363
392
364
return [ 1 , ( await sql `select 1 as x` ) [ 0 ] . x ]
@@ -417,32 +389,112 @@ t('has server parameters', async() => {
417
389
418
390
t ( 'big query body' , async ( ) => {
419
391
await sql `create table test (x int)`
420
- return [ 1000 , ( await sql `insert into test values ${
421
- sql . rows ( [ ...Array ( 1000 ) . keys ( ) ] . map ( x => [ x ] ) )
392
+ return [ 1000 , ( await sql `insert into test ${
393
+ sql ( [ ...Array ( 1000 ) . keys ( ) ] . map ( x => ( { x } ) ) )
422
394
} `) . count ]
423
395
} , ( ) => sql `drop table test` )
424
396
397
+ t ( 'Throws if more than 65534 parameters' , async ( ) => {
398
+ await sql `create table test (x int)`
399
+ return [ 'MAX_PARAMETERS_EXCEEDED' , ( await sql `insert into test ${
400
+ sql ( [ ...Array ( 65535 ) . keys ( ) ] . map ( x => ( { x } ) ) )
401
+ } `. catch ( e => e . code ) ) ]
402
+ } , ( ) => sql `drop table test` )
425
403
426
- /*
404
+ t ( 'let postgres do implicit cast of unknown types' , async ( ) => {
405
+ await sql `create table test (x timestamp with time zone)`
406
+ const [ { x } ] = await sql `insert into test values (${ new Date ( ) . toISOString ( ) } ) returning *`
407
+ return [ true , x instanceof Date ]
408
+ } , ( ) => sql `drop table test` )
427
409
410
+ t ( 'only allows one statement' , async ( ) =>
411
+ [ '42601' , await sql `select 1; select 2` . catch ( e => e . code ) ]
412
+ )
428
413
429
- t('select column vars', async() => {
430
- await sql`create table test (x int)`
431
- await sql`insert into test values (1)`
432
- return [1, (await sql`select ${ 'x' } from test`)[0].x]
414
+ t ( 'await sql() throws not tagged error' , async ( ) => {
415
+ let error
416
+ try {
417
+ await sql ( 'select 1' )
418
+ } catch ( e ) {
419
+ error = e . code
420
+ }
421
+ return [ 'NOT_TAGGED_CALL' , error ]
433
422
} )
434
423
435
- t('select column vars', async() => {
436
- await sql`create table test (x int)`
437
- await sql`insert into test values (1)`
438
- return [1, (await sql`select ${ 'x' } from test`)[0].x]
424
+ t ( 'sql().then throws not tagged error' , async ( ) => {
425
+ let error
426
+ try {
427
+ sql ( 'select 1' ) . then ( ( ) => { } )
428
+ } catch ( e ) {
429
+ error = e . code
430
+ }
431
+ return [ 'NOT_TAGGED_CALL' , error ]
439
432
} )
440
433
441
- t('select column vars', async() => {
442
- await sql`create table test (x int)`
443
- await sql`insert into test values (1)`
444
- return [1, (await sql`select ${ 'x' } from test`)[0].x]
434
+ t ( 'sql().catch throws not tagged error' , async ( ) => {
435
+ let error
436
+ try {
437
+ sql ( 'select 1' ) . catch ( ( ) => { } )
438
+ } catch ( e ) {
439
+ error = e . code
440
+ }
441
+ return [ 'NOT_TAGGED_CALL' , error ]
445
442
} )
446
443
444
+ t ( 'sql().finally throws not tagged error' , async ( ) => {
445
+ let error
446
+ try {
447
+ sql ( 'select 1' ) . finally ( ( ) => { } )
448
+ } catch ( e ) {
449
+ error = e . code
450
+ }
451
+ return [ 'NOT_TAGGED_CALL' , error ]
452
+ } )
453
+
454
+ t ( 'dynamic column name' , async ( ) => {
455
+ return [ '!not_valid' , Object . keys ( ( await sql `select 1 as ${ sql ( '!not_valid' ) } ` ) [ 0 ] ) [ 0 ] ]
456
+ } )
457
+
458
+ t ( 'dynamic select as' , async ( ) => {
459
+ return [ 2 , ( await sql `select ${ sql ( { a : 1 , b : 2 } ) } ` ) [ 0 ] . b ]
460
+ } )
461
+
462
+ t ( 'dynamic insert' , async ( ) => {
463
+ await sql `create table test (a int, b text)`
464
+ const x = { a : 42 , b : 'the answer' }
465
+
466
+ return [ 'the answer' , ( await sql `insert into test ${ sql ( x ) } returning *` ) [ 0 ] . b ]
467
+ } , ( ) => sql `drop table test` )
468
+
469
+ t ( 'dynamic multi row insert' , async ( ) => {
470
+ await sql `create table test (a int, b text)`
471
+ const x = { a : 42 , b : 'the answer' }
472
+
473
+ return [ 'the answer' , ( await sql `insert into test ${ sql ( [ x , x ] ) } returning *` ) [ 1 ] . b ]
474
+ } , ( ) => sql `drop table test` )
475
+
476
+ t ( 'dynamic update' , async ( ) => {
477
+ await sql `create table test (a int, b text)`
478
+ await sql `insert into test (a, b) values (17, 'wrong')`
479
+
480
+ return [ 'the answer' , ( await sql `update test set ${ sql ( { a : 42 , b : 'the answer' } ) } returning *` ) [ 0 ] . b ]
481
+ } , ( ) => sql `drop table test` )
482
+
483
+ t ( 'dynamic update pluck' , async ( ) => {
484
+ await sql `create table test (a int, b text)`
485
+ await sql `insert into test (a, b) values (17, 'wrong')`
447
486
448
- */
487
+ return [ 'wrong' , ( await sql `update test set ${ sql ( { a : 42 , b : 'the answer' } , 'a' ) } returning *` ) [ 0 ] . b ]
488
+ } , ( ) => sql `drop table test` )
489
+
490
+ t ( 'dynamic select array' , async ( ) => {
491
+ await sql `create table test (a int, b text)`
492
+ await sql `insert into test (a, b) values (42, 'yay')`
493
+ return [ 'yay' , ( await sql `select ${ sql ( [ 'a' , 'b' ] ) } from test` ) [ 0 ] . b ]
494
+ } , ( ) => sql `drop table test` )
495
+
496
+ t ( 'dynamic select args' , async ( ) => {
497
+ await sql `create table test (a int, b text)`
498
+ await sql `insert into test (a, b) values (42, 'yay')`
499
+ return [ 'yay' , ( await sql `select ${ sql ( 'a' , 'b' ) } from test` ) [ 0 ] . b ]
500
+ } , ( ) => sql `drop table test` )
0 commit comments