@saltcorn/db-common 0.6.2-beta.0 → 0.6.2-beta.4
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/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 +36 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +292 -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 +190 -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 +19 -4
- 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
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @category db-common
|
|
4
|
+
* @module multi-tenant
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.getTenantSchema = exports.runWithTenant = exports.enable_multi_tenant = exports.is_it_multi_tenant = exports.init = exports.tenantNamespace = void 0;
|
|
8
|
+
const internal_1 = require("./internal");
|
|
9
|
+
const async_hooks_1 = require("async_hooks");
|
|
10
|
+
const is_multi_tenant = true;
|
|
11
|
+
let connObj = null;
|
|
12
|
+
exports.tenantNamespace = new async_hooks_1.AsyncLocalStorage();
|
|
13
|
+
/**
|
|
14
|
+
* set the connection object of the the multi-tenant
|
|
15
|
+
* @param connObjPara
|
|
16
|
+
*/
|
|
17
|
+
const init = (connObjPara) => {
|
|
18
|
+
connObj = connObjPara;
|
|
19
|
+
};
|
|
20
|
+
exports.init = init;
|
|
21
|
+
/**
|
|
22
|
+
* @returns {boolean}
|
|
23
|
+
*/
|
|
24
|
+
const is_it_multi_tenant = () => is_multi_tenant;
|
|
25
|
+
exports.is_it_multi_tenant = is_it_multi_tenant;
|
|
26
|
+
/**
|
|
27
|
+
* @returns {void}
|
|
28
|
+
*/
|
|
29
|
+
const enable_multi_tenant = () => { };
|
|
30
|
+
exports.enable_multi_tenant = enable_multi_tenant;
|
|
31
|
+
/**
|
|
32
|
+
* @param {object} tenant
|
|
33
|
+
* @param {function} f
|
|
34
|
+
* @returns {object}
|
|
35
|
+
*/
|
|
36
|
+
const runWithTenant = (tenant, f) => {
|
|
37
|
+
if (!is_multi_tenant)
|
|
38
|
+
return f();
|
|
39
|
+
else
|
|
40
|
+
return exports.tenantNamespace.run((0, internal_1.sqlsanitize)(tenant).toLowerCase(), f);
|
|
41
|
+
};
|
|
42
|
+
exports.runWithTenant = runWithTenant;
|
|
43
|
+
const getTenantSchema = () => {
|
|
44
|
+
if (!connObj)
|
|
45
|
+
throw new Error("The connection object is not initalized");
|
|
46
|
+
const storeVal = exports.tenantNamespace.getStore();
|
|
47
|
+
return storeVal || connObj.default_schema;
|
|
48
|
+
};
|
|
49
|
+
exports.getTenantSchema = getTenantSchema;
|
|
50
|
+
//# sourceMappingURL=multi-tenant.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multi-tenant.js","sourceRoot":"","sources":["../multi-tenant.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,yCAAyC;AACzC,6CAAgD;AAEhD,MAAM,eAAe,GAAG,IAAI,CAAC;AAC7B,IAAI,OAAO,GAAQ,IAAI,CAAC;AAEX,QAAA,eAAe,GAAG,IAAI,+BAAiB,EAAE,CAAC;AAEvD;;;GAGG;AACI,MAAM,IAAI,GAAG,CAAC,WAAgB,EAAQ,EAAE;IAC7C,OAAO,GAAG,WAAW,CAAC;AACxB,CAAC,CAAC;AAFW,QAAA,IAAI,QAEf;AAEF;;GAEG;AACI,MAAM,kBAAkB,GAAG,GAAY,EAAE,CAAC,eAAe,CAAC;AAApD,QAAA,kBAAkB,sBAAkC;AAEjE;;GAEG;AACI,MAAM,mBAAmB,GAAG,GAAS,EAAE,GAAE,CAAC,CAAC;AAArC,QAAA,mBAAmB,uBAAkB;AAElD;;;;GAIG;AACI,MAAM,aAAa,GAAG,CAAC,MAAW,EAAE,CAAY,EAAO,EAAE;IAC9D,IAAI,CAAC,eAAe;QAAE,OAAO,CAAC,EAAE,CAAC;;QAC5B,OAAO,uBAAe,CAAC,GAAG,CAAC,IAAA,sBAAW,EAAC,MAAM,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,CAAC;AACxE,CAAC,CAAC;AAHW,QAAA,aAAa,iBAGxB;AAEK,MAAM,eAAe,GAAG,GAAW,EAAE;IAC1C,IAAI,CAAC,OAAO;QAAE,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IACzE,MAAM,QAAQ,GAAG,uBAAe,CAAC,QAAQ,EAAE,CAAC;IAC5C,OAAO,QAAQ,IAAI,OAAO,CAAC,cAAc,CAAC;AAC5C,CAAC,CAAC;AAJW,QAAA,eAAe,mBAI1B"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @category db-common
|
|
3
|
+
* @module single-tenant
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* set the connection object of the the single-tenant
|
|
7
|
+
* @param connObjPara
|
|
8
|
+
*/
|
|
9
|
+
export declare const init: (connObjPara: any) => void;
|
|
10
|
+
/**
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
export declare const getTenantSchema: () => string;
|
|
14
|
+
/**
|
|
15
|
+
* @returns {false}
|
|
16
|
+
*/
|
|
17
|
+
export declare const is_it_multi_tenant: () => boolean;
|
|
18
|
+
/**
|
|
19
|
+
* @returns {void}
|
|
20
|
+
*/
|
|
21
|
+
export declare const enable_multi_tenant: () => void;
|
|
22
|
+
/**
|
|
23
|
+
* @param {*} t
|
|
24
|
+
* @param {function} f
|
|
25
|
+
* @returns {*}
|
|
26
|
+
*/
|
|
27
|
+
export declare const runWithTenant: (t: any, f: () => any) => any;
|
|
28
|
+
//# sourceMappingURL=single-tenant.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"single-tenant.d.ts","sourceRoot":"","sources":["../single-tenant.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH;;;GAGG;AACH,eAAO,MAAM,IAAI,gBAAiB,GAAG,KAAG,IAEvC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,QAAO,MAGlC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,QAAO,OAAgB,CAAC;AAEvD;;GAEG;AACH,eAAO,MAAM,mBAAmB,QAAO,IAAU,CAAC;AAElD;;;;GAIG;AACH,eAAO,MAAM,aAAa,MAAO,GAAG,KAAK,MAAM,GAAG,KAAG,GAEpD,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @category db-common
|
|
4
|
+
* @module single-tenant
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.runWithTenant = exports.enable_multi_tenant = exports.is_it_multi_tenant = exports.getTenantSchema = exports.init = void 0;
|
|
8
|
+
let connObj = null;
|
|
9
|
+
/**
|
|
10
|
+
* set the connection object of the the single-tenant
|
|
11
|
+
* @param connObjPara
|
|
12
|
+
*/
|
|
13
|
+
const init = (connObjPara) => {
|
|
14
|
+
connObj = connObjPara;
|
|
15
|
+
};
|
|
16
|
+
exports.init = init;
|
|
17
|
+
/**
|
|
18
|
+
* @returns {string}
|
|
19
|
+
*/
|
|
20
|
+
const getTenantSchema = () => {
|
|
21
|
+
if (!connObj)
|
|
22
|
+
throw new Error("The connection object is not initalized");
|
|
23
|
+
return connObj.default_schema;
|
|
24
|
+
};
|
|
25
|
+
exports.getTenantSchema = getTenantSchema;
|
|
26
|
+
/**
|
|
27
|
+
* @returns {false}
|
|
28
|
+
*/
|
|
29
|
+
const is_it_multi_tenant = () => false;
|
|
30
|
+
exports.is_it_multi_tenant = is_it_multi_tenant;
|
|
31
|
+
/**
|
|
32
|
+
* @returns {void}
|
|
33
|
+
*/
|
|
34
|
+
const enable_multi_tenant = () => { };
|
|
35
|
+
exports.enable_multi_tenant = enable_multi_tenant;
|
|
36
|
+
/**
|
|
37
|
+
* @param {*} t
|
|
38
|
+
* @param {function} f
|
|
39
|
+
* @returns {*}
|
|
40
|
+
*/
|
|
41
|
+
const runWithTenant = (t, f) => {
|
|
42
|
+
return f();
|
|
43
|
+
};
|
|
44
|
+
exports.runWithTenant = runWithTenant;
|
|
45
|
+
//# sourceMappingURL=single-tenant.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"single-tenant.js","sourceRoot":"","sources":["../single-tenant.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,IAAI,OAAO,GAAQ,IAAI,CAAC;AAExB;;;GAGG;AACI,MAAM,IAAI,GAAG,CAAC,WAAgB,EAAQ,EAAE;IAC7C,OAAO,GAAG,WAAW,CAAC;AACxB,CAAC,CAAC;AAFW,QAAA,IAAI,QAEf;AAEF;;GAEG;AACI,MAAM,eAAe,GAAG,GAAW,EAAE;IAC1C,IAAI,CAAC,OAAO;QAAE,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IACzE,OAAO,OAAO,CAAC,cAAc,CAAC;AAChC,CAAC,CAAC;AAHW,QAAA,eAAe,mBAG1B;AAEF;;GAEG;AACI,MAAM,kBAAkB,GAAG,GAAY,EAAE,CAAC,KAAK,CAAC;AAA1C,QAAA,kBAAkB,sBAAwB;AAEvD;;GAEG;AACI,MAAM,mBAAmB,GAAG,GAAS,EAAE,GAAE,CAAC,CAAC;AAArC,QAAA,mBAAmB,uBAAkB;AAElD;;;;GAIG;AACI,MAAM,aAAa,GAAG,CAAC,CAAM,EAAE,CAAY,EAAO,EAAE;IACzD,OAAO,CAAC,EAAE,CAAC;AACb,CAAC,CAAC;AAFW,QAAA,aAAa,iBAExB"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @category db-common
|
|
3
|
+
* @module tenants
|
|
4
|
+
*/
|
|
5
|
+
import * as multiTenant from "./multi-tenant";
|
|
6
|
+
import * as singleTenant from "./single-tenant";
|
|
7
|
+
declare const _default: (connObj: any) => typeof multiTenant | typeof singleTenant;
|
|
8
|
+
export = _default;
|
|
9
|
+
//# sourceMappingURL=tenants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenants.d.ts","sourceRoot":"","sources":["../tenants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,WAAW,MAAM,gBAAgB,CAAC;AAC9C,OAAO,KAAK,YAAY,MAAM,iBAAiB,CAAC;kCAG7B,GAAG;AAAtB,kBASE"}
|
package/dist/tenants.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @category db-common
|
|
4
|
+
* @module tenants
|
|
5
|
+
*/
|
|
6
|
+
let tenantExport = null;
|
|
7
|
+
module.exports = (connObj) => {
|
|
8
|
+
if (!tenantExport) {
|
|
9
|
+
tenantExport = connObj.multi_tenant
|
|
10
|
+
? require("./multi-tenant")
|
|
11
|
+
: require("./single-tenant");
|
|
12
|
+
}
|
|
13
|
+
if (!tenantExport)
|
|
14
|
+
throw new Error("unable to initalize a tenant");
|
|
15
|
+
tenantExport.init(connObj);
|
|
16
|
+
return tenantExport;
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=tenants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenants.js","sourceRoot":"","sources":["../tenants.ts"],"names":[],"mappings":";AAAA;;;GAGG;AAKH,IAAI,YAAY,GAAoD,IAAI,CAAC;AACzE,iBAAS,CAAC,OAAY,EAAE,EAAE;IACxB,IAAI,CAAC,YAAY,EAAE;QACjB,YAAY,GAAG,OAAO,CAAC,YAAY;YACjC,CAAC,CAAC,OAAO,CAAC,gBAAgB,CAAC;YAC3B,CAAC,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;KAChC;IACD,IAAI,CAAC,YAAY;QAAE,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;IACnE,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3B,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"program":{"fileNames":["../../../node_modules/typescript/lib/lib.es5.d.ts","../../../node_modules/typescript/lib/lib.es2015.d.ts","../../../node_modules/typescript/lib/lib.es2016.d.ts","../../../node_modules/typescript/lib/lib.es2017.d.ts","../../../node_modules/typescript/lib/lib.es2018.d.ts","../../../node_modules/typescript/lib/lib.es2019.d.ts","../../../node_modules/typescript/lib/lib.es2020.d.ts","../../../node_modules/typescript/lib/lib.es2015.core.d.ts","../../../node_modules/typescript/lib/lib.es2015.collection.d.ts","../../../node_modules/typescript/lib/lib.es2015.generator.d.ts","../../../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../../node_modules/typescript/lib/lib.es2015.promise.d.ts","../../../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../../node_modules/typescript/lib/lib.es2017.object.d.ts","../../../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../../node_modules/typescript/lib/lib.es2017.string.d.ts","../../../node_modules/typescript/lib/lib.es2017.intl.d.ts","../../../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../../node_modules/typescript/lib/lib.es2018.intl.d.ts","../../../node_modules/typescript/lib/lib.es2018.promise.d.ts","../../../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../../node_modules/typescript/lib/lib.es2019.array.d.ts","../../../node_modules/typescript/lib/lib.es2019.object.d.ts","../../../node_modules/typescript/lib/lib.es2019.string.d.ts","../../../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../../node_modules/typescript/lib/lib.es2020.promise.d.ts","../../../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../../node_modules/typescript/lib/lib.es2020.string.d.ts","../../../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../../node_modules/typescript/lib/lib.es2020.intl.d.ts","../../../node_modules/typescript/lib/lib.esnext.intl.d.ts","../index.js","../internal.ts","../internal.test.js","../multi-tenant.ts","../single-tenant.ts","../tenants.ts","../node_modules/@babel/types/lib/index.d.ts","../node_modules/@types/babel__generator/index.d.ts","../node_modules/@babel/parser/typings/babel-parser.d.ts","../node_modules/@types/babel__template/index.d.ts","../node_modules/@types/babel__traverse/index.d.ts","../node_modules/@types/babel__core/index.d.ts","../node_modules/@types/node/assert.d.ts","../node_modules/@types/node/assert/strict.d.ts","../node_modules/@types/node/globals.d.ts","../node_modules/@types/node/async_hooks.d.ts","../node_modules/@types/node/buffer.d.ts","../node_modules/@types/node/child_process.d.ts","../node_modules/@types/node/cluster.d.ts","../node_modules/@types/node/console.d.ts","../node_modules/@types/node/constants.d.ts","../node_modules/@types/node/crypto.d.ts","../node_modules/@types/node/dgram.d.ts","../node_modules/@types/node/diagnostics_channel.d.ts","../node_modules/@types/node/dns.d.ts","../node_modules/@types/node/dns/promises.d.ts","../node_modules/@types/node/domain.d.ts","../node_modules/@types/node/events.d.ts","../node_modules/@types/node/fs.d.ts","../node_modules/@types/node/fs/promises.d.ts","../node_modules/@types/node/http.d.ts","../node_modules/@types/node/http2.d.ts","../node_modules/@types/node/https.d.ts","../node_modules/@types/node/inspector.d.ts","../node_modules/@types/node/module.d.ts","../node_modules/@types/node/net.d.ts","../node_modules/@types/node/os.d.ts","../node_modules/@types/node/path.d.ts","../node_modules/@types/node/perf_hooks.d.ts","../node_modules/@types/node/process.d.ts","../node_modules/@types/node/punycode.d.ts","../node_modules/@types/node/querystring.d.ts","../node_modules/@types/node/readline.d.ts","../node_modules/@types/node/repl.d.ts","../node_modules/@types/node/stream.d.ts","../node_modules/@types/node/stream/promises.d.ts","../node_modules/@types/node/stream/consumers.d.ts","../node_modules/@types/node/stream/web.d.ts","../node_modules/@types/node/string_decoder.d.ts","../node_modules/@types/node/timers.d.ts","../node_modules/@types/node/timers/promises.d.ts","../node_modules/@types/node/tls.d.ts","../node_modules/@types/node/trace_events.d.ts","../node_modules/@types/node/tty.d.ts","../node_modules/@types/node/url.d.ts","../node_modules/@types/node/util.d.ts","../node_modules/@types/node/v8.d.ts","../node_modules/@types/node/vm.d.ts","../node_modules/@types/node/wasi.d.ts","../node_modules/@types/node/worker_threads.d.ts","../node_modules/@types/node/zlib.d.ts","../node_modules/@types/node/globals.global.d.ts","../node_modules/@types/node/index.d.ts","../node_modules/@types/graceful-fs/index.d.ts","../node_modules/@types/istanbul-lib-coverage/index.d.ts","../node_modules/@types/istanbul-lib-report/index.d.ts","../node_modules/@types/istanbul-reports/index.d.ts","../node_modules/@types/normalize-package-data/index.d.ts","../node_modules/@types/prettier/index.d.ts","../node_modules/@types/stack-utils/index.d.ts","../node_modules/@types/yargs-parser/index.d.ts","../node_modules/@types/yargs/index.d.ts","../../../node_modules/@types/babel-types/index.d.ts","../../../node_modules/@types/babylon/index.d.ts","../../../node_modules/@types/minimatch/index.d.ts","../../../node_modules/@types/glob/index.d.ts","../../../node_modules/@types/minimist/index.d.ts"],"fileInfos":[{"version":"6adbf5efd0e374ff5f427a4f26a5a413e9734eee5067a0e86da69aea41910b52","affectsGlobalScope":true},"dc47c4fa66b9b9890cf076304de2a9c5201e94b740cffdf09f87296d877d71f6","7a387c58583dfca701b6c85e0adaf43fb17d590fb16d5b2dc0a2fbd89f35c467","8a12173c586e95f4433e0c6dc446bc88346be73ffe9ca6eec7aa63c8f3dca7f9","5f4e733ced4e129482ae2186aae29fde948ab7182844c3a5a51dd346182c7b06","e6b724280c694a9f588847f754198fb96c43d805f065c3a5b28bbc9594541c84","e21c071ca3e1b4a815d5f04a7475adcaeea5d64367e840dd0154096d705c3940",{"version":"d8996609230d17e90484a2dd58f22668f9a05a3bfe00bfb1d6271171e54a31fb","affectsGlobalScope":true},{"version":"43fb1d932e4966a39a41b464a12a81899d9ae5f2c829063f5571b6b87e6d2f9c","affectsGlobalScope":true},{"version":"cdccba9a388c2ee3fd6ad4018c640a471a6c060e96f1232062223063b0a5ac6a","affectsGlobalScope":true},{"version":"4378fc8122ec9d1a685b01eb66c46f62aba6b239ca7228bb6483bcf8259ee493","affectsGlobalScope":true},{"version":"0d5f52b3174bee6edb81260ebcd792692c32c81fd55499d69531496f3f2b25e7","affectsGlobalScope":true},{"version":"810627a82ac06fb5166da5ada4159c4ec11978dfbb0805fe804c86406dab8357","affectsGlobalScope":true},{"version":"62d80405c46c3f4c527ee657ae9d43fda65a0bf582292429aea1e69144a522a6","affectsGlobalScope":true},{"version":"3013574108c36fd3aaca79764002b3717da09725a36a6fc02eac386593110f93","affectsGlobalScope":true},{"version":"75ec0bdd727d887f1b79ed6619412ea72ba3c81d92d0787ccb64bab18d261f14","affectsGlobalScope":true},{"version":"3be5a1453daa63e031d266bf342f3943603873d890ab8b9ada95e22389389006","affectsGlobalScope":true},{"version":"17bb1fc99591b00515502d264fa55dc8370c45c5298f4a5c2083557dccba5a2a","affectsGlobalScope":true},{"version":"7ce9f0bde3307ca1f944119f6365f2d776d281a393b576a18a2f2893a2d75c98","affectsGlobalScope":true},{"version":"6a6b173e739a6a99629a8594bfb294cc7329bfb7b227f12e1f7c11bc163b8577","affectsGlobalScope":true},{"version":"12a310447c5d23c7d0d5ca2af606e3bd08afda69100166730ab92c62999ebb9d","affectsGlobalScope":true},{"version":"b0124885ef82641903d232172577f2ceb5d3e60aed4da1153bab4221e1f6dd4e","affectsGlobalScope":true},{"version":"0eb85d6c590b0d577919a79e0084fa1744c1beba6fd0d4e951432fa1ede5510a","affectsGlobalScope":true},{"version":"da233fc1c8a377ba9e0bed690a73c290d843c2c3d23a7bd7ec5cd3d7d73ba1e0","affectsGlobalScope":true},{"version":"d154ea5bb7f7f9001ed9153e876b2d5b8f5c2bb9ec02b3ae0d239ec769f1f2ae","affectsGlobalScope":true},{"version":"bb2d3fb05a1d2ffbca947cc7cbc95d23e1d053d6595391bd325deb265a18d36c","affectsGlobalScope":true},{"version":"c80df75850fea5caa2afe43b9949338ce4e2de086f91713e9af1a06f973872b8","affectsGlobalScope":true},{"version":"9d57b2b5d15838ed094aa9ff1299eecef40b190722eb619bac4616657a05f951","affectsGlobalScope":true},{"version":"6c51b5dd26a2c31dbf37f00cfc32b2aa6a92e19c995aefb5b97a3a64f1ac99de","affectsGlobalScope":true},{"version":"6e7997ef61de3132e4d4b2250e75343f487903ddf5370e7ce33cf1b9db9a63ed","affectsGlobalScope":true},{"version":"2ad234885a4240522efccd77de6c7d99eecf9b4de0914adb9a35c0c22433f993","affectsGlobalScope":true},{"version":"1b3fe904465430e030c93239a348f05e1be80640d91f2f004c3512c2c2c89f34","affectsGlobalScope":true},{"version":"3787b83e297de7c315d55d4a7c546ae28e5f6c0a361b7a1dcec1f1f50a54ef11","affectsGlobalScope":true},{"version":"e7e8e1d368290e9295ef18ca23f405cf40d5456fa9f20db6373a61ca45f75f40","affectsGlobalScope":true},{"version":"faf0221ae0465363c842ce6aa8a0cbda5d9296940a8e26c86e04cc4081eea21e","affectsGlobalScope":true},{"version":"06393d13ea207a1bfe08ec8d7be562549c5e2da8983f2ee074e00002629d1871","affectsGlobalScope":true},{"version":"d071129cba6a5f2700be09c86c07ad2791ab67d4e5ed1eb301d6746c62745ea4","affectsGlobalScope":true},{"version":"10bbdc1981b8d9310ee75bfac28ee0477bb2353e8529da8cff7cb26c409cb5e8","affectsGlobalScope":true},"f99c7e2d91abca0ebff4879d507022e02bd72395d0748b8bce866d507fe6bcce","e03ddb11992c9c331c5ffc5554d54d1b44675ab2d4dafc76b0d60541621a95ff","7d5ac323e9bcbfd265848ed455a099275f330d4140da0c9ff9a1780e12b894d7","22305ce5dea49416b1f35905b005df3345c11af63c1375b5bcf8e424ad7a79c1","b0f41747cec9350dd64bbeed8939a6b9f56110af6c676be420b324059390b85e","9605dc46a2a97fc8a9c2f50f986cf845278a030025598e6f40ea067585d61f99","272c2dac4baaf7fdd2d7efeef0fa2547af54cc21883c5e138b8c4d1661697a54","8dfed5c91ad36e69e6da6b7e49be929d4e19666db2b651aa839c485170a2902c","64b867c61effed7b5bc0cc06b3d8eac23b067a3fba581fc7d3c292fa593e6a45","93de1c6dab503f053efe8d304cb522bb3a89feab8c98f307a674a4fae04773e9","3b043cf9a81854a72963fdb57d1884fc4da1cf5be69b5e0a4c5b751e58cb6d88","d0b0a00cf31968a33baeaadf974ce4e5e7edf58cea5288765293f41ba5e72b3a","0d5a2ee1fdfa82740e0103389b9efd6bfe145a20018a2da3c02b89666181f4d9","a69c09dbea52352f479d3e7ac949fde3d17b195abe90b045d619f747b38d6d1a",{"version":"92d63add669d18ebc349efbacd88966d6f2ccdddfb1b880b2db98ae3aa7bf7c4","affectsGlobalScope":true},"ccc94049a9841fe47abe5baef6be9a38fc6228807974ae675fb15dc22531b4be",{"version":"9acfe4d1ff027015151ce81d60797b04b52bffe97ad8310bb0ec2e8fd61e1303","affectsGlobalScope":true},"95843d5cfafced8f3f8a5ce57d2335f0bcd361b9483587d12a25e4bd403b8216","afc6e96061af46bcff47246158caee7e056f5288783f2d83d6858cd25be1c565",{"version":"34f5bcac12b36d70304b73de5f5aab3bb91bd9919f984be80579ebcad03a624e","affectsGlobalScope":true},"82408ed3e959ddc60d3e9904481b5a8dc16469928257af22a3f7d1a3bc7fd8c4","2f520601649a893e6a49a8851ebfcf4be8ce090dc1281c2a08a871cb04e8251f","f50c975ab7b50e25a69e3d8a3773894125b44e9698924105f23b812bf7488baf","2b8c764f856a1dd0a9a2bf23e5efddbff157de8138b0754010be561ae5fcaa90","ad4b60488fb1e562bf375dac9299815f7028bf667d9b5887b2d01d501b7d1ddd","246341c3a7a2638cf830d690e69de1e6085a102c6a30596435b050e6ac86c11a","6972fca26f6e9bd56197568d4379f99071a90766e06b4fcb5920a0130a9202be",{"version":"4a2628e95962c8ab756121faa3ac2ed348112ff7a87b5c286dd2cc3326546b4c","affectsGlobalScope":true},"6dfd135b38ab97c536d9c966fc5a5a879a19c6ed75c2c9633902be1ef0945ff7","a049a59a02009fc023684fcfaf0ac526fe36c35dcc5d2b7d620c1750ba11b083","a361a26932d73497a174a6d48c53cfedb55f735f20e8638fdf7b25cdeaac9ca4","b287b810b5035d5685f1df6e1e418f1ca452a3ed4f59fd5cc081dbf2045f0d9b","4b9a003b5c556c96784132945bb41c655ea11273b1917f5c8d0c154dd5fd20dd","a458dc78104cc80048ac24fdc02fe6dce254838094c2f25641b3f954d9721241",{"version":"e8b18c6385ff784228a6f369694fcf1a6b475355ba89090a88de13587a9391d5","affectsGlobalScope":true},"902cd98bf46e95caf4118a0733fb801e9e90eec3edaed6abdad77124afec9ca2","abc1c425b2ad6720433f40f1877abfa4223f0f3dd486c9c28c492179ca183cb6","cd4854d38f4eb5592afd98ab95ca17389a7dfe38013d9079e802d739bdbcc939","94eed4cc2f5f658d5e229ff1ccd38860bddf4233e347bf78edd2154dee1f2b99",{"version":"e51bee3200733b1f58818b5a9ea90fcd61c5b8afa3a0378391991f3696826a65","affectsGlobalScope":true},"9f1069b9e2c051737b1f9b4f1baf50e4a63385a6a89c32235549ae87fc3d5492","ee18f2da7a037c6ceeb112a084e485aead9ea166980bf433474559eac1b46553","29c2706fa0cc49a2bd90c83234da33d08bb9554ecec675e91c1f85087f5a5324","0acbf26bf958f9e80c1ffa587b74749d2697b75b484062d36e103c137c562bc3","d7838022c7dab596357a9604b9c6adffe37dc34085ce0779c958ce9545bd7139","1b952304137851e45bc009785de89ada562d9376177c97e37702e39e60c2f1ff",{"version":"806ef4cac3b3d9fa4a48d849c8e084d7c72fcd7b16d76e06049a9ed742ff79c0","affectsGlobalScope":true},"a279435e7813d1f061c0cab6ab77b1b9377e8d96851e5ed4a76a1ce6eb6e628f","c33a6ea7147af60d8e98f1ac127047f4b0d4e2ce28b8f08ff3de07ca7cc00637",{"version":"b42b47e17b8ece2424ae8039feb944c2e3ba4b262986aebd582e51efbdca93dc","affectsGlobalScope":true},"664d8f2d59164f2e08c543981453893bc7e003e4dfd29651ce09db13e9457980","2408611d9b4146e35d1dbd1f443ccd8e187c74614a54b80300728277529dbf11","998a3de5237518c0b3ac00a11b3b4417affb008aa20aedee52f3fdae3cb86151","ad41008ffe077206e1811fc873f4d9005b5fd7f6ab52bb6118fef600815a5cb4","d88ecca73348e7c337541c4b8b60a50aca5e87384f6b8a422fc6603c637e4c21","badae0df9a8016ac36994b0a0e7b82ba6aaa3528e175a8c3cb161e4683eec03e","c3db860bcaaaeb3bbc23f353bbda1f8ab82756c8d5e973bebb3953cb09ea68f2","235a53595bd20b0b0eeb1a29cb2887c67c48375e92f03749b2488fbd46d0b1a0","bc09393cd4cd13f69cf1366d4236fbae5359bb550f0de4e15767e9a91d63dfb1","9c266243b01545e11d2733a55ad02b4c00ecdbda99c561cd1674f96e89cdc958","c71155c05fc76ff948a4759abc1cb9feec036509f500174bc18dad4c7827a60c",{"version":"ab9b9a36e5284fd8d3bf2f7d5fcbc60052f25f27e4d20954782099282c60d23e","affectsGlobalScope":true},"e6ef68f677c1b63967d87568043b8af9d2dfd71d5873acd1de3abeb1db606741","3ebae8c00411116a66fca65b08228ea0cf0b72724701f9b854442100aab55aba","de18acda71730bac52f4b256ce7511bb56cc21f6f114c59c46782eff2f632857","7eb06594824ada538b1d8b48c3925a83e7db792f47a081a62cf3e5c4e23cf0ee","029769d13d9917e3284cb2356ed28a6576e8b07ae6a06ee1e672518adf21a102","6fa0008bf91a4cc9c8963bace4bba0bd6865cbfa29c3e3ccc461155660fb113a","29651525db5579157e617c77e869af8bfdc1130f5d811c1f759ad35b7bafc8ef","41422586881bcd739b4e62d9b91cd29909f8572aa3e3cdf316b7c50f14708d49","f7e133b20ee2669b6c0e5d7f0cd510868c57cd64b283e68c7f598e30ce9d76d2","09c4b2e2d3070239d563fc690f0cc5db04a2d9b66a23e61aef8b5274e3e9910c","75925c52e539a150d822acf3d43f41075d6c371215be27e0c81215a79a802fb4","b76275b4a94b85da9e4b6b1f8c2b619b996560f9e6030a454260240210a39dd8","8841e2aa774b89bd23302dede20663306dc1b9902431ac64b24be8b8d0e3f649","fd326577c62145816fe1acc306c734c2396487f76719d3785d4e825b34540b33","209e814e8e71aec74f69686a9506dd7610b97ab59dcee9446266446f72a76d05"],"options":{"allowSyntheticDefaultImports":true,"composite":true,"declaration":true,"declarationMap":true,"esModuleInterop":true,"module":1,"outDir":"./","rootDir":"..","sourceMap":true,"strict":true,"target":7},"fileIdsList":[[94],[94,111],[66,67,94,101,113],[40,94],[40,54,94],[45,94],[45,46,47,48,49,94],[45,47,94],[67,94,101],[94,103],[94,103,104],[51,94],[54,94],[55,60,94],[56,66,67,74,83,93,94],[56,57,66,74,94],[58,94],[59,60,67,75,94],[60,83,90,94],[61,63,66,74,94],[62,94],[63,64,94],[65,66,94],[66,94],[66,67,68,83,93,94],[66,67,68,83,94],[69,74,83,93,94],[66,67,69,70,74,83,90,93,94],[69,71,83,90,93,94],[51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100],[66,72,94],[73,93,94],[63,66,74,83,94],[75,94],[76,94],[54,77,94],[78,92,94,98],[79,94],[80,94],[66,81,94],[81,82,94,96],[66,83,84,85,94],[83,85,94],[83,84,94],[86,94],[87,94],[66,88,89,94],[88,89,94],[60,74,90,94],[91,94],[74,92,94],[55,69,80,93,94],[60,94],[83,94,95],[94,96],[94,97],[55,60,66,68,77,83,93,94,96,98],[83,94,99],[94,109],[42,43,94]],"referencedMap":[[111,1],[112,2],[114,3],[113,1],[115,1],[9,1],[8,1],[2,1],[10,1],[11,1],[12,1],[13,1],[14,1],[15,1],[16,1],[17,1],[3,1],[4,1],[21,1],[18,1],[19,1],[20,1],[22,1],[23,1],[24,1],[5,1],[25,1],[26,1],[27,1],[28,1],[6,1],[29,1],[30,1],[31,1],[32,1],[7,1],[37,1],[33,1],[34,1],[35,1],[36,1],[1,1],[38,1],[39,1],[41,4],[40,1],[42,5],[47,6],[45,1],[50,7],[46,6],[48,8],[49,6],[102,9],[103,1],[104,10],[105,11],[51,12],[52,12],[54,13],[55,14],[56,15],[57,16],[58,17],[59,18],[60,19],[61,20],[62,21],[63,22],[64,22],[65,23],[66,24],[67,25],[68,26],[53,1],[100,1],[69,27],[70,28],[71,29],[101,30],[72,31],[73,32],[74,33],[75,34],[76,35],[77,36],[78,37],[79,38],[80,39],[81,40],[82,41],[83,42],[85,43],[84,44],[86,45],[87,46],[88,47],[89,48],[90,49],[91,50],[92,51],[93,52],[94,53],[95,54],[96,55],[97,56],[98,57],[99,58],[106,1],[107,1],[108,1],[109,1],[110,59],[43,1],[44,60]],"exportedModulesMap":[[111,1],[112,2],[114,3],[113,1],[115,1],[9,1],[8,1],[2,1],[10,1],[11,1],[12,1],[13,1],[14,1],[15,1],[16,1],[17,1],[3,1],[4,1],[21,1],[18,1],[19,1],[20,1],[22,1],[23,1],[24,1],[5,1],[25,1],[26,1],[27,1],[28,1],[6,1],[29,1],[30,1],[31,1],[32,1],[7,1],[37,1],[33,1],[34,1],[35,1],[36,1],[1,1],[38,1],[39,1],[41,4],[40,1],[42,5],[47,6],[45,1],[50,7],[46,6],[48,8],[49,6],[102,9],[103,1],[104,10],[105,11],[51,12],[52,12],[54,13],[55,14],[56,15],[57,16],[58,17],[59,18],[60,19],[61,20],[62,21],[63,22],[64,22],[65,23],[66,24],[67,25],[68,26],[53,1],[100,1],[69,27],[70,28],[71,29],[101,30],[72,31],[73,32],[74,33],[75,34],[76,35],[77,36],[78,37],[79,38],[80,39],[81,40],[82,41],[83,42],[85,43],[84,44],[86,45],[87,46],[88,47],[89,48],[90,49],[91,50],[92,51],[93,52],[94,53],[95,54],[96,55],[97,56],[98,57],[99,58],[106,1],[107,1],[108,1],[109,1],[110,59],[43,1],[44,60]],"semanticDiagnosticsPerFile":[111,112,114,113,115,9,8,2,10,11,12,13,14,15,16,17,3,4,21,18,19,20,22,23,24,5,25,26,27,28,6,29,30,31,32,7,37,33,34,35,36,1,38,39,41,40,42,47,45,50,46,48,49,102,103,104,105,51,52,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,53,100,69,70,71,101,72,73,74,75,76,77,78,79,80,81,82,83,85,84,86,87,88,89,90,91,92,93,94,95,96,97,98,99,106,107,108,109,110,43,44]},"version":"4.5.2"}
|
package/package.json
CHANGED
|
@@ -1,23 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saltcorn/db-common",
|
|
3
|
-
"version": "0.6.2-beta.
|
|
3
|
+
"version": "0.6.2-beta.4",
|
|
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 dist --runInBand",
|
|
8
8
|
"tsc": "echo \"Error: no TypeScript support yet\"",
|
|
9
9
|
"clean": "echo \"Error: no TypeScript support yet\""
|
|
10
10
|
},
|
|
11
11
|
"author": "Tom Nielsen",
|
|
12
12
|
"license": "MIT",
|
|
13
|
-
"
|
|
13
|
+
"exports": {
|
|
14
|
+
"./*": "./dist/*.js"
|
|
15
|
+
},
|
|
16
|
+
"typesVersions": {
|
|
17
|
+
"*": {
|
|
18
|
+
"*": [
|
|
19
|
+
"dist/*"
|
|
20
|
+
]
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist/**/*"
|
|
25
|
+
],
|
|
14
26
|
"dependencies": {
|
|
15
|
-
"@saltcorn/markup": "0.6.2-beta.
|
|
27
|
+
"@saltcorn/markup": "0.6.2-beta.4",
|
|
16
28
|
"contractis": "^0.1.0"
|
|
17
29
|
},
|
|
18
30
|
"devDependencies": {
|
|
19
31
|
"jest": "^25.1.0"
|
|
20
32
|
},
|
|
33
|
+
"jest": {
|
|
34
|
+
"testEnvironment": "node"
|
|
35
|
+
},
|
|
21
36
|
"repository": "github:saltcorn/saltcorn",
|
|
22
37
|
"publishConfig": {
|
|
23
38
|
"access": "public"
|
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
|
-
};
|