@dwtechs/antity-pgsql 0.17.5 → 0.17.7

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/README.md CHANGED
@@ -152,7 +152,7 @@ type Row = Record<string, string | number | boolean | Date | number[]>;
152
152
 
153
153
  type Comparator =
154
154
  "=" | "<" | ">" | "<=" | ">=" | "<>" |
155
- "IS" | "IS NOT" | "IN" | "NOT IN" | "LIKE" | "NOT LIKE";
155
+ "IS" | "IS NOT" | "IN" | "NOT IN" | "LIKE" | "NOT LIKE" | "&&";
156
156
 
157
157
  type MatchMode =
158
158
  "startsWith" |
@@ -164,6 +164,7 @@ type MatchMode =
164
164
  "between" |
165
165
  "in" |
166
166
  "notIn" |
167
+ "&&" | // array overlap — use with array-typed columns; generates: column && ARRAY[$1,$2]
167
168
  "lt" |
168
169
  "lte" |
169
170
  "gt" |
@@ -60,9 +60,9 @@ export declare class Property extends BaseProperty {
60
60
  }
61
61
 
62
62
  export type LogicalOperator = "AND" | "OR";
63
- export type Comparator = "=" | "<" | ">" | "<=" | ">=" | "<>" | "IS" | "IS NOT" | "IN" | "NOT IN" | "LIKE" | "NOT LIKE";
64
- export type MatchMode = "startsWith" | "endsWith" | "contains" | "notContains" | "equals" | "notEquals" | "between" | "in" | "notIn" | "lt" | "lte" | "gt" | "gte" | "is" | "isNot" | "before" | "after" | "st_contains" | "st_dwithin" | Comparator;
65
- export type MappedType = "string" | "number" | "date";
63
+ export type Comparator = "=" | "<" | ">" | "<=" | ">=" | "<>" | "IS" | "IS NOT" | "IN" | "NOT IN" | "LIKE" | "NOT LIKE" | "&&";
64
+ export type MatchMode = "startsWith" | "endsWith" | "contains" | "notContains" | "equals" | "notEquals" | "between" | "in" | "notIn" | "&&" | "lt" | "lte" | "gt" | "gte" | "is" | "isNot" | "before" | "after" | "st_contains" | "st_dwithin" | Comparator;
65
+ export type MappedType = "string" | "number" | "date" | "array";
66
66
  export type Geometry = {
67
67
  lng: number;
68
68
  lat: number;
@@ -24,7 +24,7 @@ SOFTWARE.
24
24
  https://github.com/DWTechs/Antity-pgsql.js
25
25
  */
26
26
 
27
- import { isArray, isString } from '@dwtechs/checkard';
27
+ import { isArray, isInteger, isString } from '@dwtechs/checkard';
28
28
  import { deleteProps, chunk, flatten } from '@dwtechs/sparray';
29
29
  import { log } from '@dwtechs/winstan';
30
30
  import { Entity } from '@dwtechs/antity';
@@ -104,7 +104,7 @@ function quoteIfUppercase(word) {
104
104
  return word;
105
105
  }
106
106
 
107
- function index(index, matchMode) {
107
+ function index(index, matchMode, value) {
108
108
  const i = index.map((i) => `$${i}`);
109
109
  switch (matchMode) {
110
110
  case "in":
@@ -112,12 +112,16 @@ function index(index, matchMode) {
112
112
  case "IN":
113
113
  case "NOT IN":
114
114
  return `(${i})`;
115
+ case "&&": {
116
+ const cast = isArray(value, ">", 0) && isInteger(value[0]) ? '::integer[]' : '';
117
+ return `ARRAY[${i}]${cast}`;
118
+ }
115
119
  default:
116
120
  return `${i}`;
117
121
  }
118
122
  }
119
123
 
120
- const COMPARATORS = new Set(["=", "<", ">", "<=", ">=", "<>", "IS", "IS NOT", "IN", "NOT IN", "LIKE", "NOT LIKE"]);
124
+ const COMPARATORS = new Set(["=", "<", ">", "<=", ">=", "<>", "IS", "IS NOT", "IN", "NOT IN", "LIKE", "NOT LIKE", "&&"]);
121
125
  function comparator(matchMode) {
122
126
  if (matchMode && COMPARATORS.has(matchMode))
123
127
  return matchMode;
@@ -154,6 +158,8 @@ function comparator(matchMode) {
154
158
  return "<";
155
159
  case "after":
156
160
  return ">";
161
+ case "&&":
162
+ return "&&";
157
163
  default:
158
164
  return null;
159
165
  }
@@ -197,7 +203,7 @@ function add(filters) {
197
203
  if (shouldSkipValue(value, matchMode))
198
204
  continue;
199
205
  const indexes = isArray(value) ? value.map(() => i++) : [i++];
200
- const cond = addOne(k, indexes, matchMode);
206
+ const cond = addOne(k, indexes, matchMode, value);
201
207
  if (cond) {
202
208
  groupConditions.push(cond);
203
209
  if (isArray(value))
@@ -217,10 +223,10 @@ function add(filters) {
217
223
  }
218
224
  return { conditions, args };
219
225
  }
220
- function addOne(key, indexes, matchMode) {
226
+ function addOne(key, indexes, matchMode, value) {
221
227
  const sqlKey = `${quoteIfUppercase(key)}`;
222
228
  const comparator$1 = comparator(matchMode);
223
- const index$1 = index(indexes, matchMode);
229
+ const index$1 = index(indexes, matchMode, value);
224
230
  return comparator$1 ? `${sqlKey} ${comparator$1} ${index$1}` : "";
225
231
  }
226
232
 
@@ -524,6 +530,8 @@ function type(type) {
524
530
  return s;
525
531
  case "object":
526
532
  return s;
533
+ case "array":
534
+ return "array";
527
535
  default:
528
536
  return s;
529
537
  }
@@ -533,6 +541,7 @@ const matchModes = {
533
541
  string: new Set(["startsWith", "contains", "endsWith", "notContains", "equals", "notEquals", "lt", "lte", "gt", "gte", "in", "notIn"]),
534
542
  number: new Set(["equals", "notEquals", "lt", "lte", "gt", "gte", "in", "notIn"]),
535
543
  date: new Set(["is", "isNot", "dateAfter"]),
544
+ array: new Set(["&&"]),
536
545
  };
537
546
  function matchMode(type, matchMode) {
538
547
  return COMPARATORS.has(matchMode) || matchModes[type].has(matchMode);
@@ -555,6 +564,12 @@ function cleanFilters(filters, properties) {
555
564
  const type$1 = type(prop.type);
556
565
  const filterValue = filters[k];
557
566
  const filterArray = isArray(filterValue) ? filterValue : [filterValue];
567
+ if (type$1 === "array") {
568
+ for (const f of filterArray) {
569
+ if (f.matchMode === "in")
570
+ f.matchMode = "&&";
571
+ }
572
+ }
558
573
  const validFilters = filterArray.filter((f) => {
559
574
  const { matchMode: matchMode$1 } = f;
560
575
  if (!matchMode$1 || !matchMode(type$1, matchMode$1)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity-pgsql",
3
- "version": "0.17.5",
3
+ "version": "0.17.7",
4
4
  "description": "Open source library to add PostgreSQL support to @dwtechs/Antity entities.",
5
5
  "keywords": [
6
6
  "entities"