@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
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
|
-
}
|