@saltcorn/db-common 0.6.2-beta.1 → 0.6.2-beta.5
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/dist/coverage/lcov-report/block-navigation.d.ts +2 -0
- package/dist/coverage/lcov-report/block-navigation.d.ts.map +1 -0
- package/dist/coverage/lcov-report/block-navigation.js +66 -0
- package/dist/coverage/lcov-report/block-navigation.js.map +1 -0
- package/dist/coverage/lcov-report/prettify.d.ts +1 -0
- package/dist/coverage/lcov-report/prettify.d.ts.map +1 -0
- package/dist/coverage/lcov-report/prettify.js +478 -0
- package/dist/coverage/lcov-report/prettify.js.map +1 -0
- package/dist/coverage/lcov-report/sorter.d.ts +2 -0
- package/dist/coverage/lcov-report/sorter.d.ts.map +1 -0
- package/dist/coverage/lcov-report/sorter.js +141 -0
- package/dist/coverage/lcov-report/sorter.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/{index.js → dist/index.js} +17 -16
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +86 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +250 -0
- package/dist/internal.js.map +1 -0
- package/dist/internal.test.d.ts +2 -0
- package/dist/internal.test.d.ts.map +1 -0
- package/dist/internal.test.js +248 -0
- package/dist/internal.test.js.map +1 -0
- package/dist/multi-tenant.d.ts +28 -0
- package/dist/multi-tenant.d.ts.map +1 -0
- package/dist/multi-tenant.js +50 -0
- package/dist/multi-tenant.js.map +1 -0
- package/dist/single-tenant.d.ts +28 -0
- package/dist/single-tenant.d.ts.map +1 -0
- package/dist/single-tenant.js +45 -0
- package/dist/single-tenant.js.map +1 -0
- package/dist/tenants.d.ts +9 -0
- package/dist/tenants.d.ts.map +1 -0
- package/dist/tenants.js +18 -0
- package/dist/tenants.js.map +1 -0
- package/dist/tsconfig.ref.tsbuildinfo +1 -0
- package/package.json +31 -7
- package/internal.js +0 -342
- package/internal.test.js +0 -198
- package/multi-tenant.js +0 -48
- package/single-tenant.js +0 -30
- package/tenants.js +0 -21
package/internal.js
DELETED
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @category db-common
|
|
3
|
-
* @module internal
|
|
4
|
-
*/
|
|
5
|
-
const { footer } = require("@saltcorn/markup/tags");
|
|
6
|
-
const { contract, is } = require("contractis");
|
|
7
|
-
|
|
8
|
-
//https://stackoverflow.com/questions/15300704/regex-with-my-jquery-function-for-sql-variable-name-validation
|
|
9
|
-
/**
|
|
10
|
-
* Transform value to correct sql name.
|
|
11
|
-
* Note! Dont use other symbols than ^A-Za-z_0-9
|
|
12
|
-
* @function
|
|
13
|
-
* @param {string} nm
|
|
14
|
-
* @returns {string}
|
|
15
|
-
*/
|
|
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
|
-
|
|
19
|
-
const s = nm.replace(/[^A-Za-z_0-9]*/g, "");
|
|
20
|
-
if (s[0] >= "0" && s[0] <= "9") return `_${s}`;
|
|
21
|
-
else return s;
|
|
22
|
-
});
|
|
23
|
-
/**
|
|
24
|
-
* Transform value to correct sql name.
|
|
25
|
-
* Instead of sqlsanitize also allows .
|
|
26
|
-
* For e.g. table name
|
|
27
|
-
* Note! Dont use other symbols than ^A-Za-z_0-9.
|
|
28
|
-
* @function
|
|
29
|
-
* @param {string} nm
|
|
30
|
-
* @returns {string}
|
|
31
|
-
*/
|
|
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
|
-
);
|
|
41
|
-
/**
|
|
42
|
-
*
|
|
43
|
-
* @param {object} v
|
|
44
|
-
* @param {string} i
|
|
45
|
-
* @param {boolean} is_sqlite
|
|
46
|
-
* @returns {string}
|
|
47
|
-
*/
|
|
48
|
-
const whereFTS = (v, i, is_sqlite) => {
|
|
49
|
-
const { fields, table } = v;
|
|
50
|
-
var flds = fields
|
|
51
|
-
.filter((f) => f.type && f.type.sql_name === "text")
|
|
52
|
-
.map(
|
|
53
|
-
(f) =>
|
|
54
|
-
"coalesce(" +
|
|
55
|
-
(table
|
|
56
|
-
? `"${sqlsanitize(table)}"."${sqlsanitize(f.name)}"`
|
|
57
|
-
: `"${sqlsanitize(f.name)}"`) +
|
|
58
|
-
",'')"
|
|
59
|
-
)
|
|
60
|
-
.join(" || ' ' || ");
|
|
61
|
-
if (flds === "") flds = "''";
|
|
62
|
-
if (is_sqlite) return `${flds} LIKE '%' || ? || '%'`;
|
|
63
|
-
else
|
|
64
|
-
return `to_tsvector('english', ${flds}) @@ plainto_tsquery('english', $${i})`;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @param {boolean} is_sqlite
|
|
69
|
-
* @param {string} i
|
|
70
|
-
* @returns {string}
|
|
71
|
-
*/
|
|
72
|
-
const placeHolder = (is_sqlite, i) => (is_sqlite ? `?` : `$${i}`);
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* @returns {number}
|
|
76
|
-
*/
|
|
77
|
-
const mkCounter = (init = 0) => {
|
|
78
|
-
let i = init;
|
|
79
|
-
return () => {
|
|
80
|
-
i += 1;
|
|
81
|
-
return i;
|
|
82
|
-
};
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
*
|
|
86
|
-
* @param {boolean} is_sqlite
|
|
87
|
-
* @param {string} i
|
|
88
|
-
* @returns {function}
|
|
89
|
-
*/
|
|
90
|
-
const subSelectWhere = (is_sqlite, i) => (k, v) => {
|
|
91
|
-
const whereObj = v.inSelect.where;
|
|
92
|
-
const wheres = whereObj ? Object.entries(whereObj) : [];
|
|
93
|
-
const where =
|
|
94
|
-
whereObj && wheres.length > 0
|
|
95
|
-
? "where " + wheres.map(whereClause(is_sqlite, i)).join(" and ")
|
|
96
|
-
: "";
|
|
97
|
-
return `${quote(sqlsanitizeAllowDots(k))} in (select ${
|
|
98
|
-
v.inSelect.field
|
|
99
|
-
} from ${v.inSelect.table} ${where})`;
|
|
100
|
-
};
|
|
101
|
-
/**
|
|
102
|
-
* @param {object} v
|
|
103
|
-
* @returns {object[]}
|
|
104
|
-
*/
|
|
105
|
-
const subSelectVals = (v) => {
|
|
106
|
-
const whereObj = v.inSelect.where;
|
|
107
|
-
const wheres = whereObj ? Object.entries(whereObj) : [];
|
|
108
|
-
const xs = wheres
|
|
109
|
-
.map(getVal)
|
|
110
|
-
.flat(1)
|
|
111
|
-
.filter((v) => v !== null);
|
|
112
|
-
return xs;
|
|
113
|
-
};
|
|
114
|
-
/**
|
|
115
|
-
* @param {string} s
|
|
116
|
-
* @returns {string}
|
|
117
|
-
*/
|
|
118
|
-
const wrapParens = (s) => (s ? `(${s})` : s);
|
|
119
|
-
/**
|
|
120
|
-
* @param {string} s
|
|
121
|
-
* @returns {string}
|
|
122
|
-
*/
|
|
123
|
-
const quote = (s) => (s.includes(".") || s.includes('"') ? s : `"${s}"`);
|
|
124
|
-
/**
|
|
125
|
-
* @param {boolean} is_sqlite
|
|
126
|
-
* @param {string} i
|
|
127
|
-
* @returns {function}
|
|
128
|
-
*/
|
|
129
|
-
const whereOr = (is_sqlite, i) => (ors) =>
|
|
130
|
-
wrapParens(
|
|
131
|
-
ors
|
|
132
|
-
.map((vi) =>
|
|
133
|
-
Object.entries(vi)
|
|
134
|
-
.map((kv) => whereClause(is_sqlite, i)(kv))
|
|
135
|
-
.join(" and ")
|
|
136
|
-
)
|
|
137
|
-
.join(" or ")
|
|
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 = [];
|
|
152
|
-
|
|
153
|
-
vs.forEach((v) => {
|
|
154
|
-
if (v !== null && typeof v !== "symbol") vals.push(v);
|
|
155
|
-
});
|
|
156
|
-
//console.log({ vals });
|
|
157
|
-
return vals;
|
|
158
|
-
};
|
|
159
|
-
/**
|
|
160
|
-
* @param {boolean} is_sqlite
|
|
161
|
-
* @param {string} i
|
|
162
|
-
* @returns {function}
|
|
163
|
-
*/
|
|
164
|
-
const whereClause = (is_sqlite, i) => ([k, v]) =>
|
|
165
|
-
k === "_fts"
|
|
166
|
-
? whereFTS(v, i(), is_sqlite)
|
|
167
|
-
: typeof (v || {}).in !== "undefined"
|
|
168
|
-
? `${quote(sqlsanitizeAllowDots(k))} = ${
|
|
169
|
-
is_sqlite ? "" : "ANY"
|
|
170
|
-
} (${placeHolder(is_sqlite, i())})`
|
|
171
|
-
: k === "or" && Array.isArray(v)
|
|
172
|
-
? whereOr(is_sqlite, i)(v)
|
|
173
|
-
: k === "not" && typeof v === "object"
|
|
174
|
-
? `not (${Object.entries(v)
|
|
175
|
-
.map((kv) => whereClause(is_sqlite, i)(kv))
|
|
176
|
-
.join(" and ")})`
|
|
177
|
-
: k === "eq" && Array.isArray(v)
|
|
178
|
-
? equals(v, is_sqlite, i)
|
|
179
|
-
: v && v.or && Array.isArray(v.or)
|
|
180
|
-
? wrapParens(
|
|
181
|
-
v.or.map((vi) => whereClause(is_sqlite, i)([k, vi])).join(" or ")
|
|
182
|
-
)
|
|
183
|
-
: Array.isArray(v)
|
|
184
|
-
? v.map((vi) => whereClause(is_sqlite, i)([k, vi])).join(" and ")
|
|
185
|
-
: typeof (v || {}).ilike !== "undefined"
|
|
186
|
-
? `${quote(sqlsanitizeAllowDots(k))} ${
|
|
187
|
-
is_sqlite ? "LIKE" : "ILIKE"
|
|
188
|
-
} '%' || ${placeHolder(is_sqlite, i())} || '%'`
|
|
189
|
-
: typeof (v || {}).gt !== "undefined"
|
|
190
|
-
? `${quote(sqlsanitizeAllowDots(k))}>${v.equal ? "=" : ""}${placeHolder(
|
|
191
|
-
is_sqlite,
|
|
192
|
-
i()
|
|
193
|
-
)}`
|
|
194
|
-
: typeof (v || {}).lt !== "undefined"
|
|
195
|
-
? `${quote(sqlsanitizeAllowDots(k))}<${v.equal ? "=" : ""}${placeHolder(
|
|
196
|
-
is_sqlite,
|
|
197
|
-
i()
|
|
198
|
-
)}`
|
|
199
|
-
: typeof (v || {}).inSelect !== "undefined"
|
|
200
|
-
? subSelectWhere(is_sqlite, i)(k, v)
|
|
201
|
-
: typeof (v || {}).json !== "undefined"
|
|
202
|
-
? is_sqlite
|
|
203
|
-
? `json_extract(${quote(
|
|
204
|
-
sqlsanitizeAllowDots(k)
|
|
205
|
-
)}, '$.${sqlsanitizeAllowDots(v.json[0])}')=${placeHolder(
|
|
206
|
-
is_sqlite,
|
|
207
|
-
i()
|
|
208
|
-
)}`
|
|
209
|
-
: `${quote(sqlsanitizeAllowDots(k))}->>'${sqlsanitizeAllowDots(
|
|
210
|
-
v.json[0]
|
|
211
|
-
)}'=${placeHolder(is_sqlite, i())}`
|
|
212
|
-
: v === null
|
|
213
|
-
? `${quote(sqlsanitizeAllowDots(k))} is null`
|
|
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
|
-
}`;
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* @param {object[]} opts
|
|
224
|
-
* @param {object} opts.k
|
|
225
|
-
* @param {object} opts.v
|
|
226
|
-
* @returns {boolean|object}
|
|
227
|
-
*/
|
|
228
|
-
const getVal = ([k, v]) =>
|
|
229
|
-
k === "_fts"
|
|
230
|
-
? v.searchTerm
|
|
231
|
-
: typeof (v || {}).in !== "undefined"
|
|
232
|
-
? [v.in]
|
|
233
|
-
: k === "not" && typeof v === "object"
|
|
234
|
-
? Object.entries(v).map(getVal).flat(1)
|
|
235
|
-
: k === "eq" && Array.isArray(v)
|
|
236
|
-
? equalsVals(v).flat(1)
|
|
237
|
-
: k === "or" && Array.isArray(v)
|
|
238
|
-
? v
|
|
239
|
-
.map((vi) => Object.entries(vi).map(getVal))
|
|
240
|
-
.flat(1)
|
|
241
|
-
.flat(1)
|
|
242
|
-
: v && v.or && Array.isArray(v.or)
|
|
243
|
-
? v.or.map((vi) => getVal([k, vi])).flat(1)
|
|
244
|
-
: Array.isArray(v)
|
|
245
|
-
? v.map((vi) => getVal([k, vi])).flat(1)
|
|
246
|
-
: typeof (v || {}).ilike !== "undefined"
|
|
247
|
-
? v.ilike
|
|
248
|
-
: typeof (v || {}).inSelect !== "undefined"
|
|
249
|
-
? subSelectVals(v)
|
|
250
|
-
: typeof (v || {}).lt !== "undefined"
|
|
251
|
-
? v.lt
|
|
252
|
-
: typeof (v || {}).gt !== "undefined"
|
|
253
|
-
? v.gt
|
|
254
|
-
: typeof (v || {}).sql !== "undefined"
|
|
255
|
-
? null
|
|
256
|
-
: typeof (v || {}).json !== "undefined"
|
|
257
|
-
? v.json[1]
|
|
258
|
-
: typeof v === "symbol"
|
|
259
|
-
? null
|
|
260
|
-
: v;
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* @param {object} whereObj
|
|
264
|
-
* @param {boolean} is_sqlite
|
|
265
|
-
* @param {number} initCount
|
|
266
|
-
* @returns {object}
|
|
267
|
-
*/
|
|
268
|
-
const mkWhere = (whereObj, is_sqlite, initCount = 0) => {
|
|
269
|
-
const wheres = whereObj ? Object.entries(whereObj) : [];
|
|
270
|
-
//console.log({ wheres });
|
|
271
|
-
const where =
|
|
272
|
-
whereObj && wheres.length > 0
|
|
273
|
-
? "where " +
|
|
274
|
-
wheres.map(whereClause(is_sqlite, mkCounter(initCount))).join(" and ")
|
|
275
|
-
: "";
|
|
276
|
-
const values = wheres
|
|
277
|
-
.map(getVal)
|
|
278
|
-
.flat(1)
|
|
279
|
-
.filter((v) => v !== null);
|
|
280
|
-
return { where, values };
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* @param {number|string} x
|
|
285
|
-
* @returns {number|null}
|
|
286
|
-
*/
|
|
287
|
-
const toInt = (x) =>
|
|
288
|
-
typeof x === "number"
|
|
289
|
-
? Math.round(x)
|
|
290
|
-
: typeof x === "string"
|
|
291
|
-
? parseInt(x)
|
|
292
|
-
: null;
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* @param {object} opts
|
|
296
|
-
* @param {string} opts.latField
|
|
297
|
-
* @param {string} opts.longField
|
|
298
|
-
* @param {number} opts.lat
|
|
299
|
-
* @param {number} opts.long
|
|
300
|
-
* @returns {string}
|
|
301
|
-
*/
|
|
302
|
-
const getDistanceOrder = ({ latField, longField, lat, long }) => {
|
|
303
|
-
const cos_lat_2 = Math.pow(Math.cos((+lat * Math.PI) / 180), 2);
|
|
304
|
-
return `((${sqlsanitizeAllowDots(
|
|
305
|
-
latField
|
|
306
|
-
)} - ${+lat})*(${sqlsanitizeAllowDots(
|
|
307
|
-
latField
|
|
308
|
-
)} - ${+lat})) + ((${sqlsanitizeAllowDots(
|
|
309
|
-
longField
|
|
310
|
-
)} - ${+long})*(${sqlsanitizeAllowDots(longField)} - ${+long})*${cos_lat_2})`;
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* @param {object} selopts
|
|
315
|
-
* @returns {string[]}
|
|
316
|
-
*/
|
|
317
|
-
const mkSelectOptions = (selopts) => {
|
|
318
|
-
const orderby =
|
|
319
|
-
selopts.orderBy === "RANDOM()"
|
|
320
|
-
? "order by RANDOM()"
|
|
321
|
-
: selopts.orderBy && selopts.orderBy.distance
|
|
322
|
-
? `order by ${getDistanceOrder(selopts.orderBy.distance)}`
|
|
323
|
-
: selopts.orderBy && selopts.nocase
|
|
324
|
-
? `order by lower(${sqlsanitizeAllowDots(selopts.orderBy)})${
|
|
325
|
-
selopts.orderDesc ? " DESC" : ""
|
|
326
|
-
}`
|
|
327
|
-
: selopts.orderBy
|
|
328
|
-
? `order by ${sqlsanitizeAllowDots(selopts.orderBy)}${
|
|
329
|
-
selopts.orderDesc ? " DESC" : ""
|
|
330
|
-
}`
|
|
331
|
-
: "";
|
|
332
|
-
const limit = selopts.limit ? `limit ${toInt(selopts.limit)}` : "";
|
|
333
|
-
const offset = selopts.offset ? `offset ${toInt(selopts.offset)}` : "";
|
|
334
|
-
return [orderby, limit, offset].filter((s) => s).join(" ");
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
module.exports = {
|
|
338
|
-
sqlsanitize,
|
|
339
|
-
mkWhere,
|
|
340
|
-
mkSelectOptions,
|
|
341
|
-
sqlsanitizeAllowDots,
|
|
342
|
-
};
|
package/internal.test.js
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
const { sqlsanitize, mkWhere, sqlsanitizeAllowDots } = require("./internal");
|
|
2
|
-
|
|
3
|
-
describe("sqlsanitize", () => {
|
|
4
|
-
it("should not alter valid name", () => {
|
|
5
|
-
expect(sqlsanitize("ffoo_oo")).toBe("ffoo_oo");
|
|
6
|
-
});
|
|
7
|
-
it("should not alter valid symbol", () => {
|
|
8
|
-
expect(sqlsanitize(Symbol("ffoo_oo"))).toBe("ffoo_oo");
|
|
9
|
-
});
|
|
10
|
-
it("should remove spaces", () => {
|
|
11
|
-
expect(sqlsanitize(" ")).toBe("");
|
|
12
|
-
});
|
|
13
|
-
it("should remove chars from invalid name", () => {
|
|
14
|
-
expect(sqlsanitize("ffoo--oo--uu")).toBe("ffoooouu");
|
|
15
|
-
});
|
|
16
|
-
it("should remove chars from invalid symbol", () => {
|
|
17
|
-
expect(sqlsanitize(Symbol("ffoo--oo--uu"))).toBe("ffoooouu");
|
|
18
|
-
});
|
|
19
|
-
it("should not allow dots", () => {
|
|
20
|
-
expect(sqlsanitize("ffoo.oo")).toBe("ffoooo");
|
|
21
|
-
});
|
|
22
|
-
it("should allow dots when specified", () => {
|
|
23
|
-
expect(sqlsanitizeAllowDots("ffoo.oo")).toBe("ffoo.oo");
|
|
24
|
-
});
|
|
25
|
-
it("should allow quotes when dots specified", () => {
|
|
26
|
-
expect(sqlsanitizeAllowDots('ffoo."oo"')).toBe('ffoo."oo"');
|
|
27
|
-
});
|
|
28
|
-
it("should allow numbers", () => {
|
|
29
|
-
expect(sqlsanitize("ff1oo_oo")).toBe("ff1oo_oo");
|
|
30
|
-
});
|
|
31
|
-
it("should not allow numbers in initial position", () => {
|
|
32
|
-
expect(sqlsanitize("1ffoo_o1o")).toBe("_1ffoo_o1o");
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe("mkWhere", () => {
|
|
37
|
-
it("should empty on no arg", () => {
|
|
38
|
-
expect(mkWhere()).toStrictEqual({ values: [], where: "" });
|
|
39
|
-
});
|
|
40
|
-
it("should empty on null obj arg", () => {
|
|
41
|
-
expect(mkWhere({})).toStrictEqual({ values: [], where: "" });
|
|
42
|
-
});
|
|
43
|
-
it("should query json", () => {
|
|
44
|
-
expect(mkWhere({ foo: { json: ["bar", 5] } })).toStrictEqual({
|
|
45
|
-
values: [5],
|
|
46
|
-
where: `where "foo"->>'bar'=$1`,
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("should set id", () => {
|
|
51
|
-
expect(mkWhere({ id: 5 })).toStrictEqual({
|
|
52
|
-
values: [5],
|
|
53
|
-
where: 'where "id"=$1',
|
|
54
|
-
});
|
|
55
|
-
expect(mkWhere({ id: 5, hello: "world" })).toStrictEqual({
|
|
56
|
-
values: [5, "world"],
|
|
57
|
-
where: 'where "id"=$1 and "hello"=$2',
|
|
58
|
-
});
|
|
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
|
-
});
|
|
86
|
-
it("should query null", () => {
|
|
87
|
-
expect(mkWhere({ id: null })).toStrictEqual({
|
|
88
|
-
values: [],
|
|
89
|
-
where: 'where "id" is null',
|
|
90
|
-
});
|
|
91
|
-
expect(mkWhere({ id: null, foo: 1 })).toStrictEqual({
|
|
92
|
-
values: [1],
|
|
93
|
-
where: 'where "id" is null and "foo"=$1',
|
|
94
|
-
});
|
|
95
|
-
expect(mkWhere({ foo: 1, id: null })).toStrictEqual({
|
|
96
|
-
values: [1],
|
|
97
|
-
where: 'where "foo"=$1 and "id" is null',
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
it("should query lt/gt", () => {
|
|
101
|
-
expect(mkWhere({ id: { lt: 5 } })).toStrictEqual({
|
|
102
|
-
values: [5],
|
|
103
|
-
where: 'where "id"<$1',
|
|
104
|
-
});
|
|
105
|
-
expect(mkWhere({ id: { gt: 8 } })).toStrictEqual({
|
|
106
|
-
values: [8],
|
|
107
|
-
where: 'where "id">$1',
|
|
108
|
-
});
|
|
109
|
-
expect(mkWhere({ id: { lt: 5, equal: true } })).toStrictEqual({
|
|
110
|
-
values: [5],
|
|
111
|
-
where: 'where "id"<=$1',
|
|
112
|
-
});
|
|
113
|
-
expect(mkWhere({ id: { gt: 8, equal: true } })).toStrictEqual({
|
|
114
|
-
values: [8],
|
|
115
|
-
where: 'where "id">=$1',
|
|
116
|
-
});
|
|
117
|
-
expect(mkWhere({ id: [{ gt: 0 }, { lt: 10 }] })).toStrictEqual({
|
|
118
|
-
values: [0, 10],
|
|
119
|
-
where: 'where "id">$1 and "id"<$2',
|
|
120
|
-
});
|
|
121
|
-
expect(mkWhere({ id: { or: [{ gt: 10 }, { lt: 5 }] } })).toStrictEqual({
|
|
122
|
-
values: [10, 5],
|
|
123
|
-
where: 'where ("id">$1 or "id"<$2)',
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
it("should query subselect", () => {
|
|
127
|
-
expect(
|
|
128
|
-
mkWhere({
|
|
129
|
-
id: [{ inSelect: { table: "foo", field: "bar", where: { baz: 7 } } }],
|
|
130
|
-
})
|
|
131
|
-
).toStrictEqual({
|
|
132
|
-
values: [7],
|
|
133
|
-
where: 'where "id" in (select bar from foo where "baz"=$1)',
|
|
134
|
-
});
|
|
135
|
-
expect(
|
|
136
|
-
mkWhere({
|
|
137
|
-
age: 45,
|
|
138
|
-
id: [{ inSelect: { table: "foo", field: "bar", where: { baz: 7 } } }],
|
|
139
|
-
name: "Alice",
|
|
140
|
-
})
|
|
141
|
-
).toStrictEqual({
|
|
142
|
-
values: [45, 7, "Alice"],
|
|
143
|
-
where: `where "age"=$1 and "id" in (select bar from foo where "baz"=$2) and "name"=$3`,
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
it("should query or", () => {
|
|
147
|
-
expect(mkWhere({ or: [{ id: 5 }, { x: 7 }] })).toStrictEqual({
|
|
148
|
-
values: [5, 7],
|
|
149
|
-
where: 'where ("id"=$1 or "x"=$2)',
|
|
150
|
-
});
|
|
151
|
-
expect(mkWhere({ or: [{ id: 5 }, { x: { gt: 7 } }] })).toStrictEqual({
|
|
152
|
-
values: [5, 7],
|
|
153
|
-
where: 'where ("id"=$1 or "x">$2)',
|
|
154
|
-
});
|
|
155
|
-
expect(mkWhere({ or: [{ id: 5 }, { x: 7, y: 8 }] })).toStrictEqual({
|
|
156
|
-
values: [5, 7, 8],
|
|
157
|
-
where: 'where ("id"=$1 or "x"=$2 and "y"=$3)',
|
|
158
|
-
});
|
|
159
|
-
expect(mkWhere({ not: { id: 5 } })).toStrictEqual({
|
|
160
|
-
values: [5],
|
|
161
|
-
where: 'where not ("id"=$1)',
|
|
162
|
-
});
|
|
163
|
-
expect(mkWhere({ not: { id: 5, y: 1 } })).toStrictEqual({
|
|
164
|
-
values: [5, 1],
|
|
165
|
-
where: 'where not ("id"=$1 and "y"=$2)',
|
|
166
|
-
});
|
|
167
|
-
expect(mkWhere({ not: { y: { in: [1, 2, 3] } } })).toStrictEqual({
|
|
168
|
-
values: [[1, 2, 3]],
|
|
169
|
-
where: 'where not ("y" = ANY ($1))',
|
|
170
|
-
});
|
|
171
|
-
expect(mkWhere({ y: { in: [1, 2, 3] } })).toStrictEqual({
|
|
172
|
-
values: [[1, 2, 3]],
|
|
173
|
-
where: 'where "y" = ANY ($1)',
|
|
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
|
-
});
|
|
187
|
-
/*
|
|
188
|
-
|
|
189
|
-
expect(mkWhere([{ id: 5 }, { x: 7 }])).toStrictEqual({
|
|
190
|
-
values: [5, 7],
|
|
191
|
-
where: 'where "id"=$1 and "x"=$2',
|
|
192
|
-
});
|
|
193
|
-
expect(mkWhere([{ or: [{ id: 5 }, { x: 7 }] }, { z: 9 }])).toStrictEqual({
|
|
194
|
-
values: [5, 7, 9],
|
|
195
|
-
where: 'where ("id"=$1 or "x"=$2) and "z"=$3',
|
|
196
|
-
});*/
|
|
197
|
-
});
|
|
198
|
-
});
|
package/multi-tenant.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @category db-common
|
|
3
|
-
* @module multi-tenant
|
|
4
|
-
*/
|
|
5
|
-
const { AsyncLocalStorage } = require("async_hooks");
|
|
6
|
-
const { sqlsanitize } = require("./internal");
|
|
7
|
-
|
|
8
|
-
var is_multi_tenant = true;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @returns {boolean}
|
|
12
|
-
*/
|
|
13
|
-
const is_it_multi_tenant = () => is_multi_tenant;
|
|
14
|
-
|
|
15
|
-
const tenantNamespace = new AsyncLocalStorage();
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @returns {object}
|
|
19
|
-
*/
|
|
20
|
-
const enable_multi_tenant = () => {};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @param {object} tenant
|
|
24
|
-
* @param {function} f
|
|
25
|
-
* @returns {object}
|
|
26
|
-
*/
|
|
27
|
-
const runWithTenant = (tenant, f) => {
|
|
28
|
-
if (!is_multi_tenant) return f();
|
|
29
|
-
else return tenantNamespace.run(sqlsanitize(tenant).toLowerCase(), f);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
module.exports = /**
|
|
33
|
-
* @function
|
|
34
|
-
* @name "module.exports function"
|
|
35
|
-
* @returns {exports}
|
|
36
|
-
*/
|
|
37
|
-
(connObj) => ({
|
|
38
|
-
/**
|
|
39
|
-
* @returns {object}
|
|
40
|
-
*/
|
|
41
|
-
getTenantSchema() {
|
|
42
|
-
const storeVal = tenantNamespace.getStore();
|
|
43
|
-
return storeVal || connObj.default_schema;
|
|
44
|
-
},
|
|
45
|
-
enable_multi_tenant,
|
|
46
|
-
runWithTenant,
|
|
47
|
-
is_it_multi_tenant,
|
|
48
|
-
});
|
package/single-tenant.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @category db-common
|
|
3
|
-
* @module single-tenant
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
module.exports = /**
|
|
7
|
-
* @function
|
|
8
|
-
* @name "module.exports function"
|
|
9
|
-
* @returns {exports}
|
|
10
|
-
*/
|
|
11
|
-
(connObj) => ({
|
|
12
|
-
/**
|
|
13
|
-
* @returns {string}
|
|
14
|
-
*/
|
|
15
|
-
getTenantSchema: () => connObj.default_schema,
|
|
16
|
-
/**
|
|
17
|
-
* @returns {false}
|
|
18
|
-
*/
|
|
19
|
-
is_it_multi_tenant: () => false,
|
|
20
|
-
enable_multi_tenant() {},
|
|
21
|
-
/**
|
|
22
|
-
* @param {*} t
|
|
23
|
-
* @param {function} f
|
|
24
|
-
* @returns {*}
|
|
25
|
-
*/
|
|
26
|
-
runWithTenant(t, f) {
|
|
27
|
-
return f();
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
|
package/tenants.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @category db-common
|
|
3
|
-
* @module tenants
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
let tenantExport = null;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
module.exports = /**
|
|
10
|
-
* @function
|
|
11
|
-
* @name "module.exports function"
|
|
12
|
-
* @returns {multi-tenant|single-tenant}
|
|
13
|
-
*/
|
|
14
|
-
(connObj) => {
|
|
15
|
-
if(!tenantExport) {
|
|
16
|
-
tenantExport = connObj.multi_tenant ?
|
|
17
|
-
require("./multi-tenant")(connObj)
|
|
18
|
-
: require("./single-tenant")(connObj);
|
|
19
|
-
}
|
|
20
|
-
return tenantExport;
|
|
21
|
-
}
|