1
1
# deno-postgres
2
2
3
3
![ Build Status] ( https://img.shields.io/github/actions/workflow/status/denodrivers/postgres/ci.yml?branch=main&label=Build&logo=github&style=flat-square )
4
- [ ![ Discord server] ( https://img.shields.io/discord/768918486575480863?color=blue&label=Ask%20for%20help%20here&logo=discord&style=flat-square )] ( https://discord.gg/HEdTCvZUSf )
4
+ [ ![ Discord server] ( https://img.shields.io/discord/768918486575480863?color=blue&label=Ask%20for%20help%20here&logo=discord&style=flat-square )] ( https://discord.gg/sCNaAvQeEa )
5
5
[ ![ JSR] ( https://jsr.io/badges/@db/postgres?style=flat-square )] ( https://jsr.io/@db/postgres )
6
6
[ ![ JSR Score] ( https://jsr.io/badges/@db/postgres/score?style=flat-square )] ( https://jsr.io/@db/postgres )
7
7
[ ![ Manual] ( https://img.shields.io/github/v/release/denodrivers/postgres?color=orange&label=Manual&logo=deno&style=flat-square )] ( https://deno-postgres.com )
@@ -300,15 +300,15 @@ const path = "/var/run/postgresql";
300
300
301
301
const client = new Client (
302
302
// postgres://user:password@%2Fvar%2Frun%2Fpostgresql:port/database_name
303
- ` postgres://user:password@${encodeURIComponent (path )}:port/database_name ` ,
303
+ ` postgres://user:password@${encodeURIComponent (path )}:port/database_name `
304
304
);
305
305
```
306
306
307
307
Additionally, you can specify the host using the ` host ` URL parameter
308
308
309
309
``` ts
310
310
const client = new Client (
311
- ` postgres://user:password@:port/database_name?host=/var/run/postgresql ` ,
311
+ ` postgres://user:password@:port/database_name?host=/var/run/postgresql `
312
312
);
313
313
```
314
314
@@ -355,7 +355,7 @@ const client = new Client({
355
355
tls: {
356
356
caCertificates: [
357
357
await Deno .readTextFile (
358
- new URL (" ./my_ca_certificate.crt" , import .meta .url ),
358
+ new URL (" ./my_ca_certificate.crt" , import .meta .url )
359
359
),
360
360
],
361
361
enabled: false ,
@@ -582,7 +582,7 @@ variables required, and then provide said variables in an array of arguments
582
582
{
583
583
const result = await client .queryArray (
584
584
" SELECT ID, NAME FROM PEOPLE WHERE AGE > $1 AND AGE < $2" ,
585
- [10 , 20 ],
585
+ [10 , 20 ]
586
586
);
587
587
console .log (result .rows );
588
588
}
@@ -605,7 +605,7 @@ replaced at runtime with an argument object
605
605
{
606
606
const result = await client .queryArray (
607
607
" SELECT ID, NAME FROM PEOPLE WHERE AGE > $MIN AND AGE < $MAX" ,
608
- { min: 10 , max: 20 },
608
+ { min: 10 , max: 20 }
609
609
);
610
610
console .log (result .rows );
611
611
}
@@ -632,7 +632,7 @@ places in your query
632
632
FROM PEOPLE
633
633
WHERE NAME ILIKE $SEARCH
634
634
OR LASTNAME ILIKE $SEARCH ` ,
635
- { search: " JACKSON" },
635
+ { search: " JACKSON" }
636
636
);
637
637
console .log (result .rows );
638
638
}
@@ -654,16 +654,16 @@ prepared statements with a nice and clear syntax for your queries
654
654
655
655
``` ts
656
656
{
657
- const result = await client
658
- .queryArray ` SELECT ID, NAME FROM PEOPLE WHERE AGE > ${10 } AND AGE < ${20 } ` ;
657
+ const result =
658
+ await client .queryArray ` SELECT ID, NAME FROM PEOPLE WHERE AGE > ${10 } AND AGE < ${20 } ` ;
659
659
console .log (result .rows );
660
660
}
661
661
662
662
{
663
663
const min = 10 ;
664
664
const max = 20 ;
665
- const result = await client
666
- .queryObject ` SELECT ID, NAME FROM PEOPLE WHERE AGE > ${min } AND AGE < ${max } ` ;
665
+ const result =
666
+ await client .queryObject ` SELECT ID, NAME FROM PEOPLE WHERE AGE > ${min } AND AGE < ${max } ` ;
667
667
console .log (result .rows );
668
668
}
669
669
```
@@ -712,8 +712,7 @@ await client.queryArray`UPDATE TABLE X SET Y = 0 WHERE Z = ${my_id}`;
712
712
// Invalid attempt to replace a specifier
713
713
const my_table = " IMPORTANT_TABLE" ;
714
714
const my_other_id = 41 ;
715
- await client
716
- .queryArray ` DELETE FROM ${my_table } WHERE MY_COLUMN = ${my_other_id }; ` ;
715
+ await client .queryArray ` DELETE FROM ${my_table } WHERE MY_COLUMN = ${my_other_id }; ` ;
717
716
```
718
717
719
718
### Result decoding
@@ -753,7 +752,7 @@ available:
753
752
});
754
753
755
754
const result = await client .queryArray (
756
- " SELECT ID, NAME, AGE, BIRTHDATE FROM PEOPLE WHERE ID = 1" ,
755
+ " SELECT ID, NAME, AGE, BIRTHDATE FROM PEOPLE WHERE ID = 1"
757
756
);
758
757
console .log (result .rows ); // [[1, "Laura", 25, Date('1996-01-01') ]]
759
758
@@ -769,7 +768,7 @@ available:
769
768
});
770
769
771
770
const result = await client .queryArray (
772
- " SELECT ID, NAME, AGE, BIRTHDATE FROM PEOPLE WHERE ID = 1" ,
771
+ " SELECT ID, NAME, AGE, BIRTHDATE FROM PEOPLE WHERE ID = 1"
773
772
);
774
773
console .log (result .rows ); // [["1", "Laura", "25", "1996-01-01"]]
775
774
}
@@ -805,7 +804,7 @@ the strategy and internal decoders.
805
804
});
806
805
807
806
const result = await client .queryObject (
808
- " SELECT ID, NAME, IS_ACTIVE FROM PEOPLE" ,
807
+ " SELECT ID, NAME, IS_ACTIVE FROM PEOPLE"
809
808
);
810
809
console .log (result .rows [0 ]);
811
810
// {id: '1', name: 'Javier', is_active: { value: false, type: "boolean"}}
@@ -834,7 +833,7 @@ for the array type itself.
834
833
});
835
834
836
835
const result = await client .queryObject (
837
- " SELECT ARRAY[ 2, 2, 3, 1 ] AS scores, 8 final_score;" ,
836
+ " SELECT ARRAY[ 2, 2, 3, 1 ] AS scores, 8 final_score;"
838
837
);
839
838
console .log (result .rows [0 ]);
840
839
// { scores: [ 200, 200, 300, 100 ], final_score: 800 }
@@ -850,7 +849,7 @@ IntelliSense
850
849
``` ts
851
850
{
852
851
const array_result = await client .queryArray <[number , string ]>(
853
- " SELECT ID, NAME FROM PEOPLE WHERE ID = 17" ,
852
+ " SELECT ID, NAME FROM PEOPLE WHERE ID = 17"
854
853
);
855
854
// [number, string]
856
855
const person = array_result .rows [0 ];
@@ -866,7 +865,7 @@ IntelliSense
866
865
867
866
{
868
867
const object_result = await client .queryObject <{ id: number ; name: string }>(
869
- " SELECT ID, NAME FROM PEOPLE WHERE ID = 17" ,
868
+ " SELECT ID, NAME FROM PEOPLE WHERE ID = 17"
870
869
);
871
870
// {id: number, name: string}
872
871
const person = object_result .rows [0 ];
@@ -931,7 +930,7 @@ one the user might expect
931
930
932
931
``` ts
933
932
const result = await client .queryObject (
934
- " SELECT ID, SUBSTR(NAME, 0, 2) FROM PEOPLE" ,
933
+ " SELECT ID, SUBSTR(NAME, 0, 2) FROM PEOPLE"
935
934
);
936
935
937
936
const users = result .rows ; // [{id: 1, substr: 'Ca'}, {id: 2, substr: 'Jo'}, ...]
@@ -959,7 +958,7 @@ interface User {
959
958
}
960
959
961
960
const result = await client .queryObject <User >(
962
- " SELECT ID, SUBSTR(NAME, 0, 2) FROM PEOPLE" ,
961
+ " SELECT ID, SUBSTR(NAME, 0, 2) FROM PEOPLE"
963
962
);
964
963
965
964
const users = result .rows ; // TypeScript says this will be User[]
@@ -1184,8 +1183,7 @@ const transaction = client_1.createTransaction("transaction_1");
1184
1183
1185
1184
await transaction .begin ();
1186
1185
1187
- await transaction
1188
- .queryArray ` CREATE TABLE TEST_RESULTS (USER_ID INTEGER, GRADE NUMERIC(10,2)) ` ;
1186
+ await transaction .queryArray ` CREATE TABLE TEST_RESULTS (USER_ID INTEGER, GRADE NUMERIC(10,2)) ` ;
1189
1187
await transaction .queryArray ` CREATE TABLE GRADUATED_STUDENTS (USER_ID INTEGER) ` ;
1190
1188
1191
1189
// This operation takes several minutes
@@ -1241,8 +1239,7 @@ following levels of transaction isolation:
1241
1239
const password_1 = rows [0 ].password ;
1242
1240
1243
1241
// Concurrent operation executed by a different user in a different part of the code
1244
- await client_2
1245
- .queryArray ` UPDATE IMPORTANT_TABLE SET PASSWORD = 'something_else' WHERE ID = ${the_same_id } ` ;
1242
+ await client_2 .queryArray ` UPDATE IMPORTANT_TABLE SET PASSWORD = 'something_else' WHERE ID = ${the_same_id } ` ;
1246
1243
1247
1244
const { rows : query_2 } = await transaction .queryObject <{
1248
1245
password: string ;
@@ -1280,14 +1277,12 @@ following levels of transaction isolation:
1280
1277
}>` SELECT PASSWORD FROM IMPORTANT_TABLE WHERE ID = ${my_id } ` ;
1281
1278
1282
1279
// Concurrent operation executed by a different user in a different part of the code
1283
- await client_2
1284
- .queryArray ` UPDATE IMPORTANT_TABLE SET PASSWORD = 'something_else' WHERE ID = ${the_same_id } ` ;
1280
+ await client_2 .queryArray ` UPDATE IMPORTANT_TABLE SET PASSWORD = 'something_else' WHERE ID = ${the_same_id } ` ;
1285
1281
1286
1282
// This statement will throw
1287
1283
// Target was modified outside of the transaction
1288
1284
// User may not be aware of the changes
1289
- await transaction
1290
- .queryArray ` UPDATE IMPORTANT_TABLE SET PASSWORD = 'shiny_new_password' WHERE ID = ${the_same_id } ` ;
1285
+ await transaction .queryArray ` UPDATE IMPORTANT_TABLE SET PASSWORD = 'shiny_new_password' WHERE ID = ${the_same_id } ` ;
1291
1286
1292
1287
// Transaction is aborted, no need to end it
1293
1288
@@ -1424,7 +1419,7 @@ explained above in the `Savepoint` documentation.
1424
1419
1425
1420
``` ts
1426
1421
const transaction = client .createTransaction (
1427
- " partially_rolled_back_transaction" ,
1422
+ " partially_rolled_back_transaction"
1428
1423
);
1429
1424
await transaction .savepoint (" undo" );
1430
1425
await transaction .queryArray ` TRUNCATE TABLE DONT_DELETE_ME ` ; // Oops, wrong table
0 commit comments