@@ -1304,7 +1304,11 @@ abstract class PostgresAdapterBase implements DbAdapter {
1304
1304
private translateQueryValue ( vars : ValuesVariables , tkey : string , value : any , type : ValueType ) : string | undefined {
1305
1305
const tkeyData = tkey . includes ( 'data' ) && ( tkey . includes ( '->' ) || tkey . includes ( '#>>' ) )
1306
1306
if ( tkeyData && ( Array . isArray ( value ) || ( typeof value !== 'object' && typeof value !== 'string' ) ) ) {
1307
- value = Array . isArray ( value ) ? value . map ( ( it ) => ( it == null ? null : `${ it } ` ) ) : `${ value } `
1307
+ value = Array . isArray ( value )
1308
+ ? value . map ( ( it ) => ( it == null ? null : `${ it } ` ) )
1309
+ : value == null
1310
+ ? null
1311
+ : `${ value } `
1308
1312
}
1309
1313
1310
1314
if ( value === null ) {
@@ -1315,76 +1319,91 @@ abstract class PostgresAdapterBase implements DbAdapter {
1315
1319
for ( const operator in value ) {
1316
1320
let val = value [ operator ]
1317
1321
if ( tkeyData && ( Array . isArray ( val ) || ( typeof val !== 'object' && typeof val !== 'string' ) ) ) {
1318
- val = Array . isArray ( val ) ? val . map ( ( it ) => ( it == null ? null : `${ it } ` ) ) : `${ val } `
1322
+ val = Array . isArray ( val ) ? val . map ( ( it ) => ( it == null ? null : `${ it } ` ) ) : val == null ? null : `${ val } `
1319
1323
}
1324
+
1325
+ const valType = inferType ( val )
1326
+ const arrowCount = ( tkey . match ( / - > / g) ?? [ ] ) . length
1327
+ // We need to convert to type without array if pressent
1328
+ let tlkey = arrowCount > 0 ? `(${ tkey } )${ valType . replace ( '[]' , '' ) } ` : tkey
1329
+
1330
+ if ( arrowCount > 1 ) {
1331
+ // We need to replace only the last -> to ->>
1332
+ tlkey = tlkey . replace ( / - > (? ! .* - > ) / , '->>' )
1333
+ }
1334
+
1320
1335
switch ( operator ) {
1321
1336
case '$ne' :
1322
- if ( val === null ) {
1323
- res . push ( `${ tkey } IS NOT NULL` )
1337
+ if ( val == null ) {
1338
+ res . push ( `${ tlkey } IS NOT NULL` )
1324
1339
} else {
1325
- res . push ( `${ tkey } != ${ vars . add ( val , inferType ( val ) ) } ` )
1340
+ res . push ( `${ tlkey } != ${ vars . add ( val , valType ) } ` )
1326
1341
}
1327
1342
break
1328
1343
case '$gt' :
1329
- res . push ( `${ tkey } > ${ vars . add ( val , inferType ( val ) ) } ` )
1344
+ res . push ( `${ tlkey } > ${ vars . add ( val , valType ) } ` )
1330
1345
break
1331
1346
case '$gte' :
1332
- res . push ( `${ tkey } >= ${ vars . add ( val , inferType ( val ) ) } ` )
1347
+ res . push ( `${ tlkey } >= ${ vars . add ( val , valType ) } ` )
1333
1348
break
1334
1349
case '$lt' :
1335
- res . push ( `${ tkey } < ${ vars . add ( val , inferType ( val ) ) } ` )
1350
+ res . push ( `${ tlkey } < ${ vars . add ( val , valType ) } ` )
1336
1351
break
1337
1352
case '$lte' :
1338
- res . push ( `${ tkey } <= ${ vars . add ( val , inferType ( val ) ) } ` )
1353
+ res . push ( `${ tlkey } <= ${ vars . add ( val , valType ) } ` )
1339
1354
break
1340
1355
case '$in' :
1341
1356
switch ( type ) {
1342
1357
case 'common' :
1343
1358
if ( Array . isArray ( val ) && val . includes ( null ) ) {
1344
- const vv = vars . addArray ( val , inferType ( val ) )
1345
- res . push ( `(${ tkey } = ANY(${ vv } ) OR ${ tkey } IS NULL)` )
1359
+ const vv = vars . addArray ( val , valType )
1360
+ res . push ( `(${ tlkey } = ANY(${ vv } ) OR ${ tkey } IS NULL)` )
1346
1361
} else {
1347
1362
if ( val . length > 0 ) {
1348
- res . push ( `${ tkey } = ANY(${ vars . addArray ( val , inferType ( val ) ) } )` )
1363
+ res . push ( `${ tlkey } = ANY(${ vars . addArray ( val , valType ) } )` )
1349
1364
} else {
1350
- res . push ( `${ tkey } IN ('NULL')` )
1365
+ res . push ( `${ tlkey } IN ('NULL')` )
1351
1366
}
1352
1367
}
1353
1368
break
1354
1369
case 'array' :
1355
1370
{
1356
- const vv = vars . addArrayI ( val , inferType ( val ) )
1371
+ const vv = vars . addArrayI ( val , valType )
1357
1372
res . push ( `${ tkey } && ${ vv } ` )
1358
1373
}
1359
1374
break
1360
1375
case 'dataArray' :
1361
1376
{
1362
- const vv = vars . addArrayI ( val , inferType ( val ) )
1377
+ const vv = vars . addArrayI ( val , valType )
1363
1378
res . push ( `${ tkey } ?| ${ vv } ` )
1364
1379
}
1365
1380
break
1366
1381
}
1367
1382
break
1368
1383
case '$nin' :
1369
1384
if ( Array . isArray ( val ) && val . includes ( null ) ) {
1370
- res . push ( `(${ tkey } != ALL(${ vars . addArray ( val , inferType ( val ) ) } ) AND ${ tkey } IS NOT NULL)` )
1385
+ res . push ( `(${ tlkey } != ALL(${ vars . addArray ( val , valType ) } ) AND ${ tkey } IS NOT NULL)` )
1371
1386
} else if ( Array . isArray ( val ) && val . length > 0 ) {
1372
- res . push ( `${ tkey } != ALL(${ vars . addArray ( val , inferType ( val ) ) } )` )
1387
+ res . push ( `${ tlkey } != ALL(${ vars . addArray ( val , valType ) } )` )
1373
1388
}
1374
1389
break
1375
1390
case '$like' :
1376
- res . push ( `${ tkey } ILIKE ${ vars . add ( val , inferType ( val ) ) } ` )
1391
+ res . push ( `${ tlkey } ILIKE ${ vars . add ( val , valType ) } ` )
1377
1392
break
1378
1393
case '$exists' :
1379
- res . push ( `${ tkey } IS ${ val === true || val === 'true' ? 'NOT NULL' : 'NULL' } ` )
1394
+ res . push ( `${ tlkey } IS ${ val === true || val === 'true' ? 'NOT NULL' : 'NULL' } ` )
1380
1395
break
1381
1396
case '$regex' :
1382
- res . push ( `${ tkey } SIMILAR TO ${ vars . add ( val , inferType ( val ) ) } ` )
1397
+ res . push ( `${ tlkey } SIMILAR TO ${ vars . add ( val , valType ) } ` )
1383
1398
break
1384
1399
case '$options' :
1385
1400
break
1386
1401
case '$all' :
1387
- res . push ( `${ tkey } @> ${ vars . addArray ( value , inferType ( value ) ) } ` )
1402
+ if ( arrowCount > 0 ) {
1403
+ res . push ( `${ tkey } @> '${ JSON . stringify ( val ) } '::jsonb` )
1404
+ } else {
1405
+ res . push ( `${ tkey } @> ${ vars . addArray ( val , inferType ( val ) ) } ` )
1406
+ }
1388
1407
break
1389
1408
default :
1390
1409
res . push ( `${ tkey } @> '[${ JSON . stringify ( value ) } ]'` )
@@ -1394,8 +1413,16 @@ abstract class PostgresAdapterBase implements DbAdapter {
1394
1413
return res . length === 0 ? undefined : res . join ( ' AND ' )
1395
1414
}
1396
1415
1416
+ const valType = inferType ( value )
1417
+ const arrowCount = ( tkey . match ( / - > / g) ?? [ ] ) . length
1418
+ // We need to convert to type without array if pressent
1419
+ let tlkey = arrowCount > 0 ? `(${ tkey } )${ valType . replace ( '[]' , '' ) } ` : tkey
1420
+ if ( arrowCount > 1 ) {
1421
+ // We need to replace only the last -> to ->>
1422
+ tlkey = tlkey . replace ( / - > (? ! .* - > ) / , '->>' )
1423
+ }
1397
1424
return type === 'common'
1398
- ? `${ tkey } = ${ vars . add ( value , inferType ( value ) ) } `
1425
+ ? `${ tlkey } = ${ vars . add ( value , valType ) } `
1399
1426
: type === 'array'
1400
1427
? `${ tkey } @> '${ typeof value === 'string' ? '{"' + value + '"}' : value } '`
1401
1428
: `${ tkey } @> '${ typeof value === 'string' ? '"' + value + '"' : value } '`
0 commit comments