@@ -18,11 +18,13 @@ import {
18
18
getTableColumns ,
19
19
gt ,
20
20
gte ,
21
+ ilike ,
21
22
inArray ,
22
23
lt ,
23
24
max ,
24
25
min ,
25
26
name ,
27
+ or ,
26
28
placeholder ,
27
29
type SQL ,
28
30
sql ,
@@ -4125,3 +4127,201 @@ test.serial('test $onUpdateFn and $onUpdate works updating', async (t) => {
4125
4127
t . assert ( eachUser . updatedAt ! . valueOf ( ) > Date . now ( ) - msDelay ) ;
4126
4128
}
4127
4129
} ) ;
4130
+
4131
+ test . serial ( 'test if method with sql operators' , async ( t ) => {
4132
+ const { db } = t . context ;
4133
+
4134
+ const users = pgTable ( 'users' , {
4135
+ id : serial ( 'id' ) . primaryKey ( ) ,
4136
+ name : text ( 'name' ) . notNull ( ) ,
4137
+ age : integer ( 'age' ) . notNull ( ) ,
4138
+ city : text ( 'city' ) . notNull ( ) ,
4139
+ } ) ;
4140
+
4141
+ await db . execute ( sql `drop table if exists ${ users } ` ) ;
4142
+
4143
+ await db . execute ( sql `
4144
+ create table ${ users } (
4145
+ id serial primary key,
4146
+ name text not null,
4147
+ age integer not null,
4148
+ city text not null
4149
+ )
4150
+ ` ) ;
4151
+
4152
+ await db . insert ( users ) . values ( [
4153
+ { id : 1 , name : 'John' , age : 20 , city : 'New York' } ,
4154
+ { id : 2 , name : 'Alice' , age : 21 , city : 'New York' } ,
4155
+ { id : 3 , name : 'Nick' , age : 22 , city : 'London' } ,
4156
+ { id : 4 , name : 'Lina' , age : 23 , city : 'London' } ,
4157
+ ] ) ;
4158
+
4159
+ const condition1 = true ;
4160
+
4161
+ const [ result1 ] = await db . select ( ) . from ( users ) . where ( eq ( users . id , 1 ) . if ( condition1 ) ) ;
4162
+
4163
+ t . deepEqual ( result1 , { id : 1 , name : 'John' , age : 20 , city : 'New York' } ) ;
4164
+
4165
+ const condition2 = 1 ;
4166
+
4167
+ const [ result2 ] = await db . select ( ) . from ( users ) . where ( sql `${ users . id } = 1` . if ( condition2 ) ) ;
4168
+
4169
+ t . deepEqual ( result2 , { id : 1 , name : 'John' , age : 20 , city : 'New York' } ) ;
4170
+
4171
+ const condition3 = 'non-empty string' ;
4172
+
4173
+ const result3 = await db . select ( ) . from ( users ) . where (
4174
+ or ( eq ( users . id , 1 ) . if ( condition3 ) , eq ( users . id , 2 ) . if ( condition3 ) ) ,
4175
+ ) ;
4176
+
4177
+ t . deepEqual ( result3 , [ { id : 1 , name : 'John' , age : 20 , city : 'New York' } , {
4178
+ id : 2 ,
4179
+ name : 'Alice' ,
4180
+ age : 21 ,
4181
+ city : 'New York' ,
4182
+ } ] ) ;
4183
+
4184
+ const condtition4 = false ;
4185
+
4186
+ const result4 = await db . select ( ) . from ( users ) . where ( eq ( users . id , 1 ) . if ( condtition4 ) ) ;
4187
+
4188
+ t . deepEqual ( result4 , [
4189
+ { id : 1 , name : 'John' , age : 20 , city : 'New York' } ,
4190
+ { id : 2 , name : 'Alice' , age : 21 , city : 'New York' } ,
4191
+ { id : 3 , name : 'Nick' , age : 22 , city : 'London' } ,
4192
+ { id : 4 , name : 'Lina' , age : 23 , city : 'London' } ,
4193
+ ] ) ;
4194
+
4195
+ const condition5 = undefined ;
4196
+
4197
+ const result5 = await db . select ( ) . from ( users ) . where ( sql `${ users . id } = 1` . if ( condition5 ) ) ;
4198
+
4199
+ t . deepEqual ( result5 , [
4200
+ { id : 1 , name : 'John' , age : 20 , city : 'New York' } ,
4201
+ { id : 2 , name : 'Alice' , age : 21 , city : 'New York' } ,
4202
+ { id : 3 , name : 'Nick' , age : 22 , city : 'London' } ,
4203
+ { id : 4 , name : 'Lina' , age : 23 , city : 'London' } ,
4204
+ ] ) ;
4205
+
4206
+ const condition6 = null ;
4207
+
4208
+ const result6 = await db . select ( ) . from ( users ) . where (
4209
+ or ( eq ( users . id , 1 ) . if ( condition6 ) , eq ( users . id , 2 ) . if ( condition6 ) ) ,
4210
+ ) ;
4211
+
4212
+ t . deepEqual ( result6 , [
4213
+ { id : 1 , name : 'John' , age : 20 , city : 'New York' } ,
4214
+ { id : 2 , name : 'Alice' , age : 21 , city : 'New York' } ,
4215
+ { id : 3 , name : 'Nick' , age : 22 , city : 'London' } ,
4216
+ { id : 4 , name : 'Lina' , age : 23 , city : 'London' } ,
4217
+ ] ) ;
4218
+
4219
+ const condition7 = {
4220
+ term1 : 0 ,
4221
+ term2 : 1 ,
4222
+ } ;
4223
+
4224
+ const result7 = await db . select ( ) . from ( users ) . where (
4225
+ and ( gt ( users . age , 20 ) . if ( condition7 . term1 ) , eq ( users . city , 'New York' ) . if ( condition7 . term2 ) ) ,
4226
+ ) ;
4227
+
4228
+ t . deepEqual ( result7 , [
4229
+ { id : 1 , name : 'John' , age : 20 , city : 'New York' } ,
4230
+ { id : 2 , name : 'Alice' , age : 21 , city : 'New York' } ,
4231
+ ] ) ;
4232
+
4233
+ const condition8 = {
4234
+ term1 : '' ,
4235
+ term2 : 'non-empty string' ,
4236
+ } ;
4237
+
4238
+ const result8 = await db . select ( ) . from ( users ) . where (
4239
+ or ( lt ( users . age , 21 ) . if ( condition8 . term1 ) , eq ( users . city , 'London' ) . if ( condition8 . term2 ) ) ,
4240
+ ) ;
4241
+
4242
+ t . deepEqual ( result8 , [
4243
+ { id : 3 , name : 'Nick' , age : 22 , city : 'London' } ,
4244
+ { id : 4 , name : 'Lina' , age : 23 , city : 'London' } ,
4245
+ ] ) ;
4246
+
4247
+ const condition9 = {
4248
+ term1 : 1 ,
4249
+ term2 : true ,
4250
+ } ;
4251
+
4252
+ const result9 = await db . select ( ) . from ( users ) . where (
4253
+ and ( inArray ( users . city , [ 'New York' , 'London' ] ) . if ( condition9 . term1 ) , ilike ( users . name , 'a%' ) . if ( condition9 . term2 ) ) ,
4254
+ ) ;
4255
+
4256
+ t . deepEqual ( result9 , [
4257
+ { id : 2 , name : 'Alice' , age : 21 , city : 'New York' } ,
4258
+ ] ) ;
4259
+
4260
+ const condition10 = {
4261
+ term1 : 4 ,
4262
+ term2 : 19 ,
4263
+ } ;
4264
+
4265
+ const result10 = await db . select ( ) . from ( users ) . where (
4266
+ and (
4267
+ sql `length(${ users . name } ) <= ${ condition10 . term1 } ` . if ( condition10 . term1 ) ,
4268
+ gt ( users . age , condition10 . term2 ) . if ( condition10 . term2 > 20 ) ,
4269
+ ) ,
4270
+ ) ;
4271
+
4272
+ t . deepEqual ( result10 , [
4273
+ { id : 1 , name : 'John' , age : 20 , city : 'New York' } ,
4274
+ { id : 3 , name : 'Nick' , age : 22 , city : 'London' } ,
4275
+ { id : 4 , name : 'Lina' , age : 23 , city : 'London' } ,
4276
+ ] ) ;
4277
+
4278
+ const condition11 = true ;
4279
+
4280
+ const result11 = await db . select ( ) . from ( users ) . where (
4281
+ or ( eq ( users . city , 'New York' ) , gte ( users . age , 22 ) ) ! . if ( condition11 ) ,
4282
+ ) ;
4283
+
4284
+ t . deepEqual ( result11 , [
4285
+ { id : 1 , name : 'John' , age : 20 , city : 'New York' } ,
4286
+ { id : 2 , name : 'Alice' , age : 21 , city : 'New York' } ,
4287
+ { id : 3 , name : 'Nick' , age : 22 , city : 'London' } ,
4288
+ { id : 4 , name : 'Lina' , age : 23 , city : 'London' } ,
4289
+ ] ) ;
4290
+
4291
+ const condition12 = false ;
4292
+
4293
+ const result12 = await db . select ( ) . from ( users ) . where (
4294
+ and ( eq ( users . city , 'London' ) , gte ( users . age , 23 ) ) ! . if ( condition12 ) ,
4295
+ ) ;
4296
+
4297
+ t . deepEqual ( result12 , [
4298
+ { id : 1 , name : 'John' , age : 20 , city : 'New York' } ,
4299
+ { id : 2 , name : 'Alice' , age : 21 , city : 'New York' } ,
4300
+ { id : 3 , name : 'Nick' , age : 22 , city : 'London' } ,
4301
+ { id : 4 , name : 'Lina' , age : 23 , city : 'London' } ,
4302
+ ] ) ;
4303
+
4304
+ const condition13 = true ;
4305
+
4306
+ const result13 = await db . select ( ) . from ( users ) . where ( sql `(city = 'New York' or age >= 22)` . if ( condition13 ) ) ;
4307
+
4308
+ t . deepEqual ( result13 , [
4309
+ { id : 1 , name : 'John' , age : 20 , city : 'New York' } ,
4310
+ { id : 2 , name : 'Alice' , age : 21 , city : 'New York' } ,
4311
+ { id : 3 , name : 'Nick' , age : 22 , city : 'London' } ,
4312
+ { id : 4 , name : 'Lina' , age : 23 , city : 'London' } ,
4313
+ ] ) ;
4314
+
4315
+ const condition14 = false ;
4316
+
4317
+ const result14 = await db . select ( ) . from ( users ) . where ( sql `(city = 'London' and age >= 23)` . if ( condition14 ) ) ;
4318
+
4319
+ t . deepEqual ( result14 , [
4320
+ { id : 1 , name : 'John' , age : 20 , city : 'New York' } ,
4321
+ { id : 2 , name : 'Alice' , age : 21 , city : 'New York' } ,
4322
+ { id : 3 , name : 'Nick' , age : 22 , city : 'London' } ,
4323
+ { id : 4 , name : 'Lina' , age : 23 , city : 'London' } ,
4324
+ ] ) ;
4325
+
4326
+ await db . execute ( sql `drop table ${ users } ` ) ;
4327
+ } ) ;
0 commit comments