@saltcorn/db-common 0.6.1-beta.2 → 0.6.2-beta.1

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/index.js CHANGED
@@ -14,4 +14,3 @@
14
14
  *
15
15
  * @category db-common
16
16
  */
17
-
package/internal.js CHANGED
@@ -13,7 +13,9 @@ const { contract, is } = require("contractis");
13
13
  * @param {string} nm
14
14
  * @returns {string}
15
15
  */
16
- const sqlsanitize = contract(is.fun(is.str, is.str), (nm) => {
16
+ const sqlsanitize = contract(is.fun(is.or(is.str, is.any), is.str), (nm) => {
17
+ if (typeof nm === "symbol") return sqlsanitize(nm.description);
18
+
17
19
  const s = nm.replace(/[^A-Za-z_0-9]*/g, "");
18
20
  if (s[0] >= "0" && s[0] <= "9") return `_${s}`;
19
21
  else return s;
@@ -27,11 +29,15 @@ const sqlsanitize = contract(is.fun(is.str, is.str), (nm) => {
27
29
  * @param {string} nm
28
30
  * @returns {string}
29
31
  */
30
- const sqlsanitizeAllowDots = contract(is.fun(is.str, is.str), (nm) => {
31
- const s = nm.replace(/[^A-Za-z_0-9."]*/g, "");
32
- if (s[0] >= "0" && s[0] <= "9") return `_${s}`;
33
- else return s;
34
- });
32
+ const sqlsanitizeAllowDots = contract(
33
+ is.fun(is.or(is.str, is.any), is.str),
34
+ (nm) => {
35
+ if (typeof nm === "symbol") return sqlsanitizeAllowDots(s.description);
36
+ const s = nm.replace(/[^A-Za-z_0-9."]*/g, "");
37
+ if (s[0] >= "0" && s[0] <= "9") return `_${s}`;
38
+ else return s;
39
+ }
40
+ );
35
41
  /**
36
42
  *
37
43
  * @param {object} v
@@ -130,7 +136,26 @@ const whereOr = (is_sqlite, i) => (ors) =>
130
136
  )
131
137
  .join(" or ")
132
138
  );
139
+ const equals = ([v1, v2], is_sqlite, i) => {
140
+ const pVal = (v) =>
141
+ typeof v === "symbol"
142
+ ? quote(sqlsanitizeAllowDots(v.description))
143
+ : placeHolder(is_sqlite, i()) + (typeof v === "string" ? "::text" : "");
144
+ const isNull = (v) => `${pVal(v)} is null`;
145
+ if (v1 === null && v2 === null) return "null is null";
146
+ if (v1 === null) return isNull(v2);
147
+ if (v2 === null) return isNull(v1);
148
+ return `${pVal(v1)}=${pVal(v2)}`;
149
+ };
150
+ const equalsVals = (vs) => {
151
+ let vals = [];
133
152
 
153
+ vs.forEach((v) => {
154
+ if (v !== null && typeof v !== "symbol") vals.push(v);
155
+ });
156
+ //console.log({ vals });
157
+ return vals;
158
+ };
134
159
  /**
135
160
  * @param {boolean} is_sqlite
136
161
  * @param {string} i
@@ -149,6 +174,8 @@ const whereClause = (is_sqlite, i) => ([k, v]) =>
149
174
  ? `not (${Object.entries(v)
150
175
  .map((kv) => whereClause(is_sqlite, i)(kv))
151
176
  .join(" and ")})`
177
+ : k === "eq" && Array.isArray(v)
178
+ ? equals(v, is_sqlite, i)
152
179
  : v && v.or && Array.isArray(v.or)
153
180
  ? wrapParens(
154
181
  v.or.map((vi) => whereClause(is_sqlite, i)([k, vi])).join(" or ")
@@ -184,7 +211,13 @@ const whereClause = (is_sqlite, i) => ([k, v]) =>
184
211
  )}'=${placeHolder(is_sqlite, i())}`
185
212
  : v === null
186
213
  ? `${quote(sqlsanitizeAllowDots(k))} is null`
187
- : `${quote(sqlsanitizeAllowDots(k))}=${placeHolder(is_sqlite, i())}`;
214
+ : k === "not"
215
+ ? `not (${
216
+ typeof v === "symbol" ? v.description : placeHolder(is_sqlite, i())
217
+ })`
218
+ : `${quote(sqlsanitizeAllowDots(k))}=${
219
+ typeof v === "symbol" ? v.description : placeHolder(is_sqlite, i())
220
+ }`;
188
221
 
189
222
  /**
190
223
  * @param {object[]} opts
@@ -199,8 +232,13 @@ const getVal = ([k, v]) =>
199
232
  ? [v.in]
200
233
  : k === "not" && typeof v === "object"
201
234
  ? Object.entries(v).map(getVal).flat(1)
235
+ : k === "eq" && Array.isArray(v)
236
+ ? equalsVals(v).flat(1)
202
237
  : k === "or" && Array.isArray(v)
203
- ? v.map((vi) => Object.entries(vi).map(getVal)).flat(1)
238
+ ? v
239
+ .map((vi) => Object.entries(vi).map(getVal))
240
+ .flat(1)
241
+ .flat(1)
204
242
  : v && v.or && Array.isArray(v.or)
205
243
  ? v.or.map((vi) => getVal([k, vi])).flat(1)
206
244
  : Array.isArray(v)
@@ -217,6 +255,8 @@ const getVal = ([k, v]) =>
217
255
  ? null
218
256
  : typeof (v || {}).json !== "undefined"
219
257
  ? v.json[1]
258
+ : typeof v === "symbol"
259
+ ? null
220
260
  : v;
221
261
 
222
262
  /**
package/internal.test.js CHANGED
@@ -4,12 +4,18 @@ describe("sqlsanitize", () => {
4
4
  it("should not alter valid name", () => {
5
5
  expect(sqlsanitize("ffoo_oo")).toBe("ffoo_oo");
6
6
  });
7
+ it("should not alter valid symbol", () => {
8
+ expect(sqlsanitize(Symbol("ffoo_oo"))).toBe("ffoo_oo");
9
+ });
7
10
  it("should remove spaces", () => {
8
11
  expect(sqlsanitize(" ")).toBe("");
9
12
  });
10
13
  it("should remove chars from invalid name", () => {
11
14
  expect(sqlsanitize("ffoo--oo--uu")).toBe("ffoooouu");
12
15
  });
16
+ it("should remove chars from invalid symbol", () => {
17
+ expect(sqlsanitize(Symbol("ffoo--oo--uu"))).toBe("ffoooouu");
18
+ });
13
19
  it("should not allow dots", () => {
14
20
  expect(sqlsanitize("ffoo.oo")).toBe("ffoooo");
15
21
  });
@@ -51,6 +57,32 @@ describe("mkWhere", () => {
51
57
  where: 'where "id"=$1 and "hello"=$2',
52
58
  });
53
59
  });
60
+ it("should read eq", () => {
61
+ expect(mkWhere({ eq: [Symbol("id"), 5] })).toStrictEqual({
62
+ values: [5],
63
+ where: 'where "id"=$1',
64
+ });
65
+ expect(mkWhere({ eq: [Symbol("id"), null] })).toStrictEqual({
66
+ values: [],
67
+ where: 'where "id" is null',
68
+ });
69
+ expect(mkWhere({ eq: ["id", null] })).toStrictEqual({
70
+ values: ["id"],
71
+ where: "where $1::text is null",
72
+ });
73
+ expect(mkWhere({ eq: [4, 5] })).toStrictEqual({
74
+ values: [4, 5],
75
+ where: "where $1=$2",
76
+ });
77
+ expect(mkWhere({ eq: [null, 5] })).toStrictEqual({
78
+ values: [5],
79
+ where: "where $1 is null",
80
+ });
81
+ expect(mkWhere({ not: { eq: [Symbol("id"), 5] } })).toStrictEqual({
82
+ values: [5],
83
+ where: 'where not ("id"=$1)',
84
+ });
85
+ });
54
86
  it("should query null", () => {
55
87
  expect(mkWhere({ id: null })).toStrictEqual({
56
88
  values: [],
@@ -140,8 +172,20 @@ describe("mkWhere", () => {
140
172
  values: [[1, 2, 3]],
141
173
  where: 'where "y" = ANY ($1)',
142
174
  });
175
+ expect(
176
+ mkWhere({
177
+ or: [
178
+ { not: { eq: ["1", null] } },
179
+ { not: { eq: [null, null] }, married_to: null },
180
+ ],
181
+ })
182
+ ).toStrictEqual({
183
+ values: ["1"],
184
+ where:
185
+ 'where (not ($1::text is null) or not (null is null) and "married_to" is null)',
186
+ });
143
187
  /*
144
- TODO
188
+
145
189
  expect(mkWhere([{ id: 5 }, { x: 7 }])).toStrictEqual({
146
190
  values: [5, 7],
147
191
  where: 'where "id"=$1 and "x"=$2',
package/package.json CHANGED
@@ -1,16 +1,18 @@
1
1
  {
2
2
  "name": "@saltcorn/db-common",
3
- "version": "0.6.1-beta.2",
3
+ "version": "0.6.2-beta.1",
4
4
  "description": "Db common structures for Saltcorn, open-source no-code platform",
5
5
  "homepage": "https://saltcorn.com",
6
6
  "scripts": {
7
- "test": "jest --runInBand"
7
+ "test": "jest --runInBand",
8
+ "tsc": "echo \"Error: no TypeScript support yet\"",
9
+ "clean": "echo \"Error: no TypeScript support yet\""
8
10
  },
9
11
  "author": "Tom Nielsen",
10
12
  "license": "MIT",
11
13
  "main": "index.js",
12
14
  "dependencies": {
13
- "@saltcorn/markup": "0.6.1-beta.2",
15
+ "@saltcorn/markup": "0.6.2-beta.1",
14
16
  "contractis": "^0.1.0"
15
17
  },
16
18
  "devDependencies": {