@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 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
- criteria.push(sql`${leftHand} LIKE ${value[key]}`)
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 {
@@ -115,3 +115,5 @@ module.exports = {
115
115
  updateOne,
116
116
  updateMany
117
117
  }
118
+
119
+ module.exports.hasILIKE = false
package/lib/queries/pg.js CHANGED
@@ -127,3 +127,5 @@ async function listEnumValues (db, sql, table, schema) {
127
127
  }
128
128
 
129
129
  module.exports.listEnumValues = listEnumValues
130
+
131
+ module.exports.hasILIKE = true
@@ -245,3 +245,5 @@ async function updateMany (db, sql, table, schema, criteria, input, fieldsToRetr
245
245
  }
246
246
 
247
247
  module.exports.updateMany = updateMany
248
+
249
+ module.exports.hasILIKE = false
package/mapper.d.ts CHANGED
@@ -100,6 +100,10 @@ export interface WhereCondition {
100
100
  * Like value.
101
101
  */
102
102
  like?: string
103
+ /**
104
+ * Like ignore-case value.
105
+ */
106
+ ilike?: string
103
107
  }
104
108
  }
105
109
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/sql-mapper",
3
- "version": "0.33.1",
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.33.1"
32
+ "@platformatic/types": "0.34.0"
33
33
  },
34
34
  "tsd": {
35
35
  "directory": "test/types"
@@ -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
+ })