@@ -34,6 +34,7 @@ const usersTable = pgTable('users', {
34
34
name : text ( 'name' ) . notNull ( ) ,
35
35
verified : boolean ( 'verified' ) . notNull ( ) . default ( false ) ,
36
36
jsonb : jsonb ( 'jsonb' ) . $type < string [ ] > ( ) ,
37
+ bestTexts : text ( 'best_texts' ) . array ( ) . default ( sql `'{}'` ) . notNull ( ) ,
37
38
createdAt : timestamp ( 'created_at' , { withTimezone : true } ) . notNull ( ) . defaultNow ( ) ,
38
39
} ) ;
39
40
@@ -69,6 +70,7 @@ beforeEach(async () => {
69
70
name text not null,
70
71
verified boolean not null default false,
71
72
jsonb jsonb,
73
+ best_texts text[] not null default '{}',
72
74
created_at timestamptz not null default now()
73
75
)
74
76
` ,
@@ -84,7 +86,14 @@ test('select all fields', async () => {
84
86
85
87
expect ( result [ 0 ] ! . createdAt ) . toBeInstanceOf ( Date ) ;
86
88
// t.assert(Math.abs(result[0]!.createdAt.getTime() - now) < 100);
87
- expect ( result ) . toEqual ( [ { id : 1 , name : 'John' , verified : false , jsonb : null , createdAt : result [ 0 ] ! . createdAt } ] ) ;
89
+ expect ( result ) . toEqual ( [ {
90
+ bestTexts : [ ] ,
91
+ id : 1 ,
92
+ name : 'John' ,
93
+ verified : false ,
94
+ jsonb : null ,
95
+ createdAt : result [ 0 ] ! . createdAt ,
96
+ } ] ) ;
88
97
} ) ;
89
98
90
99
test ( 'select sql' , async ( ) => {
@@ -176,7 +185,14 @@ test('update with returning all fields', async () => {
176
185
177
186
expect ( users [ 0 ] ! . createdAt ) . toBeInstanceOf ( Date ) ;
178
187
// t.assert(Math.abs(users[0]!.createdAt.getTime() - now) < 100);
179
- expect ( users ) . toEqual ( [ { id : 1 , name : 'Jane' , verified : false , jsonb : null , createdAt : users [ 0 ] ! . createdAt } ] ) ;
188
+ expect ( users ) . toEqual ( [ {
189
+ id : 1 ,
190
+ bestTexts : [ ] ,
191
+ name : 'Jane' ,
192
+ verified : false ,
193
+ jsonb : null ,
194
+ createdAt : users [ 0 ] ! . createdAt ,
195
+ } ] ) ;
180
196
} ) ;
181
197
182
198
test ( 'update with returning partial' , async ( ) => {
@@ -195,7 +211,14 @@ test('delete with returning all fields', async () => {
195
211
196
212
expect ( users [ 0 ] ! . createdAt ) . toBeInstanceOf ( Date ) ;
197
213
// t.assert(Math.abs(users[0]!.createdAt.getTime() - now) < 100);
198
- expect ( users ) . toEqual ( [ { id : 1 , name : 'John' , verified : false , jsonb : null , createdAt : users [ 0 ] ! . createdAt } ] ) ;
214
+ expect ( users ) . toEqual ( [ {
215
+ bestTexts : [ ] ,
216
+ id : 1 ,
217
+ name : 'John' ,
218
+ verified : false ,
219
+ jsonb : null ,
220
+ createdAt : users [ 0 ] ! . createdAt ,
221
+ } ] ) ;
199
222
} ) ;
200
223
201
224
test ( 'delete with returning partial' , async ( ) => {
@@ -211,13 +234,20 @@ test('delete with returning partial', async () => {
211
234
test ( 'insert + select' , async ( ) => {
212
235
await db . insert ( usersTable ) . values ( { name : 'John' } ) ;
213
236
const result = await db . select ( ) . from ( usersTable ) ;
214
- expect ( result ) . toEqual ( [ { id : 1 , name : 'John' , verified : false , jsonb : null , createdAt : result [ 0 ] ! . createdAt } ] ) ;
237
+ expect ( result ) . toEqual ( [ {
238
+ bestTexts : [ ] ,
239
+ id : 1 ,
240
+ name : 'John' ,
241
+ verified : false ,
242
+ jsonb : null ,
243
+ createdAt : result [ 0 ] ! . createdAt ,
244
+ } ] ) ;
215
245
216
246
await db . insert ( usersTable ) . values ( { name : 'Jane' } ) ;
217
247
const result2 = await db . select ( ) . from ( usersTable ) ;
218
248
expect ( result2 ) . toEqual ( [
219
- { id : 1 , name : 'John' , verified : false , jsonb : null , createdAt : result2 [ 0 ] ! . createdAt } ,
220
- { id : 2 , name : 'Jane' , verified : false , jsonb : null , createdAt : result2 [ 1 ] ! . createdAt } ,
249
+ { bestTexts : [ ] , id : 1 , name : 'John' , verified : false , jsonb : null , createdAt : result2 [ 0 ] ! . createdAt } ,
250
+ { bestTexts : [ ] , id : 2 , name : 'Jane' , verified : false , jsonb : null , createdAt : result2 [ 1 ] ! . createdAt } ,
221
251
] ) ;
222
252
} ) ;
223
253
@@ -236,7 +266,14 @@ test('insert with overridden default values', async () => {
236
266
await db . insert ( usersTable ) . values ( { name : 'John' , verified : true } ) ;
237
267
const result = await db . select ( ) . from ( usersTable ) ;
238
268
239
- expect ( result ) . toEqual ( [ { id : 1 , name : 'John' , verified : true , jsonb : null , createdAt : result [ 0 ] ! . createdAt } ] ) ;
269
+ expect ( result ) . toEqual ( [ {
270
+ bestTexts : [ ] ,
271
+ id : 1 ,
272
+ name : 'John' ,
273
+ verified : true ,
274
+ jsonb : null ,
275
+ createdAt : result [ 0 ] ! . createdAt ,
276
+ } ] ) ;
240
277
} ) ;
241
278
242
279
test ( 'insert many' , async ( ) => {
@@ -385,12 +422,14 @@ test('full join with alias', async () => {
385
422
expect ( result ) . toEqual ( [ {
386
423
users : {
387
424
id : 10 ,
425
+ bestTexts : [ ] ,
388
426
name : 'Ivan' ,
389
427
verified : false ,
390
428
jsonb : null ,
391
429
createdAt : result [ 0 ] ! . users . createdAt ,
392
430
} ,
393
431
customer : {
432
+ bestTexts : [ ] ,
394
433
id : 11 ,
395
434
name : 'Hans' ,
396
435
verified : false ,
@@ -627,7 +666,7 @@ test('build query insert with onConflict do update', async () => {
627
666
628
667
expect ( query ) . toEqual ( {
629
668
sql :
630
- 'insert into "users" ("id", "name", "verified", "jsonb", "created_at") values (default, :1, default, :2, default) on conflict ("id") do update set "name" = :3' ,
669
+ 'insert into "users" ("id", "name", "verified", "jsonb", "best_texts", " created_at") values (default, :1, default, :2, default , default) on conflict ("id") do update set "name" = :3' ,
631
670
params : [ 'John' , '["foo","bar"]' , 'John1' ] ,
632
671
// typings: ['none', 'json', 'none']
633
672
} ) ;
@@ -641,7 +680,7 @@ test('build query insert with onConflict do update / multiple columns', async ()
641
680
642
681
expect ( query ) . toEqual ( {
643
682
sql :
644
- 'insert into "users" ("id", "name", "verified", "jsonb", "created_at") values (default, :1, default, :2, default) on conflict ("id","name") do update set "name" = :3' ,
683
+ 'insert into "users" ("id", "name", "verified", "jsonb", "best_texts", " created_at") values (default, :1, default, :2, default , default) on conflict ("id","name") do update set "name" = :3' ,
645
684
params : [ 'John' , '["foo","bar"]' , 'John1' ] ,
646
685
// typings: ['none', 'json', 'none']
647
686
} ) ;
@@ -655,7 +694,7 @@ test('build query insert with onConflict do nothing', async () => {
655
694
656
695
expect ( query ) . toEqual ( {
657
696
sql :
658
- 'insert into "users" ("id", "name", "verified", "jsonb", "created_at") values (default, :1, default, :2, default) on conflict do nothing' ,
697
+ 'insert into "users" ("id", "name", "verified", "jsonb", "best_texts", " created_at") values (default, :1, default, :2, default , default) on conflict do nothing' ,
659
698
params : [ 'John' , '["foo","bar"]' ] ,
660
699
// typings: ['none', 'json']
661
700
} ) ;
@@ -669,7 +708,7 @@ test('build query insert with onConflict do nothing + target', async () => {
669
708
670
709
expect ( query ) . toEqual ( {
671
710
sql :
672
- 'insert into "users" ("id", "name", "verified", "jsonb", "created_at") values (default, :1, default, :2, default) on conflict ("id") do nothing' ,
711
+ 'insert into "users" ("id", "name", "verified", "jsonb", "best_texts", " created_at") values (default, :1, default, :2, default , default) on conflict ("id") do nothing' ,
673
712
params : [ 'John' , '["foo","bar"]' ] ,
674
713
// typings: ['none', 'json']
675
714
} ) ;
@@ -857,6 +896,69 @@ test('select from raw sql with mapped values', async () => {
857
896
] ) ;
858
897
} ) ;
859
898
899
+ test ( 'insert with array values works' , async ( ) => {
900
+ const bestTexts = [ 'text1' , 'text2' , 'text3' ] ;
901
+ const [ insertResult ] = await db . insert ( usersTable ) . values ( {
902
+ name : 'John' ,
903
+ bestTexts,
904
+ } ) . returning ( ) ;
905
+
906
+ expect ( insertResult ?. bestTexts ) . toEqual ( bestTexts ) ;
907
+ } ) ;
908
+
909
+ test ( 'update with array values works' , async ( ) => {
910
+ const [ newUser ] = await db . insert ( usersTable ) . values ( { name : 'John' } ) . returning ( ) ;
911
+
912
+ const bestTexts = [ 'text4' , 'text5' , 'text6' ] ;
913
+ const [ insertResult ] = await db . update ( usersTable ) . set ( {
914
+ bestTexts,
915
+ } ) . where ( eq ( usersTable . id , newUser ! . id ) ) . returning ( ) ;
916
+
917
+ expect ( insertResult ?. bestTexts ) . toEqual ( bestTexts ) ;
918
+ } ) ;
919
+
920
+ test ( 'insert with array values works' , async ( ) => {
921
+ const bestTexts = [ 'text1' , 'text2' , 'text3' ] ;
922
+ const [ insertResult ] = await db . insert ( usersTable ) . values ( {
923
+ name : 'John' ,
924
+ bestTexts,
925
+ } ) . returning ( ) ;
926
+
927
+ expect ( insertResult ?. bestTexts ) . toEqual ( bestTexts ) ;
928
+ } ) ;
929
+
930
+ test ( 'update with array values works' , async ( ) => {
931
+ const [ newUser ] = await db . insert ( usersTable ) . values ( { name : 'John' } ) . returning ( ) ;
932
+
933
+ const bestTexts = [ 'text4' , 'text5' , 'text6' ] ;
934
+ const [ insertResult ] = await db . update ( usersTable ) . set ( {
935
+ bestTexts,
936
+ } ) . where ( eq ( usersTable . id , newUser ! . id ) ) . returning ( ) ;
937
+
938
+ expect ( insertResult ?. bestTexts ) . toEqual ( bestTexts ) ;
939
+ } ) ;
940
+
941
+ test ( 'insert with array values works' , async ( ) => {
942
+ const bestTexts = [ 'text1' , 'text2' , 'text3' ] ;
943
+ const [ insertResult ] = await db . insert ( usersTable ) . values ( {
944
+ name : 'John' ,
945
+ bestTexts,
946
+ } ) . returning ( ) ;
947
+
948
+ expect ( insertResult ?. bestTexts ) . toEqual ( bestTexts ) ;
949
+ } ) ;
950
+
951
+ test ( 'update with array values works' , async ( ) => {
952
+ const [ newUser ] = await db . insert ( usersTable ) . values ( { name : 'John' } ) . returning ( ) ;
953
+
954
+ const bestTexts = [ 'text4' , 'text5' , 'text6' ] ;
955
+ const [ insertResult ] = await db . update ( usersTable ) . set ( {
956
+ bestTexts,
957
+ } ) . where ( eq ( usersTable . id , newUser ! . id ) ) . returning ( ) ;
958
+
959
+ expect ( insertResult ?. bestTexts ) . toEqual ( bestTexts ) ;
960
+ } ) ;
961
+
860
962
test ( 'all date and time columns' , async ( ) => {
861
963
const table = pgTable ( 'all_columns' , {
862
964
id : serial ( 'id' ) . primaryKey ( ) ,
0 commit comments