@platformatic/sql-mapper 0.33.1 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/entity.js +4 -2
- package/lib/queries/mysql-shared.js +2 -0
- package/lib/queries/pg.js +2 -0
- package/lib/queries/sqlite.js +2 -0
- package/mapper.d.ts +4 -0
- package/package.json +2 -2
- package/test/where.test.js +142 -0
package/lib/entity.js
CHANGED
|
@@ -200,6 +200,7 @@ function createMapper (defaultDb, sql, log, table, fields, primaryKeys, relation
|
|
|
200
200
|
lt: '<',
|
|
201
201
|
lte: '<=',
|
|
202
202
|
like: 'LIKE',
|
|
203
|
+
ilike: 'ILIKE',
|
|
203
204
|
any: 'ANY',
|
|
204
205
|
all: 'ALL'
|
|
205
206
|
}
|
|
@@ -241,14 +242,15 @@ function createMapper (defaultDb, sql, log, table, fields, primaryKeys, relation
|
|
|
241
242
|
criteria.push(sql`${sql.ident(field)} IS NULL`)
|
|
242
243
|
} else if (operator === '<>' && value[key] === null) {
|
|
243
244
|
criteria.push(sql`${sql.ident(field)} IS NOT NULL`)
|
|
244
|
-
} else if (operator === 'LIKE') {
|
|
245
|
+
} else if (operator === 'LIKE' || operator === 'ILIKE') {
|
|
245
246
|
let leftHand = sql.ident(field)
|
|
246
247
|
// NOTE: cast fields AS CHAR(64) and TRIM the whitespaces
|
|
247
248
|
// to prevent errors with fields different than VARCHAR & TEXT
|
|
248
249
|
if (!['text', 'varchar'].includes(fieldWrap.sqlType)) {
|
|
249
250
|
leftHand = sql`TRIM(CAST(${sql.ident(field)} AS CHAR(64)))`
|
|
250
251
|
}
|
|
251
|
-
|
|
252
|
+
const like = operator === 'LIKE' ? sql`LIKE` : queries.hasILIKE ? sql`ILIKE` : sql`LIKE`
|
|
253
|
+
criteria.push(sql`${leftHand} ${like} ${value[key]}`)
|
|
252
254
|
} else if (operator === 'ANY' || operator === 'ALL') {
|
|
253
255
|
throw new Error('Unsupported operator for non Array field')
|
|
254
256
|
} else {
|
package/lib/queries/pg.js
CHANGED
package/lib/queries/sqlite.js
CHANGED
package/mapper.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/sql-mapper",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.34.0",
|
|
4
4
|
"description": "A data mapper utility for SQL databases",
|
|
5
5
|
"main": "mapper.js",
|
|
6
6
|
"types": "mapper.d.ts",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"camelcase": "^6.3.0",
|
|
30
30
|
"fastify-plugin": "^4.5.0",
|
|
31
31
|
"inflected": "^2.1.0",
|
|
32
|
-
"@platformatic/types": "0.
|
|
32
|
+
"@platformatic/types": "0.34.0"
|
|
33
33
|
},
|
|
34
34
|
"tsd": {
|
|
35
35
|
"directory": "test/types"
|
package/test/where.test.js
CHANGED
|
@@ -740,3 +740,145 @@ test('LIKE', async ({ pass, teardown, same, equal }) => {
|
|
|
740
740
|
|
|
741
741
|
same(await entity.find({ where: { counter: { like: null } } }), [], 'where: { counter: { like: null } }')
|
|
742
742
|
})
|
|
743
|
+
|
|
744
|
+
test('ILIKE', async ({ pass, teardown, same, equal }) => {
|
|
745
|
+
const mapper = await connect({
|
|
746
|
+
...connInfo,
|
|
747
|
+
log: fakeLogger,
|
|
748
|
+
async onDatabaseLoad (db, sql) {
|
|
749
|
+
teardown(() => db.dispose())
|
|
750
|
+
pass('onDatabaseLoad called')
|
|
751
|
+
|
|
752
|
+
await clear(db, sql)
|
|
753
|
+
|
|
754
|
+
if (isSQLite) {
|
|
755
|
+
await db.query(sql`CREATE TABLE posts (
|
|
756
|
+
id INTEGER PRIMARY KEY,
|
|
757
|
+
title VARCHAR(42),
|
|
758
|
+
long_text TEXT,
|
|
759
|
+
counter INTEGER
|
|
760
|
+
);`)
|
|
761
|
+
} else {
|
|
762
|
+
await db.query(sql`CREATE TABLE posts (
|
|
763
|
+
id SERIAL PRIMARY KEY,
|
|
764
|
+
title VARCHAR(42),
|
|
765
|
+
long_text TEXT,
|
|
766
|
+
counter INTEGER
|
|
767
|
+
);`)
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
})
|
|
771
|
+
|
|
772
|
+
const entity = mapper.entities.post
|
|
773
|
+
|
|
774
|
+
const posts = [
|
|
775
|
+
{
|
|
776
|
+
title: 'Dog',
|
|
777
|
+
longText: 'The Dog barks',
|
|
778
|
+
counter: 1
|
|
779
|
+
},
|
|
780
|
+
{
|
|
781
|
+
title: 'Cat',
|
|
782
|
+
longText: 'The Cat meows',
|
|
783
|
+
counter: 2
|
|
784
|
+
},
|
|
785
|
+
{
|
|
786
|
+
title: 'Potato',
|
|
787
|
+
longText: 'The Potato is vegetable',
|
|
788
|
+
counter: 3
|
|
789
|
+
},
|
|
790
|
+
{
|
|
791
|
+
title: 'Atmosphere',
|
|
792
|
+
longText: 'The atmosphere is not a sphere',
|
|
793
|
+
counter: 4
|
|
794
|
+
},
|
|
795
|
+
{
|
|
796
|
+
title: 'planet',
|
|
797
|
+
longText: 'The planet have atmosphere',
|
|
798
|
+
counter: 14
|
|
799
|
+
}
|
|
800
|
+
]
|
|
801
|
+
|
|
802
|
+
await entity.insert({
|
|
803
|
+
inputs: posts
|
|
804
|
+
})
|
|
805
|
+
|
|
806
|
+
same(await entity.find({ where: { title: { ilike: '%at' } } }), [{
|
|
807
|
+
id: '2',
|
|
808
|
+
title: 'Cat',
|
|
809
|
+
longText: 'The Cat meows',
|
|
810
|
+
counter: 2
|
|
811
|
+
}], 'where: { title: { like: \'%at\' } }')
|
|
812
|
+
|
|
813
|
+
same(await entity.find({ where: { title: { ilike: '%at%' } } }), [{
|
|
814
|
+
id: '2',
|
|
815
|
+
title: 'Cat',
|
|
816
|
+
longText: 'The Cat meows',
|
|
817
|
+
counter: 2
|
|
818
|
+
},
|
|
819
|
+
{
|
|
820
|
+
id: '3',
|
|
821
|
+
title: 'Potato',
|
|
822
|
+
longText: 'The Potato is vegetable',
|
|
823
|
+
counter: 3
|
|
824
|
+
},
|
|
825
|
+
{
|
|
826
|
+
id: '4',
|
|
827
|
+
title: 'Atmosphere',
|
|
828
|
+
longText: 'The atmosphere is not a sphere',
|
|
829
|
+
counter: 4
|
|
830
|
+
}], 'where: { title: { ilike: \'%at%\' } }')
|
|
831
|
+
|
|
832
|
+
same(await entity.find({ where: { title: { ilike: 'at%' } } }), [{
|
|
833
|
+
id: '4',
|
|
834
|
+
title: 'Atmosphere',
|
|
835
|
+
longText: 'The atmosphere is not a sphere',
|
|
836
|
+
counter: 4
|
|
837
|
+
}], 'where: { title: { ilike: \'at%\' } }')
|
|
838
|
+
|
|
839
|
+
same(await entity.find({ where: { longText: { ilike: '%is%' } } }), [{
|
|
840
|
+
id: '3',
|
|
841
|
+
title: 'Potato',
|
|
842
|
+
longText: 'The Potato is vegetable',
|
|
843
|
+
counter: 3
|
|
844
|
+
},
|
|
845
|
+
{
|
|
846
|
+
id: '4',
|
|
847
|
+
title: 'Atmosphere',
|
|
848
|
+
longText: 'The atmosphere is not a sphere',
|
|
849
|
+
counter: 4
|
|
850
|
+
}], 'where: { longText: { ilike: \'%is%\' } }')
|
|
851
|
+
|
|
852
|
+
same(await entity.find({ where: { longText: { ilike: null } } }), [], 'where: { longText: { ilike: null } }')
|
|
853
|
+
|
|
854
|
+
if (!isSQLite) {
|
|
855
|
+
same(await entity.find({ where: { counter: { ilike: 4 } } }), [{
|
|
856
|
+
id: '4',
|
|
857
|
+
title: 'Atmosphere',
|
|
858
|
+
longText: 'The atmosphere is not a sphere',
|
|
859
|
+
counter: 4
|
|
860
|
+
}], 'where: { counter: { ilike: 4 } }')
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
same(await entity.find({ where: { counter: { ilike: '%4' } } }), [{
|
|
864
|
+
id: '4',
|
|
865
|
+
title: 'Atmosphere',
|
|
866
|
+
longText: 'The atmosphere is not a sphere',
|
|
867
|
+
counter: 4
|
|
868
|
+
},
|
|
869
|
+
{
|
|
870
|
+
id: '5',
|
|
871
|
+
title: 'planet',
|
|
872
|
+
longText: 'The planet have atmosphere',
|
|
873
|
+
counter: 14
|
|
874
|
+
}], 'where: { counter: { ilike: \'%4\' } }')
|
|
875
|
+
|
|
876
|
+
same(await entity.find({ where: { counter: { ilike: '4%' } } }), [{
|
|
877
|
+
id: '4',
|
|
878
|
+
title: 'Atmosphere',
|
|
879
|
+
longText: 'The atmosphere is not a sphere',
|
|
880
|
+
counter: 4
|
|
881
|
+
}], 'where: { counter: { ilike: \'4%\' } }')
|
|
882
|
+
|
|
883
|
+
same(await entity.find({ where: { counter: { ilike: null } } }), [], 'where: { counter: { ilike: null } }')
|
|
884
|
+
})
|