@saltcorn/db-common 0.6.1 → 0.6.2-beta.3

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/internal.test.js DELETED
@@ -1,154 +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 remove spaces", () => {
8
- expect(sqlsanitize(" ")).toBe("");
9
- });
10
- it("should remove chars from invalid name", () => {
11
- expect(sqlsanitize("ffoo--oo--uu")).toBe("ffoooouu");
12
- });
13
- it("should not allow dots", () => {
14
- expect(sqlsanitize("ffoo.oo")).toBe("ffoooo");
15
- });
16
- it("should allow dots when specified", () => {
17
- expect(sqlsanitizeAllowDots("ffoo.oo")).toBe("ffoo.oo");
18
- });
19
- it("should allow quotes when dots specified", () => {
20
- expect(sqlsanitizeAllowDots('ffoo."oo"')).toBe('ffoo."oo"');
21
- });
22
- it("should allow numbers", () => {
23
- expect(sqlsanitize("ff1oo_oo")).toBe("ff1oo_oo");
24
- });
25
- it("should not allow numbers in initial position", () => {
26
- expect(sqlsanitize("1ffoo_o1o")).toBe("_1ffoo_o1o");
27
- });
28
- });
29
-
30
- describe("mkWhere", () => {
31
- it("should empty on no arg", () => {
32
- expect(mkWhere()).toStrictEqual({ values: [], where: "" });
33
- });
34
- it("should empty on null obj arg", () => {
35
- expect(mkWhere({})).toStrictEqual({ values: [], where: "" });
36
- });
37
- it("should query json", () => {
38
- expect(mkWhere({ foo: { json: ["bar", 5] } })).toStrictEqual({
39
- values: [5],
40
- where: `where "foo"->>'bar'=$1`,
41
- });
42
- });
43
-
44
- it("should set id", () => {
45
- expect(mkWhere({ id: 5 })).toStrictEqual({
46
- values: [5],
47
- where: 'where "id"=$1',
48
- });
49
- expect(mkWhere({ id: 5, hello: "world" })).toStrictEqual({
50
- values: [5, "world"],
51
- where: 'where "id"=$1 and "hello"=$2',
52
- });
53
- });
54
- it("should query null", () => {
55
- expect(mkWhere({ id: null })).toStrictEqual({
56
- values: [],
57
- where: 'where "id" is null',
58
- });
59
- expect(mkWhere({ id: null, foo: 1 })).toStrictEqual({
60
- values: [1],
61
- where: 'where "id" is null and "foo"=$1',
62
- });
63
- expect(mkWhere({ foo: 1, id: null })).toStrictEqual({
64
- values: [1],
65
- where: 'where "foo"=$1 and "id" is null',
66
- });
67
- });
68
- it("should query lt/gt", () => {
69
- expect(mkWhere({ id: { lt: 5 } })).toStrictEqual({
70
- values: [5],
71
- where: 'where "id"<$1',
72
- });
73
- expect(mkWhere({ id: { gt: 8 } })).toStrictEqual({
74
- values: [8],
75
- where: 'where "id">$1',
76
- });
77
- expect(mkWhere({ id: { lt: 5, equal: true } })).toStrictEqual({
78
- values: [5],
79
- where: 'where "id"<=$1',
80
- });
81
- expect(mkWhere({ id: { gt: 8, equal: true } })).toStrictEqual({
82
- values: [8],
83
- where: 'where "id">=$1',
84
- });
85
- expect(mkWhere({ id: [{ gt: 0 }, { lt: 10 }] })).toStrictEqual({
86
- values: [0, 10],
87
- where: 'where "id">$1 and "id"<$2',
88
- });
89
- expect(mkWhere({ id: { or: [{ gt: 10 }, { lt: 5 }] } })).toStrictEqual({
90
- values: [10, 5],
91
- where: 'where ("id">$1 or "id"<$2)',
92
- });
93
- });
94
- it("should query subselect", () => {
95
- expect(
96
- mkWhere({
97
- id: [{ inSelect: { table: "foo", field: "bar", where: { baz: 7 } } }],
98
- })
99
- ).toStrictEqual({
100
- values: [7],
101
- where: 'where "id" in (select bar from foo where "baz"=$1)',
102
- });
103
- expect(
104
- mkWhere({
105
- age: 45,
106
- id: [{ inSelect: { table: "foo", field: "bar", where: { baz: 7 } } }],
107
- name: "Alice",
108
- })
109
- ).toStrictEqual({
110
- values: [45, 7, "Alice"],
111
- where: `where "age"=$1 and "id" in (select bar from foo where "baz"=$2) and "name"=$3`,
112
- });
113
- });
114
- it("should query or", () => {
115
- expect(mkWhere({ or: [{ id: 5 }, { x: 7 }] })).toStrictEqual({
116
- values: [5, 7],
117
- where: 'where ("id"=$1 or "x"=$2)',
118
- });
119
- expect(mkWhere({ or: [{ id: 5 }, { x: { gt: 7 } }] })).toStrictEqual({
120
- values: [5, 7],
121
- where: 'where ("id"=$1 or "x">$2)',
122
- });
123
- expect(mkWhere({ or: [{ id: 5 }, { x: 7, y: 8 }] })).toStrictEqual({
124
- values: [5, 7, 8],
125
- where: 'where ("id"=$1 or "x"=$2 and "y"=$3)',
126
- });
127
- expect(mkWhere({ not: { id: 5 } })).toStrictEqual({
128
- values: [5],
129
- where: 'where not ("id"=$1)',
130
- });
131
- expect(mkWhere({ not: { id: 5, y: 1 } })).toStrictEqual({
132
- values: [5, 1],
133
- where: 'where not ("id"=$1 and "y"=$2)',
134
- });
135
- expect(mkWhere({ not: { y: { in: [1, 2, 3] } } })).toStrictEqual({
136
- values: [[1, 2, 3]],
137
- where: 'where not ("y" = ANY ($1))',
138
- });
139
- expect(mkWhere({ y: { in: [1, 2, 3] } })).toStrictEqual({
140
- values: [[1, 2, 3]],
141
- where: 'where "y" = ANY ($1)',
142
- });
143
- /*
144
- TODO
145
- expect(mkWhere([{ id: 5 }, { x: 7 }])).toStrictEqual({
146
- values: [5, 7],
147
- where: 'where "id"=$1 and "x"=$2',
148
- });
149
- expect(mkWhere([{ or: [{ id: 5 }, { x: 7 }] }, { z: 9 }])).toStrictEqual({
150
- values: [5, 7, 9],
151
- where: 'where ("id"=$1 or "x"=$2) and "z"=$3',
152
- });*/
153
- });
154
- });
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
- }