@stratum-hq/mysql 0.4.0 → 0.5.0
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/README.md +10 -4
- package/dist/adapters/table.d.ts +3 -0
- package/dist/adapters/table.d.ts.map +1 -1
- package/dist/adapters/table.js +8 -1
- package/dist/adapters/table.js.map +1 -1
- package/dist/integrations/knex.d.ts +9 -0
- package/dist/integrations/knex.d.ts.map +1 -1
- package/dist/integrations/knex.js +38 -5
- package/dist/integrations/knex.js.map +1 -1
- package/dist/integrations/typeorm-subscriber.d.ts +37 -5
- package/dist/integrations/typeorm-subscriber.d.ts.map +1 -1
- package/dist/integrations/typeorm-subscriber.js +160 -5
- package/dist/integrations/typeorm-subscriber.js.map +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -46,7 +46,7 @@ import { MysqlTableAdapter } from "@stratum-hq/mysql";
|
|
|
46
46
|
const adapter = new MysqlTableAdapter({
|
|
47
47
|
pool,
|
|
48
48
|
databaseName: "myapp",
|
|
49
|
-
// Every base table that has a per-tenant copy. Required by purgeTenantData.
|
|
49
|
+
// Every base table that has a per-tenant copy. Required by scopedTable and purgeTenantData.
|
|
50
50
|
baseTables: ["users", "orders"],
|
|
51
51
|
});
|
|
52
52
|
|
|
@@ -79,7 +79,7 @@ await adapter.closeAll();
|
|
|
79
79
|
|
|
80
80
|
## ORM Integrations
|
|
81
81
|
|
|
82
|
-
### TypeORM Subscriber (writes only)
|
|
82
|
+
### TypeORM Subscriber (writes only; reads are not scoped)
|
|
83
83
|
|
|
84
84
|
```typescript
|
|
85
85
|
import { registerStratumSubscriber } from "@stratum-hq/mysql";
|
|
@@ -93,9 +93,13 @@ Call `registerStratumSubscriber()` after `dataSource.initialize()`. It throws be
|
|
|
93
93
|
|
|
94
94
|
Inserts get the current tenant's `tenant_id`. Updates never change `tenant_id`: `save()` keeps the loaded value, and `update()` / query builder updates drop it from the SET values.
|
|
95
95
|
|
|
96
|
+
`registerStratumSubscriber()` also scopes updates and deletes to the current tenant: repository `update()`, `delete()`, `softDelete()`, `restore()`, `save()` of an existing row, `remove()`, and query builder updates and deletes get `tenant_id = <current tenant>` ANDed to their WHERE clause. A row of another tenant is left unchanged, `save()` of a row that belongs to another tenant throws, and an update or delete of a tenant table outside a tenant context is refused. An update or delete builder aimed at a raw table name (not an entity) is treated as a tenant table and always gets the tenant condition, so it fails on a table without `tenant_id`. `TRUNCATE` of a table with a `tenant_id` column (`repository.clear()`, `queryRunner.clearTable()`) is refused, because it would remove every tenant's rows. A subscriber added to `dataSource.subscribers` by hand refuses every UPDATE and DELETE, so always register it with `registerStratumSubscriber()`.
|
|
97
|
+
|
|
96
98
|
Upserts (`repository.upsert()` and `.orUpdate()`) also get the current tenant's `tenant_id` on insert. If the conflict update writes `tenant_id`, the subscriber rejects the statement before it runs. To upsert, leave `tenant_id` out of the entity values and out of the `orUpdate()` columns.
|
|
97
99
|
|
|
98
|
-
|
|
100
|
+
MySQL applies `ON DUPLICATE KEY UPDATE` on a conflict with any unique key of the table, whatever conflict columns you pass. The subscriber therefore allows an upsert only when every unique key of the target table, including the primary key, contains `tenant_id` (for example `PRIMARY KEY (tenant_id, id)`). It reads the keys from `information_schema` before the statement runs, and rejects the upsert otherwise.
|
|
101
|
+
|
|
102
|
+
**Limitation:** reads (`find()`, `findOne()`, query builder selects, and the row that `save()` loads before it updates), raw SQL (`dataSource.query()`), and SQL taken from `getQuery()` / `getQueryAndParameters()` and run by hand are not scoped; the tenant condition is added only when a builder's `execute()` runs. Add the tenant condition yourself, or use the shared-table adapter's structured methods for tenant-scoped reads.
|
|
99
103
|
|
|
100
104
|
### Knex Helper
|
|
101
105
|
|
|
@@ -107,7 +111,9 @@ const users = await tenantKnex("users").where("name", "like", q).orWhere("email"
|
|
|
107
111
|
// Compiles to: WHERE tenant_id = 'tenant-a' AND (name LIKE ? OR email LIKE ?)
|
|
108
112
|
```
|
|
109
113
|
|
|
110
|
-
Your where clauses are always grouped after the tenant filter, including on clones and when the builder is used as a subquery. `insert()` sets `tenant_id`, `update()` never changes it, and `onConflict().merge()`, `upsert()` and `
|
|
114
|
+
Your where clauses are always grouped after the tenant filter, including on clones and when the builder is used as a subquery. `insert()` sets `tenant_id`, `update()` never changes it, and `onConflict().merge()`, `upsert()`, `truncate()` and `modify()` throw (a `modify()` callback would call the builder without these rules).
|
|
115
|
+
|
|
116
|
+
Joins (`join()`, `leftJoin()`, `crossJoin()`, `joinRaw()` and the other join forms) and `union()` / `unionAll()` also throw, because the tenant filter covers only the builder's own table. To combine tables, use a tenant-scoped builder as a `whereIn()` subquery, or write the query with plain Knex and a `tenant_id` condition on every table.
|
|
111
117
|
|
|
112
118
|
### Sequelize Adapter
|
|
113
119
|
|
package/dist/adapters/table.d.ts
CHANGED
|
@@ -13,6 +13,9 @@ export declare class MysqlTableAdapter implements MysqlAdapter {
|
|
|
13
13
|
/**
|
|
14
14
|
* Validates slug and returns the escaped tenant-scoped table name
|
|
15
15
|
* in the form `{baseTableName}_{tenantSlug}`.
|
|
16
|
+
*
|
|
17
|
+
* Throws when baseTables is not set: without the list, two tenants can get
|
|
18
|
+
* the same name, e.g. ("corp_acme", "orders") and ("acme", "orders_corp").
|
|
16
19
|
*/
|
|
17
20
|
scopedTable(tenantSlug: string, baseTableName: string): string;
|
|
18
21
|
/** Returns the underlying pool for raw queries against tenant tables. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/adapters/table.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,wBAAwB,EACxB,aAAa,EACb,WAAW,EACX,YAAY,EACb,MAAM,aAAa,CAAC;AAGrB;;;;;GAKG;AACH,qBAAa,iBAAkB,YAAW,YAAY;IACpD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAgB;IACrC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAuB;gBAEtC,OAAO,EAAE,wBAAwB;IAkB7C
|
|
1
|
+
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/adapters/table.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,wBAAwB,EACxB,aAAa,EACb,WAAW,EACX,YAAY,EACb,MAAM,aAAa,CAAC;AAGrB;;;;;GAKG;AACH,qBAAa,iBAAkB,YAAW,YAAY;IACpD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAgB;IACrC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAuB;gBAEtC,OAAO,EAAE,wBAAwB;IAkB7C;;;;;;OAMG;IACH,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,MAAM;IAe9D,yEAAyE;IACzE,OAAO,IAAI,aAAa;IAIxB;;;;OAIG;IACG,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAgC/D,kCAAkC;IAClC,QAAQ,IAAI,YAAY;CAGzB"}
|
package/dist/adapters/table.js
CHANGED
|
@@ -31,10 +31,17 @@ class MysqlTableAdapter {
|
|
|
31
31
|
/**
|
|
32
32
|
* Validates slug and returns the escaped tenant-scoped table name
|
|
33
33
|
* in the form `{baseTableName}_{tenantSlug}`.
|
|
34
|
+
*
|
|
35
|
+
* Throws when baseTables is not set: without the list, two tenants can get
|
|
36
|
+
* the same name, e.g. ("corp_acme", "orders") and ("acme", "orders_corp").
|
|
34
37
|
*/
|
|
35
38
|
scopedTable(tenantSlug, baseTableName) {
|
|
36
39
|
(0, core_1.validateSlug)(tenantSlug);
|
|
37
|
-
if (
|
|
40
|
+
if (!this.baseTables) {
|
|
41
|
+
throw new Error("MysqlTableAdapter: scopedTable requires the baseTables option, " +
|
|
42
|
+
"listing every base table name that has a per-tenant copy");
|
|
43
|
+
}
|
|
44
|
+
if (!this.baseTables.includes(baseTableName)) {
|
|
38
45
|
throw new Error(`MysqlTableAdapter: "${baseTableName}" is not in baseTables`);
|
|
39
46
|
}
|
|
40
47
|
const tableName = `${baseTableName}_${tenantSlug}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.js","sourceRoot":"","sources":["../../src/adapters/table.ts"],"names":[],"mappings":";;;AAAA,2CAAgD;AAQhD,0CAA+D;AAE/D;;;;;GAKG;AACH,MAAa,iBAAiB;IACX,IAAI,CAAgB;IACpB,YAAY,CAAS;IACrB,UAAU,CAAuB;IAElD,YAAY,OAAiC;QAC3C,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;QACzC,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YACvB,KAAK,MAAM,CAAC,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;gBACnC,KAAK,MAAM,CAAC,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;oBACnC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,CAAC;wBACtD,MAAM,IAAI,KAAK,CACb,mCAAmC,CAAC,UAAU,CAAC,mBAAmB;4BAChE,YAAY,CAAC,iCAAiC,CAAC,UAAU,CAC5D,CAAC;oBACJ,CAAC;gBACH,CAAC;YACH,CAAC;YACD,IAAI,CAAC,UAAU,GAAG,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;IAED
|
|
1
|
+
{"version":3,"file":"table.js","sourceRoot":"","sources":["../../src/adapters/table.ts"],"names":[],"mappings":";;;AAAA,2CAAgD;AAQhD,0CAA+D;AAE/D;;;;;GAKG;AACH,MAAa,iBAAiB;IACX,IAAI,CAAgB;IACpB,YAAY,CAAS;IACrB,UAAU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C;IAED;;;;OAIG;IACH,KAAK,CAAC,eAAe,CAAC,UAAkB;QACtC,IAAA,yBAAc,EAAC,UAAU,CAAC,CAAC;QAC3B,IAAA,mBAAY,EAAC,UAAU,CAAC,CAAC;QACzB,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CACb,qEAAqE;gBACnE,0DAA0D,CAC7D,CAAC;QACJ,CAAC;QAED,MAAM,SAAS,GAAG,IAAA,2BAAgB,EAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QACtD,MAAM,MAAM,GAA0B,EAAE,CAAC;QACzC,IAAI,eAAe,GAAG,CAAC,CAAC;QAExB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACnC,MAAM,SAAS,GAAG,GAAG,IAAI,IAAI,UAAU,EAAE,CAAC;YAC1C,IAAI,CAAC;gBACH,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,wBAAwB,SAAS,IAAI,IAAA,2BAAgB,EAAC,SAAS,CAAC,EAAE,CAAC,CAAC;gBAC1F,eAAe,EAAE,CAAC;YACpB,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,MAAM,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,GAAY,EAAE,CAAC,CAAC;YACzD,CAAC;QACH,CAAC;QAED,OAAO;YACL,OAAO,EAAE,MAAM,CAAC,MAAM,KAAK,CAAC;YAC5B,eAAe;YACf,WAAW,EAAE,CAAC;YACd,MAAM;SACP,CAAC;IACJ,CAAC;IAED,kCAAkC;IAClC,QAAQ;QACN,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,CAAC;IAC1C,CAAC;CACF;AA3FD,8CA2FC"}
|
|
@@ -26,6 +26,15 @@ export interface KnexLike {
|
|
|
26
26
|
* UPDATE never changes tenant_id: the column is dropped from the update data.
|
|
27
27
|
* onConflict().merge(), upsert() and truncate() throw, because MySQL applies
|
|
28
28
|
* none of them through the WHERE clause. onConflict().ignore() is allowed.
|
|
29
|
+
*
|
|
30
|
+
* modify() throws, because its callback would call the builder's methods
|
|
31
|
+
* without the insert, update and onConflict rules above.
|
|
32
|
+
*
|
|
33
|
+
* Joins (join, innerJoin, leftJoin, crossJoin, joinRaw and the other forms)
|
|
34
|
+
* and union() / unionAll() throw, because the tenant filter covers only this
|
|
35
|
+
* builder's table, not the joined or unioned rows. To combine tenant data, use
|
|
36
|
+
* a tenant-scoped builder as a whereIn subquery, or write the query with an
|
|
37
|
+
* explicit tenant_id condition on every table.
|
|
29
38
|
*/
|
|
30
39
|
export declare function withTenantScope(knex: KnexLike, tenantId: string): (tableName: string) => KnexQueryBuilderLike;
|
|
31
40
|
//# sourceMappingURL=knex.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"knex.d.ts","sourceRoot":"","sources":["../../src/integrations/knex.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,oBAAoB;IACnC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,IAAI,CAAC;IACvC,MAAM,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAChC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACpF,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAC5C,MAAM,IAAI,IAAI,CAAC;IACf,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC3B;AAED,MAAM,WAAW,QAAQ;IACvB,CAAC,SAAS,EAAE,MAAM,GAAG,oBAAoB,CAAC;CAC3C;
|
|
1
|
+
{"version":3,"file":"knex.d.ts","sourceRoot":"","sources":["../../src/integrations/knex.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,oBAAoB;IACnC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,IAAI,CAAC;IACvC,MAAM,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAChC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACpF,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAC5C,MAAM,IAAI,IAAI,CAAC;IACf,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC3B;AAED,MAAM,WAAW,QAAQ;IACvB,CAAC,SAAS,EAAE,MAAM,GAAG,oBAAoB,CAAC;CAC3C;AAsDD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,QAAQ,EACd,QAAQ,EAAE,MAAM,GACf,CAAC,SAAS,EAAE,MAAM,KAAK,oBAAoB,CAwI7C"}
|
|
@@ -3,8 +3,32 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.withTenantScope = withTenantScope;
|
|
5
5
|
const TENANT_COLUMN = "tenant_id";
|
|
6
|
-
/**
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Builder methods the tenant filter cannot cover, mapped to the reason given
|
|
8
|
+
* in the error: writes that MySQL does not apply through the WHERE clause, and
|
|
9
|
+
* joins and unions, whose other rows the filter does not reach.
|
|
10
|
+
*/
|
|
11
|
+
const NOT_FILTERED = "because MySQL does not apply the tenant filter to it";
|
|
12
|
+
const NOT_SCOPED = "because the tenant filter does not apply to the rows it adds";
|
|
13
|
+
const REFUSED_METHODS = new Map([
|
|
14
|
+
["upsert", NOT_FILTERED],
|
|
15
|
+
["truncate", NOT_FILTERED],
|
|
16
|
+
["modify", "because the tenant rules for insert, update and onConflict do not apply inside its callback"],
|
|
17
|
+
...[
|
|
18
|
+
"join",
|
|
19
|
+
"innerJoin",
|
|
20
|
+
"leftJoin",
|
|
21
|
+
"leftOuterJoin",
|
|
22
|
+
"rightJoin",
|
|
23
|
+
"rightOuterJoin",
|
|
24
|
+
"outerJoin",
|
|
25
|
+
"fullOuterJoin",
|
|
26
|
+
"crossJoin",
|
|
27
|
+
"joinRaw",
|
|
28
|
+
"union",
|
|
29
|
+
"unionAll",
|
|
30
|
+
].map((method) => [method, NOT_SCOPED]),
|
|
31
|
+
]);
|
|
8
32
|
function isTenantColumn(name) {
|
|
9
33
|
return name.toLowerCase() === TENANT_COLUMN;
|
|
10
34
|
}
|
|
@@ -33,6 +57,15 @@ function withoutTenantColumn(row) {
|
|
|
33
57
|
* UPDATE never changes tenant_id: the column is dropped from the update data.
|
|
34
58
|
* onConflict().merge(), upsert() and truncate() throw, because MySQL applies
|
|
35
59
|
* none of them through the WHERE clause. onConflict().ignore() is allowed.
|
|
60
|
+
*
|
|
61
|
+
* modify() throws, because its callback would call the builder's methods
|
|
62
|
+
* without the insert, update and onConflict rules above.
|
|
63
|
+
*
|
|
64
|
+
* Joins (join, innerJoin, leftJoin, crossJoin, joinRaw and the other forms)
|
|
65
|
+
* and union() / unionAll() throw, because the tenant filter covers only this
|
|
66
|
+
* builder's table, not the joined or unioned rows. To combine tenant data, use
|
|
67
|
+
* a tenant-scoped builder as a whereIn subquery, or write the query with an
|
|
68
|
+
* explicit tenant_id condition on every table.
|
|
36
69
|
*/
|
|
37
70
|
function withTenantScope(knex, tenantId) {
|
|
38
71
|
// Statement objects this module created, so they can be recognized again on
|
|
@@ -96,10 +129,10 @@ function withTenantScope(knex, tenantId) {
|
|
|
96
129
|
if (typeof value !== "function" || typeof prop !== "string" || prop === "constructor") {
|
|
97
130
|
return value;
|
|
98
131
|
}
|
|
99
|
-
|
|
132
|
+
const refusal = REFUSED_METHODS.get(prop);
|
|
133
|
+
if (refusal) {
|
|
100
134
|
return () => {
|
|
101
|
-
throw new Error(`withTenantScope: ${prop}() is not allowed on a tenant-scoped builder, `
|
|
102
|
-
`because MySQL does not apply the tenant filter to it`);
|
|
135
|
+
throw new Error(`withTenantScope: ${prop}() is not allowed on a tenant-scoped builder, ${refusal}`);
|
|
103
136
|
};
|
|
104
137
|
}
|
|
105
138
|
if (depth > 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"knex.js","sourceRoot":"","sources":["../../src/integrations/knex.ts"],"names":[],"mappings":";AAAA,gEAAgE;;
|
|
1
|
+
{"version":3,"file":"knex.js","sourceRoot":"","sources":["../../src/integrations/knex.ts"],"names":[],"mappings":";AAAA,gEAAgE;;AA8FhE,0CA2IC;AA/MD,MAAM,aAAa,GAAG,WAAW,CAAC;AAElC;;;;GAIG;AACH,MAAM,YAAY,GAAG,sDAAsD,CAAC;AAC5E,MAAM,UAAU,GAAG,8DAA8D,CAAC;AAClF,MAAM,eAAe,GAAG,IAAI,GAAG,CAAiB;IAC9C,CAAC,QAAQ,EAAE,YAAY,CAAC;IACxB,CAAC,UAAU,EAAE,YAAY,CAAC;IAC1B,CAAC,QAAQ,EAAE,6FAA6F,CAAC;IACzG,GAAG;QACD,MAAM;QACN,WAAW;QACX,UAAU;QACV,eAAe;QACf,WAAW;QACX,gBAAgB;QAChB,WAAW;QACX,eAAe;QACf,WAAW;QACX,SAAS;QACT,OAAO;QACP,UAAU;KACX,CAAC,GAAG,CAAC,CAAC,MAAM,EAAoB,EAAE,CAAC,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CAC1D,CAAC,CAAC;AAEH,SAAS,cAAc,CAAC,IAAY;IAClC,OAAO,IAAI,CAAC,WAAW,EAAE,KAAK,aAAa,CAAC;AAC9C,CAAC;AAED,SAAS,mBAAmB,CAAC,GAA4B;IACvD,MAAM,IAAI,GAA4B,EAAE,CAAC;IAC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,IAAI,KAAK,OAAO;wBAAE,OAAO,KAAK,CAAC,MAA8B,CAAC,CAAC;oBACnE,OAAO,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC;gBACzC,CAAC,CAAC;YACJ,CAAC;SACF,CAAC,CAAC;QACH,SAAS,CAAC,MAAM,CAAC,CAAC;QAClB,OAAO,KAAwC,CAAC;IAClD,CAAC;IAED,OAAO,CAAC,SAAiB,EAAE,EAAE,CAC3B,KAAK,CAAC,IAAI,CAAC,SAAS,CAAoC,CAAC,CAAC;AAC9D,CAAC"}
|
|
@@ -16,6 +16,12 @@ export interface UpdateEvent {
|
|
|
16
16
|
export interface BeforeQueryEvent {
|
|
17
17
|
/** The SQL text that TypeORM is about to send to MySQL. */
|
|
18
18
|
query: string;
|
|
19
|
+
/** The query runner that is about to send the query. */
|
|
20
|
+
queryRunner?: {
|
|
21
|
+
query(sql: string, parameters?: unknown[]): Promise<unknown>;
|
|
22
|
+
};
|
|
23
|
+
/** The data source that is about to send the query. */
|
|
24
|
+
dataSource?: object;
|
|
19
25
|
}
|
|
20
26
|
export interface EntitySubscriberInterface {
|
|
21
27
|
beforeInsert?(event: InsertEvent): void | Promise<void>;
|
|
@@ -26,6 +32,12 @@ export interface EntitySubscriberInterface {
|
|
|
26
32
|
export interface TypeOrmDataSourceLike {
|
|
27
33
|
readonly isInitialized: boolean;
|
|
28
34
|
readonly subscribers: unknown[];
|
|
35
|
+
/** Used once to reach TypeORM's update, delete and soft-delete query builder classes. */
|
|
36
|
+
createQueryBuilder(): {
|
|
37
|
+
update(): object;
|
|
38
|
+
delete(): object;
|
|
39
|
+
softDelete(): object;
|
|
40
|
+
};
|
|
29
41
|
}
|
|
30
42
|
/**
|
|
31
43
|
* TypeORM subscriber that injects tenant_id into inserted entities via ALS and
|
|
@@ -34,10 +46,14 @@ export interface TypeOrmDataSourceLike {
|
|
|
34
46
|
* Register it with `registerStratumSubscriber(dataSource)` after
|
|
35
47
|
* `dataSource.initialize()`. TypeORM's `subscribers` option only loads classes
|
|
36
48
|
* decorated with `@EventSubscriber()`, so it does not load this class.
|
|
49
|
+
* Registration also scopes TypeORM's update, delete and soft-delete query
|
|
50
|
+
* builders (repository update(), delete(), save() of an existing row, remove(),
|
|
51
|
+
* softDelete(), restore()) to the current tenant: `tenant_id = <tenant>` is
|
|
52
|
+
* ANDed to their WHERE clause, and they are refused outside a tenant context.
|
|
37
53
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
54
|
+
* Not scoped: reads (find, findOne, query builder selects) and raw SQL
|
|
55
|
+
* (`dataSource.query()`). Add the tenant condition to those yourself, or use
|
|
56
|
+
* the shared-table adapter's structured methods.
|
|
41
57
|
*/
|
|
42
58
|
export declare class StratumTypeOrmSubscriber implements EntitySubscriberInterface {
|
|
43
59
|
/** Injects the current tenant's ID into the entity before insert. */
|
|
@@ -47,18 +63,30 @@ export declare class StratumTypeOrmSubscriber implements EntitySubscriberInterfa
|
|
|
47
63
|
* so the column is unchanged; on a repository or query builder update the
|
|
48
64
|
* column is dropped from the SET values (MySQL column names are
|
|
49
65
|
* case-insensitive, so any letter case is dropped).
|
|
66
|
+
*
|
|
67
|
+
* @throws Error when save() loaded a row whose tenant_id is not the current
|
|
68
|
+
* tenant's. The loaded tenant_id is not copied onto the entity.
|
|
50
69
|
*/
|
|
51
70
|
beforeUpdate(event: UpdateEvent): void;
|
|
52
71
|
/**
|
|
53
|
-
* Rejects an upsert that
|
|
72
|
+
* Rejects an upsert that could update another tenant's row.
|
|
54
73
|
*
|
|
55
74
|
* A conflict update must never change the tenant of an existing row. A
|
|
56
75
|
* subscriber cannot remove one column from the conflict update, so the
|
|
57
76
|
* statement fails before it runs.
|
|
58
77
|
*
|
|
78
|
+
* MySQL runs ON DUPLICATE KEY UPDATE on a conflict with any unique key of
|
|
79
|
+
* the table, whatever conflict columns were passed to upsert() or
|
|
80
|
+
* orUpdate(). The upsert is therefore allowed only when every unique key
|
|
81
|
+
* (including the primary key) of the target table contains tenant_id, so a
|
|
82
|
+
* conflict can only ever be with a row of the same tenant. The keys are read
|
|
83
|
+
* from information_schema through the same query runner.
|
|
84
|
+
*
|
|
59
85
|
* @throws Error when the ON DUPLICATE KEY UPDATE clause assigns tenant_id.
|
|
86
|
+
* @returns A promise that rejects when a unique key of the table does not
|
|
87
|
+
* include tenant_id, or when the table cannot be determined.
|
|
60
88
|
*/
|
|
61
|
-
beforeQuery(event: BeforeQueryEvent): void
|
|
89
|
+
beforeQuery(event: BeforeQueryEvent): void | Promise<void>;
|
|
62
90
|
}
|
|
63
91
|
/**
|
|
64
92
|
* Returns the one StratumTypeOrmSubscriber on the data source, and adds it first when it is missing.
|
|
@@ -66,6 +94,10 @@ export declare class StratumTypeOrmSubscriber implements EntitySubscriberInterfa
|
|
|
66
94
|
* A second call returns the subscriber that the first call added, so the
|
|
67
95
|
* data source never runs the subscriber twice.
|
|
68
96
|
*
|
|
97
|
+
* It also scopes TypeORM's update, delete and soft-delete query builders to the
|
|
98
|
+
* current tenant (see StratumTypeOrmSubscriber). The subscriber refuses an
|
|
99
|
+
* UPDATE or DELETE on a data source that was not registered this way.
|
|
100
|
+
*
|
|
69
101
|
* @param dataSource - An initialized TypeORM `DataSource`.
|
|
70
102
|
* @throws Error when the data source is not initialized. `initialize()` replaces
|
|
71
103
|
* the subscriber list, so an earlier registration has no effect.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeorm-subscriber.d.ts","sourceRoot":"","sources":["../../src/integrations/typeorm-subscriber.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC;AAED,MAAM,WAAW,WAAW;IAC1B,wFAAwF;IACxF,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IACxC,yEAAyE;IACzE,cAAc,CAAC,EAA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AAC,KAAK,EAAE,WAAW,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxD,WAAW,CAAC,CAAC,KAAK,EAAE,gBAAgB,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7D;AAED,4EAA4E;AAC5E,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,WAAW,EAAE,OAAO,EAAE,CAAC;IAChC,yFAAyF;IACzF,kBAAkB,IAAI;QAAE,MAAM,IAAI,MAAM,CAAC;QAAC,MAAM,IAAI,MAAM,CAAC;QAAC,UAAU,IAAI,MAAM,CAAA;KAAE,CAAC;CACpF;AAyCD;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,wBAAyB,YAAW,yBAAyB;IACxE,qEAAqE;IACrE,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI;IAMtC;;;;;;;;OAQG;IACH,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI;IA4CtC;;;;;;;;;;;;;;;;;OAiBG;IACH,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CAsB3D;AA+GD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,yBAAyB,CACvC,UAAU,EAAE,qBAAqB,GAChC,wBAAwB,CAiB1B"}
|
|
@@ -8,6 +8,20 @@ const utils_js_1 = require("../utils.js");
|
|
|
8
8
|
// quotes each column with backticks.
|
|
9
9
|
const UPSERT_CLAUSE = /\bON\s+DUPLICATE\s+KEY\s+UPDATE\b/i;
|
|
10
10
|
const TENANT_ASSIGNMENT = /`tenant_id`\s*=/i;
|
|
11
|
+
// The target of an INSERT, as TypeORM writes it: `table` or `schema`.`table`.
|
|
12
|
+
const INSERT_TARGET = /^\s*INSERT\s+(?:IGNORE\s+)?INTO\s+(?:`((?:[^`]|``)+)`\.)?`((?:[^`]|``)+)`/i;
|
|
13
|
+
const UPDATE_OR_DELETE = /^\s*(?:UPDATE|DELETE)\b/i;
|
|
14
|
+
// The target of a TRUNCATE, as TypeORM writes it: `table` or `schema`.`table`.
|
|
15
|
+
const TRUNCATE_TARGET = /^\s*TRUNCATE\s+(?:TABLE\s+)?(?:`((?:[^`]|``)+)`\.)?`?((?:[^`\s;]|``)+)`?/i;
|
|
16
|
+
const TENANT_COLUMN_SQL = "SELECT COUNT(*) AS n FROM information_schema.COLUMNS " +
|
|
17
|
+
"WHERE TABLE_SCHEMA = COALESCE(?, DATABASE()) AND TABLE_NAME = ? AND LOWER(COLUMN_NAME) = 'tenant_id'";
|
|
18
|
+
const TENANT_PARAMETER = "stratumTenantId";
|
|
19
|
+
const SCOPED_EXECUTE = Symbol("stratum.tenantScopedExecute");
|
|
20
|
+
/** Data sources whose update and delete query builders are tenant-scoped. */
|
|
21
|
+
const scopedDataSources = new WeakSet();
|
|
22
|
+
const UNIQUE_KEYS_SQL = "SELECT INDEX_NAME AS index_name, COLUMN_NAME AS column_name " +
|
|
23
|
+
"FROM information_schema.STATISTICS " +
|
|
24
|
+
"WHERE TABLE_SCHEMA = COALESCE(?, DATABASE()) AND TABLE_NAME = ? AND NON_UNIQUE = 0";
|
|
11
25
|
/**
|
|
12
26
|
* TypeORM subscriber that injects tenant_id into inserted entities via ALS and
|
|
13
27
|
* never lets an update change tenant_id.
|
|
@@ -15,10 +29,14 @@ const TENANT_ASSIGNMENT = /`tenant_id`\s*=/i;
|
|
|
15
29
|
* Register it with `registerStratumSubscriber(dataSource)` after
|
|
16
30
|
* `dataSource.initialize()`. TypeORM's `subscribers` option only loads classes
|
|
17
31
|
* decorated with `@EventSubscriber()`, so it does not load this class.
|
|
32
|
+
* Registration also scopes TypeORM's update, delete and soft-delete query
|
|
33
|
+
* builders (repository update(), delete(), save() of an existing row, remove(),
|
|
34
|
+
* softDelete(), restore()) to the current tenant: `tenant_id = <tenant>` is
|
|
35
|
+
* ANDed to their WHERE clause, and they are refused outside a tenant context.
|
|
18
36
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
37
|
+
* Not scoped: reads (find, findOne, query builder selects) and raw SQL
|
|
38
|
+
* (`dataSource.query()`). Add the tenant condition to those yourself, or use
|
|
39
|
+
* the shared-table adapter's structured methods.
|
|
22
40
|
*/
|
|
23
41
|
class StratumTypeOrmSubscriber {
|
|
24
42
|
/** Injects the current tenant's ID into the entity before insert. */
|
|
@@ -32,11 +50,32 @@ class StratumTypeOrmSubscriber {
|
|
|
32
50
|
* so the column is unchanged; on a repository or query builder update the
|
|
33
51
|
* column is dropped from the SET values (MySQL column names are
|
|
34
52
|
* case-insensitive, so any letter case is dropped).
|
|
53
|
+
*
|
|
54
|
+
* @throws Error when save() loaded a row whose tenant_id is not the current
|
|
55
|
+
* tenant's. The loaded tenant_id is not copied onto the entity.
|
|
35
56
|
*/
|
|
36
57
|
beforeUpdate(event) {
|
|
37
58
|
const entity = event.entity;
|
|
38
59
|
if (!entity)
|
|
39
60
|
return;
|
|
61
|
+
const loaded = event.databaseEntity;
|
|
62
|
+
if (loaded) {
|
|
63
|
+
const loadedProps = new Set(Object.keys(loaded).filter((key) => key.toLowerCase() === "tenant_id"));
|
|
64
|
+
for (const column of event.metadata?.columns ?? []) {
|
|
65
|
+
if (column.databaseName.toLowerCase() === "tenant_id" && column.propertyName in loaded) {
|
|
66
|
+
loadedProps.add(column.propertyName);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (loadedProps.size > 0) {
|
|
70
|
+
const context = (0, sdk_1.getTenantContext)();
|
|
71
|
+
(0, utils_js_1.assertTenantId)(context.tenant_id);
|
|
72
|
+
for (const prop of loadedProps) {
|
|
73
|
+
if (loaded[prop] !== context.tenant_id) {
|
|
74
|
+
throw new Error("Stratum: save() refused, because the row belongs to another tenant.");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
40
79
|
const tenantProps = new Set(Object.keys(entity).filter((key) => key.toLowerCase() === "tenant_id"));
|
|
41
80
|
for (const column of event.metadata?.columns ?? []) {
|
|
42
81
|
if (column.databaseName.toLowerCase() === "tenant_id" && column.propertyName in entity) {
|
|
@@ -54,29 +93,144 @@ class StratumTypeOrmSubscriber {
|
|
|
54
93
|
}
|
|
55
94
|
}
|
|
56
95
|
/**
|
|
57
|
-
* Rejects an upsert that
|
|
96
|
+
* Rejects an upsert that could update another tenant's row.
|
|
58
97
|
*
|
|
59
98
|
* A conflict update must never change the tenant of an existing row. A
|
|
60
99
|
* subscriber cannot remove one column from the conflict update, so the
|
|
61
100
|
* statement fails before it runs.
|
|
62
101
|
*
|
|
102
|
+
* MySQL runs ON DUPLICATE KEY UPDATE on a conflict with any unique key of
|
|
103
|
+
* the table, whatever conflict columns were passed to upsert() or
|
|
104
|
+
* orUpdate(). The upsert is therefore allowed only when every unique key
|
|
105
|
+
* (including the primary key) of the target table contains tenant_id, so a
|
|
106
|
+
* conflict can only ever be with a row of the same tenant. The keys are read
|
|
107
|
+
* from information_schema through the same query runner.
|
|
108
|
+
*
|
|
63
109
|
* @throws Error when the ON DUPLICATE KEY UPDATE clause assigns tenant_id.
|
|
110
|
+
* @returns A promise that rejects when a unique key of the table does not
|
|
111
|
+
* include tenant_id, or when the table cannot be determined.
|
|
64
112
|
*/
|
|
65
113
|
beforeQuery(event) {
|
|
114
|
+
if (event.dataSource &&
|
|
115
|
+
!scopedDataSources.has(event.dataSource) &&
|
|
116
|
+
UPDATE_OR_DELETE.test(event.query)) {
|
|
117
|
+
throw new Error("Stratum: register the subscriber with registerStratumSubscriber(dataSource), " +
|
|
118
|
+
"which scopes updates and deletes to the current tenant.");
|
|
119
|
+
}
|
|
120
|
+
if (/^\s*TRUNCATE\b/i.test(event.query))
|
|
121
|
+
return assertNotTenantTable(event);
|
|
66
122
|
const clause = event.query.split(UPSERT_CLAUSE)[1];
|
|
67
|
-
if (clause
|
|
123
|
+
if (clause === undefined)
|
|
124
|
+
return;
|
|
125
|
+
if (TENANT_ASSIGNMENT.test(clause)) {
|
|
68
126
|
throw new Error("Stratum: an upsert must not update tenant_id on conflict. " +
|
|
69
127
|
"Remove tenant_id from the entity values or from the orUpdate() columns.");
|
|
70
128
|
}
|
|
129
|
+
return assertUniqueKeysIncludeTenant(event);
|
|
71
130
|
}
|
|
72
131
|
}
|
|
73
132
|
exports.StratumTypeOrmSubscriber = StratumTypeOrmSubscriber;
|
|
133
|
+
/**
|
|
134
|
+
* ANDs `tenant_id = <current tenant>` to the WHERE clause of an update, delete
|
|
135
|
+
* or soft-delete builder whose data source has the subscriber. Entities without
|
|
136
|
+
* a tenant_id column are left alone; a target without metadata (a table name)
|
|
137
|
+
* is treated as a tenant table.
|
|
138
|
+
*/
|
|
139
|
+
function addTenantCondition(builder) {
|
|
140
|
+
if (!builder.connection.subscribers.some((s) => s instanceof StratumTypeOrmSubscriber))
|
|
141
|
+
return;
|
|
142
|
+
const alias = builder.expressionMap.mainAlias;
|
|
143
|
+
let column = "tenant_id";
|
|
144
|
+
if (alias?.hasMetadata) {
|
|
145
|
+
const tenantColumn = alias.metadata.columns.find((c) => c.databaseName.toLowerCase() === "tenant_id");
|
|
146
|
+
if (!tenantColumn)
|
|
147
|
+
return;
|
|
148
|
+
column = tenantColumn.databaseName;
|
|
149
|
+
}
|
|
150
|
+
const context = (0, sdk_1.getTenantContext)();
|
|
151
|
+
(0, utils_js_1.assertTenantId)(context.tenant_id);
|
|
152
|
+
const qualified = builder.expressionMap.aliasNamePrefixingEnabled && alias
|
|
153
|
+
? `${builder.escape(alias.name)}.${builder.escape(column)}`
|
|
154
|
+
: builder.escape(column);
|
|
155
|
+
const condition = `${qualified} = :${TENANT_PARAMETER}`;
|
|
156
|
+
const existing = builder.expressionMap.extraAppendedAndWhereCondition;
|
|
157
|
+
// TypeORM ANDs this condition, in its own parentheses, to the caller's
|
|
158
|
+
// WHERE clause, so an orWhere() cannot widen the statement past the tenant.
|
|
159
|
+
builder.expressionMap.extraAppendedAndWhereCondition = existing
|
|
160
|
+
? `(${existing}) AND ${condition}`
|
|
161
|
+
: condition;
|
|
162
|
+
builder.setParameter(TENANT_PARAMETER, context.tenant_id);
|
|
163
|
+
}
|
|
164
|
+
/** Wraps execute() of TypeORM's update, delete and soft-delete query builder classes, once. */
|
|
165
|
+
function scopeWriteBuilders(dataSource) {
|
|
166
|
+
const builders = [
|
|
167
|
+
dataSource.createQueryBuilder().update(),
|
|
168
|
+
dataSource.createQueryBuilder().delete(),
|
|
169
|
+
dataSource.createQueryBuilder().softDelete(),
|
|
170
|
+
];
|
|
171
|
+
for (const builder of builders) {
|
|
172
|
+
const proto = Object.getPrototypeOf(builder);
|
|
173
|
+
if (proto[SCOPED_EXECUTE])
|
|
174
|
+
continue;
|
|
175
|
+
const original = proto.execute;
|
|
176
|
+
proto.execute = function () {
|
|
177
|
+
addTenantCondition(this);
|
|
178
|
+
return original.call(this);
|
|
179
|
+
};
|
|
180
|
+
proto[SCOPED_EXECUTE] = true;
|
|
181
|
+
}
|
|
182
|
+
scopedDataSources.add(dataSource);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Refuses a TRUNCATE (Repository.clear(), QueryRunner.clearTable()) of a table
|
|
186
|
+
* with a tenant_id column, because it would empty the table for every tenant.
|
|
187
|
+
* A TRUNCATE whose table cannot be checked is refused too.
|
|
188
|
+
*/
|
|
189
|
+
async function assertNotTenantTable(event) {
|
|
190
|
+
const target = TRUNCATE_TARGET.exec(event.query);
|
|
191
|
+
const unquote = (name) => (name === undefined ? null : name.replace(/``/g, "`"));
|
|
192
|
+
const table = target ? unquote(target[2]) : null;
|
|
193
|
+
let isTenantTable = true;
|
|
194
|
+
if (table && event.queryRunner) {
|
|
195
|
+
const rows = (await event.queryRunner.query(TENANT_COLUMN_SQL, [unquote(target?.[1]), table]));
|
|
196
|
+
isTenantTable = Number(rows[0]?.n ?? 1) > 0;
|
|
197
|
+
}
|
|
198
|
+
if (isTenantTable) {
|
|
199
|
+
throw new Error(`Stratum: TRUNCATE of "${table ?? "unknown table"}" is refused, because it would remove every tenant's rows. ` +
|
|
200
|
+
"Delete the current tenant's rows instead.");
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
async function assertUniqueKeysIncludeTenant(event) {
|
|
204
|
+
const target = INSERT_TARGET.exec(event.query);
|
|
205
|
+
if (!target || !event.queryRunner) {
|
|
206
|
+
throw new Error("Stratum: an upsert is refused because its table's unique keys cannot be checked for tenant_id.");
|
|
207
|
+
}
|
|
208
|
+
const unquote = (name) => (name === undefined ? null : name.replace(/``/g, "`"));
|
|
209
|
+
const table = unquote(target[2]);
|
|
210
|
+
const rows = (await event.queryRunner.query(UNIQUE_KEYS_SQL, [unquote(target[1]), table]));
|
|
211
|
+
const keys = new Map();
|
|
212
|
+
for (const row of rows) {
|
|
213
|
+
const hasTenant = row.column_name.toLowerCase() === "tenant_id";
|
|
214
|
+
keys.set(row.index_name, (keys.get(row.index_name) ?? false) || hasTenant);
|
|
215
|
+
}
|
|
216
|
+
for (const [key, hasTenant] of keys) {
|
|
217
|
+
if (!hasTenant) {
|
|
218
|
+
throw new Error(`Stratum: an upsert on "${table}" is refused, because its unique key "${key}" does not include tenant_id, ` +
|
|
219
|
+
"so a conflict could update another tenant's row. Add tenant_id to every unique key of the table, " +
|
|
220
|
+
"or look the row up by tenant before writing it.");
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
74
224
|
/**
|
|
75
225
|
* Returns the one StratumTypeOrmSubscriber on the data source, and adds it first when it is missing.
|
|
76
226
|
*
|
|
77
227
|
* A second call returns the subscriber that the first call added, so the
|
|
78
228
|
* data source never runs the subscriber twice.
|
|
79
229
|
*
|
|
230
|
+
* It also scopes TypeORM's update, delete and soft-delete query builders to the
|
|
231
|
+
* current tenant (see StratumTypeOrmSubscriber). The subscriber refuses an
|
|
232
|
+
* UPDATE or DELETE on a data source that was not registered this way.
|
|
233
|
+
*
|
|
80
234
|
* @param dataSource - An initialized TypeORM `DataSource`.
|
|
81
235
|
* @throws Error when the data source is not initialized. `initialize()` replaces
|
|
82
236
|
* the subscriber list, so an earlier registration has no effect.
|
|
@@ -86,6 +240,7 @@ function registerStratumSubscriber(dataSource) {
|
|
|
86
240
|
throw new Error("Stratum: call registerStratumSubscriber() after dataSource.initialize(). " +
|
|
87
241
|
"initialize() replaces the subscriber list.");
|
|
88
242
|
}
|
|
243
|
+
scopeWriteBuilders(dataSource);
|
|
89
244
|
const existing = dataSource.subscribers.find((subscriber) => subscriber instanceof StratumTypeOrmSubscriber);
|
|
90
245
|
if (existing)
|
|
91
246
|
return existing;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeorm-subscriber.js","sourceRoot":"","sources":["../../src/integrations/typeorm-subscriber.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"typeorm-subscriber.js","sourceRoot":"","sources":["../../src/integrations/typeorm-subscriber.ts"],"names":[],"mappings":";;;AAiUA,8DAmBC;AApVD,yCAAmD;AACnD,0CAA6C;AAuD7C,8EAA8E;AAC9E,qCAAqC;AACrC,MAAM,aAAa,GAAG,oCAAoC,CAAC;AAC3D,MAAM,iBAAiB,GAAG,kBAAkB,CAAC;AAC7C,8EAA8E;AAC9E,MAAM,aAAa,GAAG,4EAA4E,CAAC;AAEnG,MAAM,gBAAgB,GAAG,0BAA0B,CAAC;AACpD,+EAA+E;AAC/E,MAAM,eAAe,GAAG,2EAA2E,CAAC;AACpG,MAAM,iBAAiB,GACrB,uDAAuD;IACvD,sGAAsG,CAAC;AACzG,MAAM,gBAAgB,GAAG,iBAAiB,CAAC;AAC3C,MAAM,cAAc,GAAG,MAAM,CAAC,6BAA6B,CAAC,CAAC;AAC7D,6EAA6E;AAC7E,MAAM,iBAAiB,GAAG,IAAI,OAAO,EAAU,CAAC;AAEhD,MAAM,eAAe,GACnB,8DAA8D;IAC9D,qCAAqC;IACrC,oFAAoF,CAAC;AAEvF;;;;;;;;;;;;;;;GAeG;AACH,MAAa,wBAAwB;I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Y,wBAAwB,CACjD,CAAC;IACF,IAAI,QAAQ;QAAE,OAAO,QAAQ,CAAC;IAE9B,MAAM,UAAU,GAAG,IAAI,wBAAwB,EAAE,CAAC;IAClD,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACxC,OAAO,UAAU,CAAC;AACpB,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -39,8 +39,9 @@ export interface MysqlTableAdapterOptions {
|
|
|
39
39
|
databaseName: string;
|
|
40
40
|
/**
|
|
41
41
|
* Every base table name that has a per-tenant copy. Required by
|
|
42
|
+
* scopedTable, which only accepts these base names, and by
|
|
42
43
|
* purgeTenantData, which drops exactly `{base}_{slug}` for each entry and
|
|
43
|
-
* nothing else.
|
|
44
|
+
* nothing else.
|
|
44
45
|
* No entry may be another entry followed by `_`, since `{base}_{slug}` would
|
|
45
46
|
* then be ambiguous between two tenants.
|
|
46
47
|
*/
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,mBAAmB;IAClC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACzD,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3D,OAAO,IAAI,IAAI,CAAC;IAChB,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACtB;AAED,MAAM,WAAW,aAAa;IAC5B,aAAa,IAAI,OAAO,CAAC,mBAAmB,CAAC,CAAC;IAC9C,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACzD,GAAG,IAAI,OAAO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AuB;IACtC,kEAAkE;IAClE,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACpE,2BAA2B;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,mDAAmD;IACnD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,oDAAoD;IACpD,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stratum-hq/mysql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "MySQL tenant isolation for Stratum — shared table, table-per-tenant, and database-per-tenant strategies",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"multi-tenancy",
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
"clean": "rm -rf dist"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@stratum-hq/core": "^1.
|
|
42
|
-
"@stratum-hq/sdk": "^1.
|
|
41
|
+
"@stratum-hq/core": "^1.5.0",
|
|
42
|
+
"@stratum-hq/sdk": "^1.3.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"mysql2": "^3.0.0",
|