prostgles-server 4.2.483 → 4.2.484

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.
Files changed (40) hide show
  1. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +1 -0
  2. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts.map +1 -1
  3. package/dist/DboBuilder/ViewHandler/parseComplexFilter.d.ts.map +1 -1
  4. package/dist/DboBuilder/ViewHandler/parseComplexFilter.js +3 -2
  5. package/dist/DboBuilder/ViewHandler/parseComplexFilter.js.map +1 -1
  6. package/dist/DboBuilder/ViewHandler/prepareWhere.d.ts +1 -0
  7. package/dist/DboBuilder/ViewHandler/prepareWhere.d.ts.map +1 -1
  8. package/dist/DboBuilder/ViewHandler/prepareWhere.js +26 -13
  9. package/dist/DboBuilder/ViewHandler/prepareWhere.js.map +1 -1
  10. package/dist/DboBuilder/getCondition.d.ts +1 -0
  11. package/dist/DboBuilder/getCondition.d.ts.map +1 -1
  12. package/dist/DboBuilder/getCondition.js +10 -8
  13. package/dist/DboBuilder/getCondition.js.map +1 -1
  14. package/dist/DboBuilder/getSubscribeRelatedTables.d.ts.map +1 -1
  15. package/dist/DboBuilder/getSubscribeRelatedTables.js +1 -1
  16. package/dist/DboBuilder/getSubscribeRelatedTables.js.map +1 -1
  17. package/dist/{Filtering.d.ts → Filtering/Filtering.d.ts} +5 -7
  18. package/dist/Filtering/Filtering.d.ts.map +1 -0
  19. package/dist/Filtering/Filtering.js +244 -0
  20. package/dist/Filtering/Filtering.js.map +1 -0
  21. package/dist/Filtering/getFilterItemCondition.d.ts +7 -0
  22. package/dist/Filtering/getFilterItemCondition.d.ts.map +1 -0
  23. package/dist/Filtering/getFilterItemCondition.js +186 -0
  24. package/dist/Filtering/getFilterItemCondition.js.map +1 -0
  25. package/dist/Filtering/parseFilterRightValue.d.ts +8 -0
  26. package/dist/Filtering/parseFilterRightValue.d.ts.map +1 -0
  27. package/dist/Filtering/parseFilterRightValue.js +23 -0
  28. package/dist/Filtering/parseFilterRightValue.js.map +1 -0
  29. package/lib/DboBuilder/ViewHandler/parseComplexFilter.ts +2 -5
  30. package/lib/DboBuilder/ViewHandler/prepareWhere.ts +30 -15
  31. package/lib/DboBuilder/getCondition.ts +15 -13
  32. package/lib/DboBuilder/getSubscribeRelatedTables.ts +3 -1
  33. package/lib/Filtering/Filtering.ts +286 -0
  34. package/lib/Filtering/getFilterItemCondition.ts +205 -0
  35. package/lib/Filtering/parseFilterRightValue.ts +24 -0
  36. package/package.json +1 -1
  37. package/dist/Filtering.d.ts.map +0 -1
  38. package/dist/Filtering.js +0 -418
  39. package/dist/Filtering.js.map +0 -1
  40. package/lib/Filtering.ts +0 -468
@@ -0,0 +1,286 @@
1
+ import type { FullFilter } from "prostgles-types";
2
+ import {
3
+ BetweenFilterKeys,
4
+ CompareFilterKeys,
5
+ CompareInFilterKeys,
6
+ JsonbFilterKeys,
7
+ TextFilterKeys,
8
+ isDefined,
9
+ isEmpty,
10
+ } from "prostgles-types";
11
+ import { pgp } from "../DboBuilder/DboBuilderTypes";
12
+ import { type SelectItemValidated } from "../DboBuilder/QueryBuilder/QueryBuilder";
13
+ import { getFilterItemCondition } from "./getFilterItemCondition";
14
+
15
+ export const FILTER_OPERANDS = [
16
+ ...TextFilterKeys,
17
+ ...JsonbFilterKeys,
18
+ ...CompareFilterKeys,
19
+ ...BetweenFilterKeys,
20
+ ...CompareInFilterKeys,
21
+ ] as const;
22
+
23
+ export const FILTER_OPERAND_TO_SQL_OPERAND = Object.fromEntries(
24
+ FILTER_OPERANDS.map((filterOperand) => {
25
+ let sqlOperand = filterOperand as string;
26
+ if (filterOperand === "$eq") sqlOperand = "=";
27
+ else if (filterOperand === "$gt") sqlOperand = ">";
28
+ else if (filterOperand === "$gte") sqlOperand = ">=";
29
+ else if (filterOperand === "$lt") sqlOperand = "<";
30
+ else if (filterOperand === "$lte") sqlOperand = "<=";
31
+ else if (filterOperand === "$ne") sqlOperand = "<>";
32
+ else if (filterOperand === "$like") sqlOperand = "LIKE";
33
+ else if (filterOperand === "$ilike") sqlOperand = "ILIKE";
34
+ else if (filterOperand === "$nlike") sqlOperand = "NOT LIKE";
35
+ else if (filterOperand === "$nilike") sqlOperand = "NOT ILIKE";
36
+ else if (filterOperand === "$in") sqlOperand = "IN";
37
+ else if (filterOperand === "$nin") sqlOperand = "NOT IN";
38
+ else if (filterOperand === "$between") sqlOperand = "BETWEEN";
39
+ else if (filterOperand === "$notBetween") sqlOperand = "NOT BETWEEN";
40
+ else if (filterOperand === "$isDistinctFrom") sqlOperand = "IS DISTINCT FROM";
41
+ else if (filterOperand === "$isNotDistinctFrom") sqlOperand = "IS NOT DISTINCT FROM";
42
+ return [filterOperand, sqlOperand];
43
+ }),
44
+ ) as Record<(typeof FILTER_OPERANDS)[number], string>;
45
+
46
+ /**
47
+ * Parse a single filter
48
+ * Ensure only single key objects reach this point
49
+ */
50
+ type ParseFilterItemArgs = {
51
+ filter: FullFilter<void, void> | undefined;
52
+ select: SelectItemValidated[] | undefined;
53
+ tableAliasRaw: string | undefined;
54
+ allowedColumnNames: string[];
55
+ };
56
+
57
+ export const parseFilterItem = (
58
+ args: ParseFilterItemArgs,
59
+ ): { condition: string; columnsUsed: string[] } | undefined => {
60
+ const { filter: filterItem, select, tableAliasRaw, allowedColumnNames } = args;
61
+
62
+ if (!filterItem || isEmpty(filterItem)) return;
63
+
64
+ const makeError = (msg: string): never => {
65
+ throw `${msg}: ${JSON.stringify(filterItem, null, 2)}`;
66
+ };
67
+ const asValue = (v: any) => pgp.as.format("$1", [v]);
68
+
69
+ const filterEntries = Object.entries(filterItem);
70
+ const [firstFilterEntry, ...otherFilterEnties] = filterEntries;
71
+ if (!firstFilterEntry) {
72
+ return;
73
+
74
+ /**
75
+ * { field1: cond1, field2: cond2 }
76
+ */
77
+ } else if (otherFilterEnties.length) {
78
+ const items = filterEntries
79
+ .map(([filterKey, filterValue]) =>
80
+ parseFilterItem({
81
+ filter: { [filterKey]: filterValue },
82
+ select,
83
+ tableAliasRaw,
84
+ allowedColumnNames,
85
+ }),
86
+ )
87
+ .filter(isDefined);
88
+
89
+ const condition = items
90
+ .map((i) => i.condition)
91
+ .sort() /* sorted to ensure duplicate subscription channels are not created due to different condition order */
92
+ .join(" AND ");
93
+ const columnsUsed = items.map((i) => i.columnsUsed).flat();
94
+
95
+ return { condition, columnsUsed };
96
+ }
97
+
98
+ // const fKey: string = filterKeys[0]!;
99
+ const [firstFilterKey, firstFilterValue] = firstFilterEntry;
100
+ let selItem: SelectItemValidated | undefined;
101
+ if (select) {
102
+ selItem = select.find((s) => firstFilterKey === s.alias);
103
+ }
104
+ let rightF = firstFilterValue;
105
+
106
+ const validateSelectedItemFilter = (selectedItem: SelectItemValidated | undefined) => {
107
+ const fields = selectedItem?.fields;
108
+ if (Array.isArray(fields) && fields.length) {
109
+ const dissallowedFields = fields.filter((fname) => !allowedColumnNames.includes(fname));
110
+ if (dissallowedFields.length) {
111
+ throw new Error(
112
+ `Invalid/disallowed columns found in filter: ${dissallowedFields.join(", ")}`,
113
+ );
114
+ }
115
+ }
116
+ };
117
+ const getLeftQ = (selItm: SelectItemValidated) => {
118
+ validateSelectedItemFilter(selItem);
119
+ if (selItm.type === "function" || selItm.type === "aggregation") return selItm.getQuery();
120
+ return selItm.getQuery(tableAliasRaw);
121
+ };
122
+
123
+ /**
124
+ * Parsed left side of the query
125
+ */
126
+ let leftQ: string | undefined; // = asName(selItem.alias);
127
+
128
+ /*
129
+ Select item not found.
130
+ Check if dot/json notation. Build obj if necessary
131
+ */
132
+ const dot_notation_delims = ["->", "."];
133
+ if (!selItem) {
134
+ /* See if dot notation. Pick the best matching starting string */
135
+ if (select) {
136
+ selItem = select.find((s) =>
137
+ dot_notation_delims.find((delimiter) => firstFilterKey.startsWith(s.alias + delimiter)),
138
+ );
139
+ validateSelectedItemFilter(selItem);
140
+ }
141
+ if (!selItem) {
142
+ return makeError(
143
+ "Bad filter. Could not match to a column or alias or dot notation" +
144
+ select?.map((s) => s.alias).join(", "),
145
+ );
146
+ }
147
+
148
+ let remainingStr = firstFilterKey.slice(selItem.alias.length);
149
+
150
+ /* Is json path spec */
151
+ if (remainingStr.startsWith("->")) {
152
+ /** Has shorthand operand 'col->>key.<>' */
153
+ const matchingOperand = CompareFilterKeys.find((operand) =>
154
+ remainingStr.endsWith(`.${operand}`),
155
+ );
156
+ if (matchingOperand) {
157
+ remainingStr = remainingStr.slice(0, -matchingOperand.length - 1);
158
+ rightF = { [matchingOperand]: rightF };
159
+ }
160
+
161
+ leftQ = getLeftQ(selItem);
162
+
163
+ /**
164
+ * get json path separators. Expecting -> to come first
165
+ */
166
+ type GetSepRes = { idx: number; sep: string } | undefined;
167
+ const getSep = (fromIdx = 0): GetSepRes => {
168
+ const strPart = remainingStr.slice(fromIdx);
169
+ let idx = strPart.indexOf("->");
170
+ const idxx = strPart.indexOf("->>");
171
+ if (idx > -1) {
172
+ /* if -> matches then check if it's the last separator */
173
+ if (idx === idxx) return { idx: idx + fromIdx, sep: "->>" };
174
+ return { idx: idx + fromIdx, sep: "->" };
175
+ }
176
+ idx = strPart.indexOf("->>");
177
+ if (idx > -1) {
178
+ return { idx: idx + fromIdx, sep: "->>" };
179
+ }
180
+
181
+ return undefined;
182
+ };
183
+
184
+ let currSep = getSep();
185
+ while (currSep) {
186
+ let nextSep = getSep(currSep.idx + currSep.sep.length);
187
+
188
+ let nextIdx = nextSep ? nextSep.idx : remainingStr.length;
189
+
190
+ /* If ending in set then add set as well into key */
191
+ if (nextSep && nextIdx + nextSep.sep.length === remainingStr.length) {
192
+ nextIdx = remainingStr.length;
193
+ nextSep = undefined;
194
+ }
195
+
196
+ leftQ +=
197
+ currSep.sep + asValue(remainingStr.slice(currSep.idx + currSep.sep.length, nextIdx));
198
+ currSep = nextSep;
199
+ }
200
+
201
+ /*
202
+ Is collapsed filter spec e.g. { "col.$ilike": 'text' }
203
+ will transform into { col: { $ilike: ['text'] } }
204
+ */
205
+ } else if (remainingStr.startsWith(".")) {
206
+ leftQ = getLeftQ(selItem);
207
+
208
+ const getSep = (fromIdx = 0) => {
209
+ const idx = remainingStr.slice(fromIdx).indexOf(".");
210
+ if (idx > -1) return fromIdx + idx;
211
+ return idx;
212
+ };
213
+ let currIdx = getSep();
214
+ const res: any = {};
215
+ let curObj = res;
216
+
217
+ while (currIdx > -1) {
218
+ let nextIdx = getSep(currIdx + 1);
219
+ let nIdx = nextIdx > -1 ? nextIdx : remainingStr.length;
220
+
221
+ /* If ending in dot then add dot as well into key */
222
+ if (nextIdx + 1 === remainingStr.length) {
223
+ nIdx = remainingStr.length;
224
+ nextIdx = -1;
225
+ }
226
+
227
+ const key = remainingStr.slice(currIdx + 1, nIdx);
228
+ curObj[key] = nextIdx > -1 ? {} : firstFilterValue;
229
+ curObj = curObj[key];
230
+
231
+ currIdx = nextIdx;
232
+ }
233
+
234
+ rightF = res;
235
+ } else {
236
+ // console.trace(141, select, selItem, remainingStr)
237
+ makeError("Bad filter. Could not find the valid col name or alias or col json path");
238
+ }
239
+ } else {
240
+ leftQ = getLeftQ(selItem);
241
+ }
242
+
243
+ if (!leftQ) {
244
+ makeError("Internal error: leftQ missing?!");
245
+ return;
246
+ }
247
+
248
+ try {
249
+ const condition = getFilterItemCondition({
250
+ selItem,
251
+ leftQ,
252
+ rightF,
253
+ });
254
+ return {
255
+ condition,
256
+ columnsUsed: selItem.fields,
257
+ };
258
+ } catch (e) {
259
+ return makeError((e as Error).message);
260
+ }
261
+ };
262
+
263
+ // ensure pgp is not NULL!!!
264
+ // const asValue = v => v;// pgp.as.value;
265
+
266
+ // const filters: FilterSpec[] = [
267
+ // ...(["ilike", "like"].map(op => ({
268
+ // operands: ["$" + op],
269
+ // tsDataTypes: ["any"] as TSDataType[],
270
+ // tsDefinition: ` { $${op}: string } `,
271
+ // // data_types:
272
+ // getQuery: (leftQuery: string, rightVal: any) => {
273
+ // return `${leftQuery}::text ${op.toUpperCase()} ${asValue(rightVal)}::text`
274
+ // }
275
+ // }))),
276
+ // {
277
+ // operands: ["", "="],
278
+ // tsDataTypes: ["any"],
279
+ // tsDefinition: ` { "=": any } | any `,
280
+ // // data_types:
281
+ // getQuery: (leftQuery: string, rightVal: any) => {
282
+ // if(rightVal === null) return`${leftQuery} IS NULL `;
283
+ // return `${leftQuery} = ${asValue(rightVal)}`;
284
+ // }
285
+ // }
286
+ // ];
@@ -0,0 +1,205 @@
1
+ import {
2
+ GeomFilterKeys,
3
+ GeomFilter_Funcs,
4
+ TextFilterKeys,
5
+ TextFilter_FullTextSearchFilterKeys,
6
+ includes,
7
+ isDefined,
8
+ isObject,
9
+ } from "prostgles-types";
10
+ import { parseFilterRightValue } from "./parseFilterRightValue";
11
+ import { FILTER_OPERAND_TO_SQL_OPERAND, FILTER_OPERANDS } from "./Filtering";
12
+ import type { SelectItemValidated } from "../DboBuilder/QueryBuilder/QueryBuilder";
13
+ import { pgp } from "../DboBuilder/DboBuilderTypes";
14
+
15
+ export const getFilterItemCondition = ({
16
+ selItem,
17
+ leftQ,
18
+ rightF,
19
+ }: {
20
+ leftQ: string;
21
+ selItem: SelectItemValidated;
22
+ rightF: unknown;
23
+ }): string => {
24
+ const asValue = (v: any) => pgp.as.format("$1", [v]);
25
+
26
+ const parseRightVal = (val: any, expect?: "csv" | "array" | "json" | "jsonb") => {
27
+ try {
28
+ return parseFilterRightValue(val, { selectItem: selItem, expect });
29
+ } catch (e: any) {
30
+ throw new Error(e);
31
+ }
32
+ };
33
+
34
+ /* Matching sel item */
35
+ if (isObject(rightF)) {
36
+ const filterKeys = Object.keys(rightF);
37
+ let filterOperand = filterKeys[0] as (typeof FILTER_OPERANDS)[number];
38
+
39
+ /** JSON cannot be compared so we'll cast it to TEXT */
40
+ if (selItem.column_udt_type === "json" || includes(TextFilterKeys, filterOperand)) {
41
+ leftQ += "::TEXT ";
42
+ }
43
+
44
+ /** It's an object key which means it's an equality comparison against a json object */
45
+ if (selItem.column_udt_type?.startsWith("json") && !includes(FILTER_OPERANDS, filterOperand)) {
46
+ return leftQ + " = " + parseRightVal(rightF);
47
+ }
48
+
49
+ let filterValue = rightF[filterOperand] as string | null | number | Date | any[];
50
+ const ALLOWED_FUNCS = [...GeomFilter_Funcs, ...TextFilter_FullTextSearchFilterKeys] as const;
51
+ let funcName: undefined | (typeof ALLOWED_FUNCS)[number];
52
+ let funcArgs: undefined | any[];
53
+
54
+ if (
55
+ selItem.column_udt_type === "interval" &&
56
+ isObject(rightF) &&
57
+ Object.values(rightF).every((v) => Number.isFinite(v))
58
+ ) {
59
+ filterOperand = "=";
60
+ filterValue = Object.entries(rightF)
61
+ .map(([k, v]) => `${v}${k}`)
62
+ .join(" ");
63
+ } else if (
64
+ (filterKeys.length !== 1 || !isDefined(filterOperand)) &&
65
+ selItem.column_udt_type !== "jsonb"
66
+ ) {
67
+ throw new Error("Bad filter. Expecting one key only");
68
+ } else if (isObject(filterValue) && !(filterValue instanceof Date)) {
69
+ /**
70
+ * Filter notation
71
+ * geom && st_makeenvelope(funcArgs)
72
+ */
73
+ const filterValueKeys = Object.keys(filterValue);
74
+ funcName = filterValueKeys[0] as any;
75
+ if (includes(ALLOWED_FUNCS, funcName)) {
76
+ funcArgs = filterValue[funcName as any];
77
+ } else {
78
+ funcName = undefined;
79
+ }
80
+ }
81
+
82
+ /** st_makeenvelope */
83
+ if (
84
+ includes(GeomFilterKeys, filterOperand) &&
85
+ funcName &&
86
+ includes(GeomFilter_Funcs, funcName)
87
+ ) {
88
+ /**
89
+ * If leftQ is geography then:
90
+ * - err can happen: 'Antipodal (180 degrees long) edge detected!'
91
+ * - inacurrate results at large envelopes due to the curvature of the earth
92
+ * https://gis.stackexchange.com/questions/78816/maximum-size-on-the-bounding-box-with-st-makeenvelope-and-and-geography-colum
93
+ */
94
+ if (funcName.toLowerCase() === "st_makeenvelope") {
95
+ leftQ += "::geometry";
96
+ }
97
+
98
+ return `${leftQ} ${filterOperand} ${funcName}${parseRightVal(funcArgs, "csv")}`;
99
+ } else if (["=", "$eq"].includes(filterOperand) && !funcName) {
100
+ if (filterValue === null) return leftQ + " IS NULL ";
101
+ return leftQ + " = " + parseRightVal(filterValue);
102
+ } else if (["<>", "$ne"].includes(filterOperand)) {
103
+ if (filterValue === null) return leftQ + " IS NOT NULL ";
104
+ return leftQ + " <> " + parseRightVal(filterValue);
105
+ } else if ([">", "$gt"].includes(filterOperand)) {
106
+ return leftQ + " > " + parseRightVal(filterValue);
107
+ } else if (["<", "$lt"].includes(filterOperand)) {
108
+ return leftQ + " < " + parseRightVal(filterValue);
109
+ } else if ([">=", "$gte"].includes(filterOperand)) {
110
+ return leftQ + " >= " + parseRightVal(filterValue);
111
+ } else if (["<=", "$lte"].includes(filterOperand)) {
112
+ return leftQ + " <= " + parseRightVal(filterValue);
113
+ } else if (["$in", "$nin"].includes(filterOperand)) {
114
+ const isIn = filterOperand === "$in";
115
+ if (filterValue !== null && !Array.isArray(filterValue)) {
116
+ throw new Error("In filter expects an array");
117
+ }
118
+ if (!filterValue?.length) {
119
+ return isIn ? " FALSE " : " TRUE ";
120
+ }
121
+
122
+ const nonNullFilterValues = filterValue.filter((v) => v !== null);
123
+ const conditions: string[] = [];
124
+ if (nonNullFilterValues.length) {
125
+ conditions.push(
126
+ leftQ + (isIn ? " IN " : " NOT IN ") + parseRightVal(nonNullFilterValues, "csv"),
127
+ );
128
+ }
129
+ if (filterValue.includes(null)) {
130
+ const comparator = isIn ? " IS NULL " : " IS NOT NULL ";
131
+ conditions.push(` ${leftQ} ${comparator} `);
132
+ }
133
+ const joinedConditions = conditions.join(isIn ? " OR " : " AND ");
134
+ return conditions.length > 1 ? `(${joinedConditions})` : joinedConditions;
135
+ } else if (["$between"].includes(filterOperand)) {
136
+ if (!Array.isArray(filterValue) || filterValue.length !== 2) {
137
+ throw new Error("Between filter expects an array of two values");
138
+ }
139
+ return leftQ + " BETWEEN " + asValue(filterValue[0]) + " AND " + asValue(filterValue[1]);
140
+ } else if (["$ilike"].includes(filterOperand)) {
141
+ return leftQ + " ILIKE " + asValue(filterValue);
142
+ } else if (["$like"].includes(filterOperand)) {
143
+ return leftQ + " LIKE " + asValue(filterValue);
144
+ } else if (["$nilike"].includes(filterOperand)) {
145
+ return leftQ + " NOT ILIKE " + asValue(filterValue);
146
+ } else if (["$nlike"].includes(filterOperand)) {
147
+ return leftQ + " NOT LIKE " + asValue(filterValue);
148
+ } else if (filterOperand === "$isDistinctFrom" || filterOperand === "$isNotDistinctFrom") {
149
+ const operator = FILTER_OPERAND_TO_SQL_OPERAND[filterOperand];
150
+ return leftQ + ` ${operator} ` + asValue(filterValue);
151
+
152
+ /* MAYBE TEXT OR MAYBE ARRAY */
153
+ } else if (
154
+ ["@>", "<@", "$contains", "$containedBy", "$overlaps", "&&", "@@"].includes(filterOperand)
155
+ ) {
156
+ const operand =
157
+ filterOperand === "@@" ? "@@"
158
+ : ["@>", "$contains"].includes(filterOperand) ? "@>"
159
+ : ["&&", "$overlaps"].includes(filterOperand) ? "&&"
160
+ : "<@";
161
+
162
+ if (operand === "<@" || operand === "@>") {
163
+ if (selItem.column_udt_type === "jsonb") {
164
+ const filterValueAsString =
165
+ typeof filterValue === "string" ? filterValue : JSON.stringify(filterValue);
166
+ return leftQ + operand + parseRightVal(filterValueAsString);
167
+ }
168
+ }
169
+
170
+ /* Array for sure */
171
+ if (Array.isArray(filterValue)) {
172
+ return leftQ + operand + parseRightVal(filterValue, "array");
173
+
174
+ /* FTSQuery */
175
+ } else if (
176
+ ["@@"].includes(filterOperand) &&
177
+ includes(TextFilter_FullTextSearchFilterKeys, funcName)
178
+ ) {
179
+ let lq = `to_tsvector(${leftQ}::text)`;
180
+ if (selItem.columnPGDataType === "tsvector") lq = leftQ!;
181
+
182
+ const res = `${lq} ${operand} ` + `${funcName}${parseRightVal(funcArgs, "csv")}`;
183
+
184
+ return res;
185
+ } else {
186
+ throw new Error("Unrecognised filter operand: " + filterOperand + " ");
187
+ }
188
+ } else {
189
+ throw new Error("Unrecognised filter operand: " + filterOperand + " ");
190
+ }
191
+ }
192
+ /* Is an equal filter */
193
+ if (rightF === null) {
194
+ return leftQ + " IS NULL ";
195
+ }
196
+
197
+ /**
198
+ * Ensure that when comparing an array to a json column, the array is cast to json
199
+ */
200
+ let valueStr = asValue(rightF);
201
+ if (selItem.column_udt_type?.startsWith("json") && Array.isArray(rightF)) {
202
+ valueStr = pgp.as.format(`$1::jsonb`, [JSON.stringify(rightF)]);
203
+ }
204
+ return `${leftQ} = ${valueStr}`;
205
+ };
@@ -0,0 +1,24 @@
1
+ import { pgp } from "../DboBuilder/DboBuilderTypes";
2
+ import { type SelectItemValidated } from "../DboBuilder/QueryBuilder/QueryBuilder";
3
+
4
+ type ParseRightValOpts = {
5
+ expect?: "csv" | "array" | "json" | "jsonb";
6
+ selectItem: SelectItemValidated | undefined;
7
+ };
8
+ export const parseFilterRightValue = (val: any, { expect, selectItem }: ParseRightValOpts) => {
9
+ const asValue = (v: any) => pgp.as.format("$1", [v]);
10
+ const checkIfArr = () => {
11
+ if (!Array.isArray(val)) {
12
+ throw "This type of filter/column expects an Array of items";
13
+ }
14
+ };
15
+ if (expect === "csv" || expect?.startsWith("json")) {
16
+ checkIfArr();
17
+ return pgp.as.format(`($1:${expect})`, [val]);
18
+ } else if (expect === "array" || selectItem?.columnPGDataType === "ARRAY") {
19
+ checkIfArr();
20
+ return pgp.as.format(" ARRAY[$1:csv]", [val]);
21
+ }
22
+
23
+ return asValue(val);
24
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "4.2.483",
3
+ "version": "4.2.484",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1 +0,0 @@
1
- {"version":3,"file":"Filtering.d.ts","sourceRoot":"","sources":["../lib/Filtering.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAiBlE,OAAO,EAAE,KAAK,mBAAmB,EAAE,MAAM,wCAAwC,CAAC;AAElF,eAAO,MAAM,eAAe,oSAMlB,CAAC;AAEX,eAAO,MAAM,6BAA6B,EAqBrC,MAAM,CAAC,CAAC,OAAO,eAAe,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;AAEtD;;;GAGG;AACH,KAAK,mBAAmB,GAAG;IACzB,MAAM,EAAE,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,SAAS,CAAC;IAC3C,MAAM,EAAE,mBAAmB,EAAE,GAAG,SAAS,CAAC;IAC1C,aAAa,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,kBAAkB,EAAE,MAAM,EAAE,CAAC;CAC9B,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,MAAM,mBAAmB,KAAG,MAuW3D,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,MAAM,CAAC,EAAE,KAAK,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,CAAC;IAC5C,UAAU,EAAE,mBAAmB,GAAG,SAAS,CAAC;CAC7C,CAAC;AACF,eAAO,MAAM,qBAAqB,GAAI,KAAK,GAAG,EAAE,wBAAwB,iBAAiB,WAgBxF,CAAC"}