@webiny/api-core-sql 6.6.0-alpha.11 → 6.6.0-alpha.13
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/TableManager.d.ts +5 -4
- package/TableManager.js +11 -7
- package/TableManager.js.map +1 -1
- package/adminUsers/index.d.ts +3 -0
- package/adminUsers/index.js +55 -3
- package/adminUsers/index.js.map +1 -1
- package/connection/SqlConnectionFeature.d.ts +3 -1
- package/connection/SqlConnectionFeature.js +2 -0
- package/connection/SqlConnectionFeature.js.map +1 -1
- package/coreStorage/CoreSqlSchemaSetup.js +2 -1
- package/coreStorage/CoreSqlSchemaSetup.js.map +1 -1
- package/coreStorage/SqlApiCoreStorageOperationsFactory.d.ts +1 -1
- package/coreStorage/SqlApiCoreStorageOperationsFactory.js +2 -1
- package/coreStorage/SqlApiCoreStorageOperationsFactory.js.map +1 -1
- package/coreStorage/SqlTenancyStorageOperationsFactory.d.ts +19 -0
- package/coreStorage/SqlTenancyStorageOperationsFactory.js +36 -0
- package/coreStorage/SqlTenancyStorageOperationsFactory.js.map +1 -0
- package/createApiCoreSql.d.ts +8 -2
- package/createApiCoreSql.js +7 -5
- package/createApiCoreSql.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.js.map +1 -1
- package/package.json +8 -8
- package/schemaMemory/ensureTable.d.ts +10 -3
- package/schemaMemory/ensureTable.js +7 -4
- package/schemaMemory/ensureTable.js.map +1 -1
- package/security/index.d.ts +3 -0
- package/security/index.js +22 -1
- package/security/index.js.map +1 -1
package/TableManager.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Knex } from "knex";
|
|
2
|
-
import { type TableCreator } from "./schemaMemory/ensureTable.js";
|
|
2
|
+
import { type TableCreator, type TableUpgrade } from "./schemaMemory/ensureTable.js";
|
|
3
3
|
export interface TableManagerOptions {
|
|
4
4
|
/**
|
|
5
5
|
* The schema was set up before the API took traffic (see `SqlSchemaConfig`): `ensure()` then
|
|
@@ -26,11 +26,12 @@ export declare class TableManager {
|
|
|
26
26
|
reset(): void;
|
|
27
27
|
/**
|
|
28
28
|
* Declares a table and returns the function that ensures it on first use. Declared tables are
|
|
29
|
-
* created together by `setup()` (schema setup).
|
|
29
|
+
* created together by `setup()` (schema setup). `upgrade` brings a table that already existed up
|
|
30
|
+
* to date, for example with an index added after it was created.
|
|
30
31
|
*/
|
|
31
|
-
define(tableName: string, creator: TableCreator): () => Promise<void>;
|
|
32
|
+
define(tableName: string, creator: TableCreator, upgrade?: TableUpgrade): () => Promise<void>;
|
|
32
33
|
setup(): Promise<void>;
|
|
33
34
|
resolve(tableName: string): string;
|
|
34
|
-
ensure(tableName: string, creator: TableCreator): Promise<void>;
|
|
35
|
+
ensure(tableName: string, creator: TableCreator, upgrade?: TableUpgrade): Promise<void>;
|
|
35
36
|
private get memory();
|
|
36
37
|
}
|
package/TableManager.js
CHANGED
|
@@ -12,28 +12,32 @@ class TableManager {
|
|
|
12
12
|
reset() {
|
|
13
13
|
this.memory.reset();
|
|
14
14
|
}
|
|
15
|
-
define(tableName, creator) {
|
|
16
|
-
this.defined.set(tableName,
|
|
17
|
-
|
|
15
|
+
define(tableName, creator, upgrade) {
|
|
16
|
+
this.defined.set(tableName, {
|
|
17
|
+
creator,
|
|
18
|
+
upgrade
|
|
19
|
+
});
|
|
20
|
+
return ()=>this.ensure(tableName, creator, upgrade);
|
|
18
21
|
}
|
|
19
22
|
async setup() {
|
|
20
|
-
for (const [tableName, creator] of this.defined){
|
|
23
|
+
for (const [tableName, { creator, upgrade }] of this.defined){
|
|
21
24
|
const resolved = this.resolve(tableName);
|
|
22
25
|
const generation = this.memory.generation;
|
|
23
|
-
await createTableIfMissing(this.knex, resolved, creator);
|
|
26
|
+
await createTableIfMissing(this.knex, resolved, creator, upgrade);
|
|
24
27
|
if (this.memory.generation === generation) recordTable(this.knex, this.memory, resolved);
|
|
25
28
|
}
|
|
26
29
|
}
|
|
27
30
|
resolve(tableName) {
|
|
28
31
|
return `${this.prefix}${tableName}`;
|
|
29
32
|
}
|
|
30
|
-
async ensure(tableName, creator) {
|
|
33
|
+
async ensure(tableName, creator, upgrade) {
|
|
31
34
|
if (this.schemaManaged()) return;
|
|
32
35
|
await ensureTable({
|
|
33
36
|
knex: this.knex,
|
|
34
37
|
memory: this.memory,
|
|
35
38
|
tableName: this.resolve(tableName),
|
|
36
|
-
create: creator
|
|
39
|
+
create: creator,
|
|
40
|
+
upgrade
|
|
37
41
|
});
|
|
38
42
|
}
|
|
39
43
|
get memory() {
|
package/TableManager.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TableManager.js","sources":["../src/TableManager.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport { getSchemaMemory, type ISchemaMemory } from \"./schemaMemory/getSchemaMemory.js\";\nimport { getConnectionKey } from \"./schemaMemory/getConnectionKey.js\";\nimport {\n createTableIfMissing,\n ensureTable,\n recordTable,\n type TableCreator\n} from \"./schemaMemory/ensureTable.js\";\n\nexport interface TableManagerOptions {\n /**\n * The schema was set up before the API took traffic (see `SqlSchemaConfig`): `ensure()` then\n * never checks for or creates a table. `setup()` still creates them. A function when it can\n * change (a server turns it on once its schema setup at start ran): read on every `ensure()`.\n */\n schemaManaged?: boolean | (() => boolean);\n}\n\n/**\n * The tables of one SQL storage part (core, audit logs, login) on one connection. Built wherever the\n * storage is built, which is once per request: it holds no state of its own. What it learns (which\n * tables exist) is kept for the process, in one memory per connection and table prefix, so a new\n * instance doesn't check a table again and a long-running server keeps one entry per connection, not\n * one per request.\n */\nexport class TableManager {\n private readonly knex: Knex;\n private readonly prefix: string;\n private readonly schemaManaged: () => boolean;\n /* Tables declared by the storage operations using this manager, for schema setup. */\n private readonly defined = new Map<string,
|
|
1
|
+
{"version":3,"file":"TableManager.js","sources":["../src/TableManager.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport { getSchemaMemory, type ISchemaMemory } from \"./schemaMemory/getSchemaMemory.js\";\nimport { getConnectionKey } from \"./schemaMemory/getConnectionKey.js\";\nimport {\n createTableIfMissing,\n ensureTable,\n recordTable,\n type TableCreator,\n type TableUpgrade\n} from \"./schemaMemory/ensureTable.js\";\n\ninterface DefinedTable {\n creator: TableCreator;\n upgrade?: TableUpgrade;\n}\n\nexport interface TableManagerOptions {\n /**\n * The schema was set up before the API took traffic (see `SqlSchemaConfig`): `ensure()` then\n * never checks for or creates a table. `setup()` still creates them. A function when it can\n * change (a server turns it on once its schema setup at start ran): read on every `ensure()`.\n */\n schemaManaged?: boolean | (() => boolean);\n}\n\n/**\n * The tables of one SQL storage part (core, audit logs, login) on one connection. Built wherever the\n * storage is built, which is once per request: it holds no state of its own. What it learns (which\n * tables exist) is kept for the process, in one memory per connection and table prefix, so a new\n * instance doesn't check a table again and a long-running server keeps one entry per connection, not\n * one per request.\n */\nexport class TableManager {\n private readonly knex: Knex;\n private readonly prefix: string;\n private readonly schemaManaged: () => boolean;\n /* Tables declared by the storage operations using this manager, for schema setup. */\n private readonly defined = new Map<string, DefinedTable>();\n private memoryOfTables?: ISchemaMemory;\n\n constructor(knex: Knex, prefix: string = \"\", options: TableManagerOptions = {}) {\n this.knex = knex;\n this.prefix = prefix ? `${prefix}_` : \"\";\n const schemaManaged = options.schemaManaged;\n this.schemaManaged =\n typeof schemaManaged === \"function\" ? schemaManaged : () => schemaManaged === true;\n }\n\n /** Forgets which tables this connection has (test setups drop all tables between tests). */\n public reset(): void {\n this.memory.reset();\n }\n\n /**\n * Declares a table and returns the function that ensures it on first use. Declared tables are\n * created together by `setup()` (schema setup). `upgrade` brings a table that already existed up\n * to date, for example with an index added after it was created.\n */\n public define(\n tableName: string,\n creator: TableCreator,\n upgrade?: TableUpgrade\n ): () => Promise<void> {\n this.defined.set(tableName, { creator, upgrade });\n return () => this.ensure(tableName, creator, upgrade);\n }\n\n /* Creates every declared table that doesn't exist yet. Runs at schema setup. */\n public async setup(): Promise<void> {\n for (const [tableName, { creator, upgrade }] of this.defined) {\n const resolved = this.resolve(tableName);\n // Nothing checked before a reset is recorded after it (as in `ensureTable`).\n const generation = this.memory.generation;\n await createTableIfMissing(this.knex, resolved, creator, upgrade);\n if (this.memory.generation === generation) {\n recordTable(this.knex, this.memory, resolved);\n }\n }\n }\n\n public resolve(tableName: string): string {\n return `${this.prefix}${tableName}`;\n }\n\n public async ensure(\n tableName: string,\n creator: TableCreator,\n upgrade?: TableUpgrade\n ): Promise<void> {\n if (this.schemaManaged()) {\n return;\n }\n await ensureTable({\n knex: this.knex,\n memory: this.memory,\n tableName: this.resolve(tableName),\n create: creator,\n upgrade\n });\n }\n\n /* Made on first use: a schema-managed manager never needs it. */\n private get memory(): ISchemaMemory {\n this.memoryOfTables ??= getSchemaMemory(\n `core.sql.tables:${getConnectionKey(this.knex)}:${this.prefix}`\n );\n return this.memoryOfTables;\n }\n}\n"],"names":["TableManager","knex","prefix","options","Map","schemaManaged","tableName","creator","upgrade","resolved","generation","createTableIfMissing","recordTable","ensureTable","getSchemaMemory","getConnectionKey"],"mappings":";;;AAgCO,MAAMA;IAQT,YAAYC,IAAU,EAAEC,SAAiB,EAAE,EAAEC,UAA+B,CAAC,CAAC,CAAE;QAJG,KAClE,OAAO,GAAG,IAAIC;QAI3B,IAAI,CAAC,IAAI,GAAGH;QACZ,IAAI,CAAC,MAAM,GAAGC,SAAS,GAAGA,OAAO,CAAC,CAAC,GAAG;QACtC,MAAMG,gBAAgBF,QAAQ,aAAa;QAC3C,IAAI,CAAC,aAAa,GACd,AAAyB,cAAzB,OAAOE,gBAA+BA,gBAAgB,IAAMA,AAAkB,SAAlBA;IACpE;IAGO,QAAc;QACjB,IAAI,CAAC,MAAM,CAAC,KAAK;IACrB;IAOO,OACHC,SAAiB,EACjBC,OAAqB,EACrBC,OAAsB,EACH;QACnB,IAAI,CAAC,OAAO,CAAC,GAAG,CAACF,WAAW;YAAEC;YAASC;QAAQ;QAC/C,OAAO,IAAM,IAAI,CAAC,MAAM,CAACF,WAAWC,SAASC;IACjD;IAGA,MAAa,QAAuB;QAChC,KAAK,MAAM,CAACF,WAAW,EAAEC,OAAO,EAAEC,OAAO,EAAE,CAAC,IAAI,IAAI,CAAC,OAAO,CAAE;YAC1D,MAAMC,WAAW,IAAI,CAAC,OAAO,CAACH;YAE9B,MAAMI,aAAa,IAAI,CAAC,MAAM,CAAC,UAAU;YACzC,MAAMC,qBAAqB,IAAI,CAAC,IAAI,EAAEF,UAAUF,SAASC;YACzD,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,KAAKE,YAC3BE,YAAY,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,EAAEH;QAE5C;IACJ;IAEO,QAAQH,SAAiB,EAAU;QACtC,OAAO,GAAG,IAAI,CAAC,MAAM,GAAGA,WAAW;IACvC;IAEA,MAAa,OACTA,SAAiB,EACjBC,OAAqB,EACrBC,OAAsB,EACT;QACb,IAAI,IAAI,CAAC,aAAa,IAClB;QAEJ,MAAMK,YAAY;YACd,MAAM,IAAI,CAAC,IAAI;YACf,QAAQ,IAAI,CAAC,MAAM;YACnB,WAAW,IAAI,CAAC,OAAO,CAACP;YACxB,QAAQC;YACRC;QACJ;IACJ;IAGA,IAAY,SAAwB;QAChC,IAAI,CAAC,cAAc,KAAKM,gBACpB,CAAC,gBAAgB,EAAEC,iBAAiB,IAAI,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE;QAEnE,OAAO,IAAI,CAAC,cAAc;IAC9B;AACJ"}
|
package/adminUsers/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { Knex } from "knex";
|
|
2
2
|
import type { AdminUsersStorageOperations } from "@webiny/api-core/types/users.js";
|
|
3
|
+
import type { ScopeTenant } from "@webiny/api-core/features/tenancy/ScopeTenant/index.js";
|
|
3
4
|
import type { TableManager } from "../TableManager.js";
|
|
4
5
|
interface CreateStorageOperationsParams {
|
|
5
6
|
knex: Knex;
|
|
6
7
|
tableManager: TableManager;
|
|
8
|
+
/** The tenant the storage acts on: its scope's. */
|
|
9
|
+
scopeTenant: ScopeTenant.Interface;
|
|
7
10
|
}
|
|
8
11
|
export declare const createStorageOperations: (params: CreateStorageOperationsParams) => AdminUsersStorageOperations;
|
|
9
12
|
export {};
|
package/adminUsers/index.js
CHANGED
|
@@ -9,7 +9,12 @@ const userToRow = (user)=>({
|
|
|
9
9
|
});
|
|
10
10
|
const rowToUser = (row)=>JSON.parse(row.data);
|
|
11
11
|
const createStorageOperations = (params)=>{
|
|
12
|
-
const { knex, tableManager } = params;
|
|
12
|
+
const { knex, tableManager, scopeTenant } = params;
|
|
13
|
+
const inScope = (user)=>({
|
|
14
|
+
...user,
|
|
15
|
+
tenant: scopeTenant.getId()
|
|
16
|
+
});
|
|
17
|
+
const idIndexName = `${tableManager.resolve(TABLE_NAME)}_id_index`;
|
|
13
18
|
const ensureTable = tableManager.define(TABLE_NAME, (table)=>{
|
|
14
19
|
table.text("id").notNullable();
|
|
15
20
|
table.text("tenant").notNullable();
|
|
@@ -23,12 +28,22 @@ const createStorageOperations = (params)=>{
|
|
|
23
28
|
"tenant",
|
|
24
29
|
"email"
|
|
25
30
|
]);
|
|
31
|
+
table.index([
|
|
32
|
+
"id"
|
|
33
|
+
], idIndexName);
|
|
34
|
+
}, async (knex, tableName)=>{
|
|
35
|
+
await knex.raw("CREATE INDEX IF NOT EXISTS ?? ON ?? (??)", [
|
|
36
|
+
idIndexName,
|
|
37
|
+
tableName,
|
|
38
|
+
"id"
|
|
39
|
+
]);
|
|
26
40
|
});
|
|
27
41
|
const query = ()=>knex(tableManager.resolve(TABLE_NAME));
|
|
28
42
|
return {
|
|
29
43
|
async getUser (params) {
|
|
30
44
|
await ensureTable();
|
|
31
|
-
const { where: {
|
|
45
|
+
const { where: { id, email } } = params;
|
|
46
|
+
const tenant = scopeTenant.getId();
|
|
32
47
|
try {
|
|
33
48
|
if (id) {
|
|
34
49
|
const row = await query().where("tenant", tenant).andWhere("id", id).first();
|
|
@@ -47,11 +62,45 @@ const createStorageOperations = (params)=>{
|
|
|
47
62
|
});
|
|
48
63
|
}
|
|
49
64
|
},
|
|
65
|
+
async getUserAcrossTenants (params) {
|
|
66
|
+
const { id, tenants } = params;
|
|
67
|
+
if (0 === tenants.length) return [];
|
|
68
|
+
await ensureTable();
|
|
69
|
+
try {
|
|
70
|
+
const rows = await query().where("id", id).whereIn("tenant", tenants);
|
|
71
|
+
return rows.map((row)=>rowToUser(row));
|
|
72
|
+
} catch (err) {
|
|
73
|
+
throw error.from(err, {
|
|
74
|
+
message: "Could not load user across tenants.",
|
|
75
|
+
code: "GET_ADMIN_USER_ACROSS_TENANTS_ERROR",
|
|
76
|
+
data: {
|
|
77
|
+
id,
|
|
78
|
+
tenants
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
async listUserRecords (params) {
|
|
84
|
+
await ensureTable();
|
|
85
|
+
const { id } = params;
|
|
86
|
+
try {
|
|
87
|
+
const rows = await query().where("id", id);
|
|
88
|
+
return rows.map((row)=>rowToUser(row));
|
|
89
|
+
} catch (err) {
|
|
90
|
+
throw error.from(err, {
|
|
91
|
+
message: "Could not list user records.",
|
|
92
|
+
code: "LIST_ADMIN_USER_RECORDS_ERROR",
|
|
93
|
+
data: {
|
|
94
|
+
id
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
},
|
|
50
99
|
async listUsers (params) {
|
|
51
100
|
await ensureTable();
|
|
52
101
|
const { where, sort } = params;
|
|
53
102
|
try {
|
|
54
|
-
const rows = await query().where("tenant",
|
|
103
|
+
const rows = await query().where("tenant", scopeTenant.getId());
|
|
55
104
|
let items = rows.map((row)=>rowToUser(row));
|
|
56
105
|
items = sortItems(items, sort);
|
|
57
106
|
const { id_in, teams_in } = where;
|
|
@@ -68,6 +117,7 @@ const createStorageOperations = (params)=>{
|
|
|
68
117
|
}
|
|
69
118
|
},
|
|
70
119
|
async createUser ({ user }) {
|
|
120
|
+
user = inScope(user);
|
|
71
121
|
await ensureTable();
|
|
72
122
|
try {
|
|
73
123
|
const row = userToRow(user);
|
|
@@ -84,6 +134,7 @@ const createStorageOperations = (params)=>{
|
|
|
84
134
|
}
|
|
85
135
|
},
|
|
86
136
|
async updateUser ({ user }) {
|
|
137
|
+
user = inScope(user);
|
|
87
138
|
await ensureTable();
|
|
88
139
|
try {
|
|
89
140
|
const row = userToRow(user);
|
|
@@ -100,6 +151,7 @@ const createStorageOperations = (params)=>{
|
|
|
100
151
|
}
|
|
101
152
|
},
|
|
102
153
|
async deleteUser ({ user }) {
|
|
154
|
+
user = inScope(user);
|
|
103
155
|
await ensureTable();
|
|
104
156
|
try {
|
|
105
157
|
await query().where("tenant", user.tenant).andWhere("id", user.id).delete();
|
package/adminUsers/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adminUsers/index.js","sources":["../../src/adminUsers/index.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type {\n AdminUser,\n AdminUsersStorageOperations,\n StorageOperationsGetUserParams,\n StorageOperationsListUsersParams\n} from \"@webiny/api-core/types/users.js\";\nimport WebinyError from \"@webiny/error\";\nimport type { TableManager } from \"~/TableManager.js\";\nimport { sortItems } from \"~/sortItems.js\";\n\nconst TABLE_NAME = \"webiny_core_admin_users\";\n\ninterface IAdminUserRow {\n id: string;\n tenant: string;\n email: string;\n data: string;\n}\n\nconst userToRow = (user: AdminUser): IAdminUserRow => {\n return {\n id: user.id,\n tenant: user.tenant,\n email: user.email,\n data: JSON.stringify(user)\n };\n};\n\nconst rowToUser = <TUser extends AdminUser = AdminUser>(row: IAdminUserRow): TUser => {\n return JSON.parse(row.data) as TUser;\n};\n\ninterface CreateStorageOperationsParams {\n knex: Knex;\n tableManager: TableManager;\n}\n\nexport const createStorageOperations = (\n params: CreateStorageOperationsParams\n): AdminUsersStorageOperations => {\n const { knex, tableManager } = params;\n\n const ensureTable = tableManager.define(TABLE_NAME, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"email\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"email\"]);\n });\n\n const query = () => {\n return knex<IAdminUserRow>(tableManager.resolve(TABLE_NAME));\n };\n\n return {\n async getUser<TUser extends AdminUser = AdminUser>(\n params: StorageOperationsGetUserParams\n ): Promise<TUser | null> {\n await ensureTable();\n\n const {\n where: { tenant, id, email }\n } = params;\n\n try {\n if (id) {\n const row = await query().where(\"tenant\", tenant).andWhere(\"id\", id).first();\n\n return row ? rowToUser<TUser>(row) : null;\n }\n\n const row = await query()\n .where(\"tenant\", tenant)\n .andWhere(\"email\", email as string)\n .first();\n\n return row ? rowToUser<TUser>(row) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load user.\",\n code: \"GET_ADMIN_USERS_ERROR\",\n data: { id, email }\n });\n }\n },\n\n async listUsers<TUser extends AdminUser = AdminUser>(\n params: StorageOperationsListUsersParams\n ): Promise<TUser[]> {\n await ensureTable();\n\n const { where, sort } = params;\n\n try {\n const rows = await query().where(\"tenant\", where.tenant);\n\n let items = rows.map(row => rowToUser<TUser>(row));\n\n items = sortItems(items, sort);\n\n const { id_in, teams_in } = where;\n\n return items.filter(item => {\n if (Array.isArray(id_in) && !id_in.includes(item.id)) {\n return false;\n }\n if (Array.isArray(teams_in)) {\n return (item.teams || []).some(team => teams_in.includes(team));\n }\n return true;\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list users.\",\n code: \"LIST_ADMIN_USERS_ERROR\"\n });\n }\n },\n\n async createUser<TUser extends AdminUser = AdminUser>({\n user\n }: {\n user: TUser;\n }): Promise<TUser> {\n await ensureTable();\n\n try {\n const row = userToRow(user);\n await query().insert(row);\n\n return user;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create admin user.\",\n code: \"CREATE_ADMIN_USER_ERROR\",\n data: { user }\n });\n }\n },\n\n async updateUser<TUser extends AdminUser = AdminUser>({\n user\n }: {\n user: TUser;\n }): Promise<TUser> {\n await ensureTable();\n\n try {\n const row = userToRow(user);\n await query().where(\"tenant\", user.tenant).andWhere(\"id\", user.id).update(row);\n\n return user;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update user.\",\n code: \"UPDATE_ADMIN_USER_ERROR\",\n data: { user }\n });\n }\n },\n\n async deleteUser<TUser extends AdminUser = AdminUser>({\n user\n }: {\n user: TUser;\n }): Promise<void> {\n await ensureTable();\n\n try {\n await query().where(\"tenant\", user.tenant).andWhere(\"id\", user.id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete user.\",\n code: \"DELETE_ADMIN_USER_ERROR\",\n data: { user }\n });\n }\n }\n };\n};\n"],"names":["TABLE_NAME","userToRow","user","JSON","rowToUser","row","createStorageOperations","params","knex","tableManager","ensureTable","table","query","tenant","id","email","err","WebinyError","where","sort","rows","items","sortItems","id_in","teams_in","item","Array","team"],"mappings":";;AAWA,MAAMA,aAAa;AASnB,MAAMC,YAAY,CAACC,OACR;QACH,IAAIA,KAAK,EAAE;QACX,QAAQA,KAAK,MAAM;QACnB,OAAOA,KAAK,KAAK;QACjB,MAAMC,KAAK,SAAS,CAACD;IACzB;AAGJ,MAAME,YAAY,CAAsCC,MAC7CF,KAAK,KAAK,CAACE,IAAI,IAAI;AAQvB,MAAMC,0BAA0B,CACnCC;IAEA,MAAM,EAAEC,IAAI,EAAEC,YAAY,EAAE,GAAGF;IAE/B,MAAMG,cAAcD,aAAa,MAAM,CAACT,YAAYW,CAAAA;QAChDA,MAAM,IAAI,CAAC,MAAM,WAAW;QAC5BA,MAAM,IAAI,CAAC,UAAU,WAAW;QAChCA,MAAM,IAAI,CAAC,SAAS,WAAW;QAC/BA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAE9BA,MAAM,OAAO,CAAC;YAAC;YAAU;SAAK;QAC9BA,MAAM,MAAM,CAAC;YAAC;YAAU;SAAQ;IACpC;IA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X;YACJ,EAAE,OAAOX,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;gBACV;YACJ;QACJ;QAEA,MAAM,YAAgD,EAClDd,IAAI,EAGP;YACG,MAAMQ;YAEN,IAAI;gBACA,MAAML,MAAMJ,UAAUC;gBACtB,MAAMU,QAAQ,MAAM,CAACP;gBAErB,OAAOH;YACX,EAAE,OAAOc,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAEd;oBAAK;gBACjB;YACJ;QACJ;QAEA,MAAM,YAAgD,EAClDA,IAAI,EAGP;YACG,MAAMQ;YAEN,IAAI;gBACA,MAAML,MAAMJ,UAAUC;gBACtB,MAAMU,QAAQ,KAAK,CAAC,UAAUV,KAAK,MAAM,EAAE,QAAQ,CAAC,MAAMA,KAAK,EAAE,EAAE,MAAM,CAACG;gBAE1E,OAAOH;YACX,EAAE,OAAOc,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAEd;oBAAK;gBACjB;YACJ;QACJ;QAEA,MAAM,YAAgD,EAClDA,IAAI,EAGP;YACG,MAAMQ;YAEN,IAAI;gBACA,MAAME,QAAQ,KAAK,CAAC,UAAUV,KAAK,MAAM,EAAE,QAAQ,CAAC,MAAMA,KAAK,EAAE,EAAE,MAAM;YAC7E,EAAE,OAAOc,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAEd;oBAAK;gBACjB;YACJ;QACJ;IACJ;AACJ"}
|
|
1
|
+
{"version":3,"file":"adminUsers/index.js","sources":["../../src/adminUsers/index.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type {\n AdminUser,\n AdminUsersStorageOperations,\n StorageOperationsGetUserAcrossTenantsParams,\n StorageOperationsGetUserParams,\n StorageOperationsListUserRecordsParams,\n StorageOperationsListUsersParams\n} from \"@webiny/api-core/types/users.js\";\nimport WebinyError from \"@webiny/error\";\nimport type { ScopeTenant } from \"@webiny/api-core/features/tenancy/ScopeTenant/index.js\";\nimport type { TableManager } from \"~/TableManager.js\";\nimport { sortItems } from \"~/sortItems.js\";\n\nconst TABLE_NAME = \"webiny_core_admin_users\";\n\ninterface IAdminUserRow {\n id: string;\n tenant: string;\n email: string;\n data: string;\n}\n\nconst userToRow = (user: AdminUser): IAdminUserRow => {\n return {\n id: user.id,\n tenant: user.tenant,\n email: user.email,\n data: JSON.stringify(user)\n };\n};\n\nconst rowToUser = <TUser extends AdminUser = AdminUser>(row: IAdminUserRow): TUser => {\n return JSON.parse(row.data) as TUser;\n};\n\ninterface CreateStorageOperationsParams {\n knex: Knex;\n tableManager: TableManager;\n /** The tenant the storage acts on: its scope's. */\n scopeTenant: ScopeTenant.Interface;\n}\n\nexport const createStorageOperations = (\n params: CreateStorageOperationsParams\n): AdminUsersStorageOperations => {\n const { knex, tableManager, scopeTenant } = params;\n\n /** The user with its scope's tenant, whatever tenant it carries. */\n const inScope = <T extends AdminUser>(user: T): T => {\n return { ...user, tenant: scopeTenant.getId() };\n };\n\n // The user→tenant index: finds every tenant's record of one user.\n const idIndexName = `${tableManager.resolve(TABLE_NAME)}_id_index`;\n\n const ensureTable = tableManager.define(\n TABLE_NAME,\n table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"email\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"email\"]);\n table.index([\"id\"], idIndexName);\n },\n async (knex, tableName) => {\n // Tables created before the index existed.\n await knex.raw(\"CREATE INDEX IF NOT EXISTS ?? ON ?? (??)\", [\n idIndexName,\n tableName,\n \"id\"\n ]);\n }\n );\n\n const query = () => {\n return knex<IAdminUserRow>(tableManager.resolve(TABLE_NAME));\n };\n\n return {\n async getUser<TUser extends AdminUser = AdminUser>(\n params: StorageOperationsGetUserParams\n ): Promise<TUser | null> {\n await ensureTable();\n\n const {\n where: { id, email }\n } = params;\n const tenant = scopeTenant.getId();\n\n try {\n if (id) {\n const row = await query().where(\"tenant\", tenant).andWhere(\"id\", id).first();\n\n return row ? rowToUser<TUser>(row) : null;\n }\n\n const row = await query()\n .where(\"tenant\", tenant)\n .andWhere(\"email\", email as string)\n .first();\n\n return row ? rowToUser<TUser>(row) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load user.\",\n code: \"GET_ADMIN_USERS_ERROR\",\n data: { id, email }\n });\n }\n },\n\n // Cross-tenant by design: one user's records in the given tenants, not the scope's tenant.\n async getUserAcrossTenants<TUser extends AdminUser = AdminUser>(\n params: StorageOperationsGetUserAcrossTenantsParams\n ): Promise<TUser[]> {\n const { id, tenants } = params;\n if (tenants.length === 0) {\n return [];\n }\n\n await ensureTable();\n\n try {\n const rows = await query().where(\"id\", id).whereIn(\"tenant\", tenants);\n\n return rows.map(row => rowToUser<TUser>(row));\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load user across tenants.\",\n code: \"GET_ADMIN_USER_ACROSS_TENANTS_ERROR\",\n data: { id, tenants }\n });\n }\n },\n\n // Cross-tenant by design: every tenant's record of one user.\n async listUserRecords<TUser extends AdminUser = AdminUser>(\n params: StorageOperationsListUserRecordsParams\n ): Promise<TUser[]> {\n await ensureTable();\n\n const { id } = params;\n\n try {\n const rows = await query().where(\"id\", id);\n\n return rows.map(row => rowToUser<TUser>(row));\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list user records.\",\n code: \"LIST_ADMIN_USER_RECORDS_ERROR\",\n data: { id }\n });\n }\n },\n\n async listUsers<TUser extends AdminUser = AdminUser>(\n params: StorageOperationsListUsersParams\n ): Promise<TUser[]> {\n await ensureTable();\n\n const { where, sort } = params;\n\n try {\n const rows = await query().where(\"tenant\", scopeTenant.getId());\n\n let items = rows.map(row => rowToUser<TUser>(row));\n\n items = sortItems(items, sort);\n\n const { id_in, teams_in } = where;\n\n return items.filter(item => {\n if (Array.isArray(id_in) && !id_in.includes(item.id)) {\n return false;\n }\n if (Array.isArray(teams_in)) {\n return (item.teams || []).some(team => teams_in.includes(team));\n }\n return true;\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list users.\",\n code: \"LIST_ADMIN_USERS_ERROR\"\n });\n }\n },\n\n async createUser<TUser extends AdminUser = AdminUser>({\n user\n }: {\n user: TUser;\n }): Promise<TUser> {\n user = inScope(user);\n await ensureTable();\n\n try {\n const row = userToRow(user);\n await query().insert(row);\n\n return user;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create admin user.\",\n code: \"CREATE_ADMIN_USER_ERROR\",\n data: { user }\n });\n }\n },\n\n async updateUser<TUser extends AdminUser = AdminUser>({\n user\n }: {\n user: TUser;\n }): Promise<TUser> {\n user = inScope(user);\n await ensureTable();\n\n try {\n const row = userToRow(user);\n await query().where(\"tenant\", user.tenant).andWhere(\"id\", user.id).update(row);\n\n return user;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update user.\",\n code: \"UPDATE_ADMIN_USER_ERROR\",\n data: { user }\n });\n }\n },\n\n async deleteUser<TUser extends AdminUser = AdminUser>({\n user\n }: {\n user: TUser;\n }): Promise<void> {\n user = inScope(user);\n await ensureTable();\n\n try {\n await query().where(\"tenant\", user.tenant).andWhere(\"id\", user.id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete user.\",\n code: \"DELETE_ADMIN_USER_ERROR\",\n data: { user }\n });\n }\n }\n };\n};\n"],"names":["TABLE_NAME","userToRow","user","JSON","rowToUser","row","createStorageOperations","params","knex","tableManager","scopeTenant","inScope","idIndexName","ensureTable","table","tableName","query","id","email","tenant","err","WebinyError","tenants","rows","where","sort","items","sortItems","id_in","teams_in","item","Array","team"],"mappings":";;AAcA,MAAMA,aAAa;AASnB,MAAMC,YAAY,CAACC,OACR;QACH,IAAIA,KAAK,EA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EAGP;YACGA,OAAOS,QAAQT;YACf,MAAMW;YAEN,IAAI;gBACA,MAAMG,QAAQ,KAAK,CAAC,UAAUd,KAAK,MAAM,EAAE,QAAQ,CAAC,MAAMA,KAAK,EAAE,EAAE,MAAM;YAC7E,EAAE,OAAOkB,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAElB;oBAAK;gBACjB;YACJ;QACJ;IACJ;AACJ"}
|
|
@@ -15,7 +15,9 @@ export interface SqlConnectionConfig {
|
|
|
15
15
|
* storage part registered per request, and every module on the storage (the Postgres CMS, the folder
|
|
16
16
|
* catalog), runs on these.
|
|
17
17
|
*
|
|
18
|
-
* It holds no feature's storage: those register
|
|
18
|
+
* It holds no feature's storage: those register in each tenant scope (`RequestStorage`). The one
|
|
19
|
+
* exception is the tenant registry's (`TenancyStorageOperationsFactory`): the request container
|
|
20
|
+
* reads the request's tenant from it before any tenant scope exists.
|
|
19
21
|
*/
|
|
20
22
|
export declare const SqlConnectionFeature: {
|
|
21
23
|
name: string;
|
|
@@ -4,6 +4,7 @@ import { KnexClientFeature } from "../feature/KnexClient/index.js";
|
|
|
4
4
|
import { SqlSchemaConfig } from "../schemaConfig/index.js";
|
|
5
5
|
import { SqlServiceManifestLoader } from "../serviceDiscovery/index.js";
|
|
6
6
|
import { TableManager } from "../TableManager.js";
|
|
7
|
+
import { SqlTenancyStorageOperationsFactory } from "../coreStorage/SqlTenancyStorageOperationsFactory.js";
|
|
7
8
|
const isManaged = (schemaManaged)=>"function" == typeof schemaManaged ? schemaManaged() : true === schemaManaged;
|
|
8
9
|
const SqlConnectionFeature = createFeature({
|
|
9
10
|
name: "SqlConnection",
|
|
@@ -13,6 +14,7 @@ const SqlConnectionFeature = createFeature({
|
|
|
13
14
|
isManaged: ()=>isManaged(schemaManaged),
|
|
14
15
|
tableNamePrefix
|
|
15
16
|
});
|
|
17
|
+
container.register(SqlTenancyStorageOperationsFactory);
|
|
16
18
|
}
|
|
17
19
|
});
|
|
18
20
|
const setSqlServiceManifestLoader = (config)=>{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection/SqlConnectionFeature.js","sources":["../../src/connection/SqlConnectionFeature.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport { createFeature } from \"@webiny/feature/api\";\nimport { ServiceDiscovery } from \"@webiny/api-core/features/serviceDiscovery/index.js\";\nimport { KnexClientFeature } from \"~/feature/KnexClient/index.js\";\nimport { SqlSchemaConfig } from \"~/schemaConfig/index.js\";\nimport { SqlServiceManifestLoader } from \"~/serviceDiscovery/index.js\";\nimport { TableManager } from \"~/TableManager.js\";\n\nexport interface SqlConnectionConfig {\n knex: Knex;\n tableNamePrefix?: string;\n /**\n * The schema was set up before the API took traffic (the storage's schema setup ran), so no\n * storage code checks for or creates tables at runtime. Off by default: tables are created on\n * first use. A function when it can change: a server turns it on once its setup at start ran.\n */\n schemaManaged?: boolean | (() => boolean);\n}\n\nconst isManaged = (schemaManaged: SqlConnectionConfig[\"schemaManaged\"]): boolean => {\n return typeof schemaManaged === \"function\" ? schemaManaged() : schemaManaged === true;\n};\n\n/**\n * What a SQL storage puts in the root container, once per process: the connection (`KnexClient`)\n * and its settings, the table prefix and the schema-managed setting (`SqlSchemaConfig`). Every\n * storage part registered per request, and every module on the storage (the Postgres CMS, the folder\n * catalog), runs on these.\n *\n * It holds no feature's storage: those register
|
|
1
|
+
{"version":3,"file":"connection/SqlConnectionFeature.js","sources":["../../src/connection/SqlConnectionFeature.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport { createFeature } from \"@webiny/feature/api\";\nimport { ServiceDiscovery } from \"@webiny/api-core/features/serviceDiscovery/index.js\";\nimport { KnexClientFeature } from \"~/feature/KnexClient/index.js\";\nimport { SqlSchemaConfig } from \"~/schemaConfig/index.js\";\nimport { SqlServiceManifestLoader } from \"~/serviceDiscovery/index.js\";\nimport { TableManager } from \"~/TableManager.js\";\nimport { SqlTenancyStorageOperationsFactory } from \"~/coreStorage/SqlTenancyStorageOperationsFactory.js\";\n\nexport interface SqlConnectionConfig {\n knex: Knex;\n tableNamePrefix?: string;\n /**\n * The schema was set up before the API took traffic (the storage's schema setup ran), so no\n * storage code checks for or creates tables at runtime. Off by default: tables are created on\n * first use. A function when it can change: a server turns it on once its setup at start ran.\n */\n schemaManaged?: boolean | (() => boolean);\n}\n\nconst isManaged = (schemaManaged: SqlConnectionConfig[\"schemaManaged\"]): boolean => {\n return typeof schemaManaged === \"function\" ? schemaManaged() : schemaManaged === true;\n};\n\n/**\n * What a SQL storage puts in the root container, once per process: the connection (`KnexClient`)\n * and its settings, the table prefix and the schema-managed setting (`SqlSchemaConfig`). Every\n * storage part registered per request, and every module on the storage (the Postgres CMS, the folder\n * catalog), runs on these.\n *\n * It holds no feature's storage: those register in each tenant scope (`RequestStorage`). The one\n * exception is the tenant registry's (`TenancyStorageOperationsFactory`): the request container\n * reads the request's tenant from it before any tenant scope exists.\n */\nexport const SqlConnectionFeature = createFeature<SqlConnectionConfig>({\n name: \"SqlConnection\",\n register(container, { knex, tableNamePrefix, schemaManaged = false }) {\n KnexClientFeature.register(container, knex);\n container.registerInstance(SqlSchemaConfig, {\n isManaged: () => isManaged(schemaManaged),\n tableNamePrefix\n });\n container.register(SqlTenancyStorageOperationsFactory);\n }\n});\n\n/**\n * Points service discovery at the manifests table of a connection. The loader is a static global of\n * the process, not a DI registration, so a storage sets it once, when it registers in the root.\n */\nexport const setSqlServiceManifestLoader = (config: SqlConnectionConfig): void => {\n const tableManager = new TableManager(config.knex, config.tableNamePrefix, {\n schemaManaged: () => isManaged(config.schemaManaged)\n });\n ServiceDiscovery.setLoader(new SqlServiceManifestLoader(config.knex, tableManager));\n};\n"],"names":["isManaged","schemaManaged","SqlConnectionFeature","createFeature","container","knex","tableNamePrefix","KnexClientFeature","SqlSchemaConfig","SqlTenancyStorageOperationsFactory","setSqlServiceManifestLoader","config","tableManager","TableManager","ServiceDiscovery","SqlServiceManifestLoader"],"mappings":";;;;;;;AAoBA,MAAMA,YAAY,CAACC,gBACR,AAAyB,cAAzB,OAAOA,gBAA+BA,kBAAkBA,AAAkB,SAAlBA;AAa5D,MAAMC,uBAAuBC,cAAmC;IACnE,MAAM;IACN,UAASC,SAAS,EAAE,EAAEC,IAAI,EAAEC,eAAe,EAAEL,gBAAgB,KAAK,EAAE;QAChEM,kBAAkB,QAAQ,CAACH,WAAWC;QACtCD,UAAU,gBAAgB,CAACI,iBAAiB;YACxC,WAAW,IAAMR,UAAUC;YAC3BK;QACJ;QACAF,UAAU,QAAQ,CAACK;IACvB;AACJ;AAMO,MAAMC,8BAA8B,CAACC;IACxC,MAAMC,eAAe,IAAIC,aAAaF,OAAO,IAAI,EAAEA,OAAO,eAAe,EAAE;QACvE,eAAe,IAAMX,UAAUW,OAAO,aAAa;IACvD;IACAG,iBAAiB,SAAS,CAAC,IAAIC,yBAAyBJ,OAAO,IAAI,EAAEC;AACzE"}
|
|
@@ -4,6 +4,7 @@ import { SqlSchemaConfig } from "../schemaConfig/index.js";
|
|
|
4
4
|
import { SqlSchemaSetup } from "../schemaSetup/index.js";
|
|
5
5
|
import { TableManager } from "../TableManager.js";
|
|
6
6
|
import { defineServiceManifestsTable } from "../serviceDiscovery/serviceManifestsTable.js";
|
|
7
|
+
import { tenantNeutralScopeTenant } from "@webiny/api-core/features/tenancy/ScopeTenant/index.js";
|
|
7
8
|
class CoreSqlSchemaSetupImpl {
|
|
8
9
|
constructor(knex, schemaConfig){
|
|
9
10
|
this.knex = knex;
|
|
@@ -13,7 +14,7 @@ class CoreSqlSchemaSetupImpl {
|
|
|
13
14
|
async setup() {
|
|
14
15
|
const knex = this.knex.client;
|
|
15
16
|
const tableManager = new TableManager(knex, this.schemaConfig?.tableNamePrefix);
|
|
16
|
-
createApiCoreSqlOperations(knex, tableManager);
|
|
17
|
+
createApiCoreSqlOperations(knex, tableManager, tenantNeutralScopeTenant);
|
|
17
18
|
defineServiceManifestsTable(tableManager);
|
|
18
19
|
await tableManager.setup();
|
|
19
20
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coreStorage/CoreSqlSchemaSetup.js","sources":["../../src/coreStorage/CoreSqlSchemaSetup.ts"],"sourcesContent":["import { createApiCoreSqlOperations } from \"~/createApiCoreSql.js\";\nimport { KnexClient } from \"~/feature/KnexClient/index.js\";\nimport { SqlSchemaConfig } from \"~/schemaConfig/index.js\";\nimport { SqlSchemaSetup } from \"~/schemaSetup/index.js\";\nimport { TableManager } from \"~/TableManager.js\";\nimport { defineServiceManifestsTable } from \"~/serviceDiscovery/serviceManifestsTable.js\";\n\n/**\n * The `core` schema setup step: the tables of users, tenancy, security, the key-value store and the\n * service manifests, on the container's connection.\n */\nclass CoreSqlSchemaSetupImpl implements SqlSchemaSetup.Interface {\n public readonly name = \"core\";\n\n public constructor(\n private readonly knex: KnexClient.Interface,\n private readonly schemaConfig?: SqlSchemaConfig.Interface\n ) {}\n\n public async setup(): Promise<void> {\n const knex = this.knex.client;\n const tableManager = new TableManager(knex, this.schemaConfig?.tableNamePrefix);\n // Building them declares their tables on the manager.\n createApiCoreSqlOperations(knex, tableManager);\n defineServiceManifestsTable(tableManager);\n await tableManager.setup();\n }\n}\n\nexport const CoreSqlSchemaSetup = SqlSchemaSetup.createImplementation({\n implementation: CoreSqlSchemaSetupImpl,\n dependencies: [KnexClient, [SqlSchemaConfig, { optional: true }]]\n});\n"],"names":["CoreSqlSchemaSetupImpl","knex","schemaConfig","tableManager","TableManager","createApiCoreSqlOperations","defineServiceManifestsTable","CoreSqlSchemaSetup","SqlSchemaSetup","KnexClient","SqlSchemaConfig"],"mappings":"
|
|
1
|
+
{"version":3,"file":"coreStorage/CoreSqlSchemaSetup.js","sources":["../../src/coreStorage/CoreSqlSchemaSetup.ts"],"sourcesContent":["import { createApiCoreSqlOperations } from \"~/createApiCoreSql.js\";\nimport { KnexClient } from \"~/feature/KnexClient/index.js\";\nimport { SqlSchemaConfig } from \"~/schemaConfig/index.js\";\nimport { SqlSchemaSetup } from \"~/schemaSetup/index.js\";\nimport { TableManager } from \"~/TableManager.js\";\nimport { defineServiceManifestsTable } from \"~/serviceDiscovery/serviceManifestsTable.js\";\nimport { tenantNeutralScopeTenant } from \"@webiny/api-core/features/tenancy/ScopeTenant/index.js\";\n\n/**\n * The `core` schema setup step: the tables of users, tenancy, security, the key-value store and the\n * service manifests, on the container's connection.\n */\nclass CoreSqlSchemaSetupImpl implements SqlSchemaSetup.Interface {\n public readonly name = \"core\";\n\n public constructor(\n private readonly knex: KnexClient.Interface,\n private readonly schemaConfig?: SqlSchemaConfig.Interface\n ) {}\n\n public async setup(): Promise<void> {\n const knex = this.knex.client;\n const tableManager = new TableManager(knex, this.schemaConfig?.tableNamePrefix);\n // Building them declares their tables on the manager. The setup runs in no tenant.\n createApiCoreSqlOperations(knex, tableManager, tenantNeutralScopeTenant);\n defineServiceManifestsTable(tableManager);\n await tableManager.setup();\n }\n}\n\nexport const CoreSqlSchemaSetup = SqlSchemaSetup.createImplementation({\n implementation: CoreSqlSchemaSetupImpl,\n dependencies: [KnexClient, [SqlSchemaConfig, { optional: true }]]\n});\n"],"names":["CoreSqlSchemaSetupImpl","knex","schemaConfig","tableManager","TableManager","createApiCoreSqlOperations","tenantNeutralScopeTenant","defineServiceManifestsTable","CoreSqlSchemaSetup","SqlSchemaSetup","KnexClient","SqlSchemaConfig"],"mappings":";;;;;;;AAYA,MAAMA;IAGF,YACqBC,IAA0B,EAC1BC,YAAwC,CAC3D;aAFmBD,IAAI,GAAJA;aACAC,YAAY,GAAZA;aAJL,IAAI,GAAG;IAKpB;IAEH,MAAa,QAAuB;QAChC,MAAMD,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM;QAC7B,MAAME,eAAe,IAAIC,aAAaH,MAAM,IAAI,CAAC,YAAY,EAAE;QAE/DI,2BAA2BJ,MAAME,cAAcG;QAC/CC,4BAA4BJ;QAC5B,MAAMA,aAAa,KAAK;IAC5B;AACJ;AAEO,MAAMK,qBAAqBC,eAAe,oBAAoB,CAAC;IAClE,gBAAgBT;IAChB,cAAc;QAACU;QAAY;YAACC;YAAiB;gBAAE,UAAU;YAAK;SAAE;KAAC;AACrE"}
|
|
@@ -7,7 +7,7 @@ declare class SqlApiCoreStorageOperationsFactoryImpl implements ApiCoreStorageOp
|
|
|
7
7
|
private readonly knex;
|
|
8
8
|
private readonly schemaConfig?;
|
|
9
9
|
constructor(knex: KnexClient.Interface, schemaConfig?: SqlSchemaConfig.Interface | undefined);
|
|
10
|
-
create(): ApiCoreStorageOperations;
|
|
10
|
+
create({ scopeTenant }: ApiCoreStorageOperationsFactory.Params): ApiCoreStorageOperations;
|
|
11
11
|
}
|
|
12
12
|
export declare const SqlApiCoreStorageOperationsFactory: typeof SqlApiCoreStorageOperationsFactoryImpl & {
|
|
13
13
|
__abstraction: import("@webiny/di").Abstraction<import("@webiny/api-core/features/storageOperations/abstractions").IApiCoreStorageOperationsFactory>;
|
|
@@ -7,8 +7,9 @@ class SqlApiCoreStorageOperationsFactoryImpl {
|
|
|
7
7
|
this.knex = knex;
|
|
8
8
|
this.schemaConfig = schemaConfig;
|
|
9
9
|
}
|
|
10
|
-
create() {
|
|
10
|
+
create({ scopeTenant }) {
|
|
11
11
|
return createApiCoreSql({
|
|
12
|
+
scopeTenant,
|
|
12
13
|
knex: this.knex.client,
|
|
13
14
|
tableNamePrefix: this.schemaConfig?.tableNamePrefix,
|
|
14
15
|
schemaManaged: this.schemaConfig?.isManaged() === true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"coreStorage/SqlApiCoreStorageOperationsFactory.js","sources":["../../src/coreStorage/SqlApiCoreStorageOperationsFactory.ts"],"sourcesContent":["import { ApiCoreStorageOperationsFactory } from \"@webiny/api-core\";\nimport type { ApiCoreStorageOperations } from \"@webiny/api-core/types/core.js\";\nimport { createApiCoreSql } from \"~/createApiCoreSql.js\";\nimport { KnexClient } from \"~/feature/KnexClient/index.js\";\nimport { SqlSchemaConfig } from \"~/schemaConfig/index.js\";\n\n/** Builds the core storage operations on the container's connection and table prefix. */\nclass SqlApiCoreStorageOperationsFactoryImpl implements ApiCoreStorageOperationsFactory.Interface {\n public constructor(\n private readonly knex: KnexClient.Interface,\n private readonly schemaConfig?: SqlSchemaConfig.Interface\n ) {}\n\n public create(): ApiCoreStorageOperations {\n return createApiCoreSql({\n knex: this.knex.client,\n tableNamePrefix: this.schemaConfig?.tableNamePrefix,\n schemaManaged: this.schemaConfig?.isManaged() === true\n });\n }\n}\n\nexport const SqlApiCoreStorageOperationsFactory =\n ApiCoreStorageOperationsFactory.createImplementation({\n implementation: SqlApiCoreStorageOperationsFactoryImpl,\n dependencies: [KnexClient, [SqlSchemaConfig, { optional: true }]]\n });\n"],"names":["SqlApiCoreStorageOperationsFactoryImpl","knex","schemaConfig","createApiCoreSql","SqlApiCoreStorageOperationsFactory","ApiCoreStorageOperationsFactory","KnexClient","SqlSchemaConfig"],"mappings":";;;;AAOA,MAAMA;IACF,YACqBC,IAA0B,EAC1BC,YAAwC,CAC3D;aAFmBD,IAAI,GAAJA;aACAC,YAAY,GAAZA;IAClB;IAEI,
|
|
1
|
+
{"version":3,"file":"coreStorage/SqlApiCoreStorageOperationsFactory.js","sources":["../../src/coreStorage/SqlApiCoreStorageOperationsFactory.ts"],"sourcesContent":["import { ApiCoreStorageOperationsFactory } from \"@webiny/api-core\";\nimport type { ApiCoreStorageOperations } from \"@webiny/api-core/types/core.js\";\nimport { createApiCoreSql } from \"~/createApiCoreSql.js\";\nimport { KnexClient } from \"~/feature/KnexClient/index.js\";\nimport { SqlSchemaConfig } from \"~/schemaConfig/index.js\";\n\n/** Builds the core storage operations on the container's connection and table prefix. */\nclass SqlApiCoreStorageOperationsFactoryImpl implements ApiCoreStorageOperationsFactory.Interface {\n public constructor(\n private readonly knex: KnexClient.Interface,\n private readonly schemaConfig?: SqlSchemaConfig.Interface\n ) {}\n\n public create({\n scopeTenant\n }: ApiCoreStorageOperationsFactory.Params): ApiCoreStorageOperations {\n return createApiCoreSql({\n scopeTenant,\n knex: this.knex.client,\n tableNamePrefix: this.schemaConfig?.tableNamePrefix,\n schemaManaged: this.schemaConfig?.isManaged() === true\n });\n }\n}\n\nexport const SqlApiCoreStorageOperationsFactory =\n ApiCoreStorageOperationsFactory.createImplementation({\n implementation: SqlApiCoreStorageOperationsFactoryImpl,\n dependencies: [KnexClient, [SqlSchemaConfig, { optional: true }]]\n });\n"],"names":["SqlApiCoreStorageOperationsFactoryImpl","knex","schemaConfig","scopeTenant","createApiCoreSql","SqlApiCoreStorageOperationsFactory","ApiCoreStorageOperationsFactory","KnexClient","SqlSchemaConfig"],"mappings":";;;;AAOA,MAAMA;IACF,YACqBC,IAA0B,EAC1BC,YAAwC,CAC3D;aAFmBD,IAAI,GAAJA;aACAC,YAAY,GAAZA;IAClB;IAEI,OAAO,EACVC,WAAW,EAC0B,EAA4B;QACjE,OAAOC,iBAAiB;YACpBD;YACA,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM;YACtB,iBAAiB,IAAI,CAAC,YAAY,EAAE;YACpC,eAAe,IAAI,CAAC,YAAY,EAAE,gBAAgB;QACtD;IACJ;AACJ;AAEO,MAAME,qCACTC,gCAAgC,oBAAoB,CAAC;IACjD,gBAAgBN;IAChB,cAAc;QAACO;QAAY;YAACC;YAAiB;gBAAE,UAAU;YAAK;SAAE;KAAC;AACrE"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TenancyStorageOperationsFactory } from "@webiny/api-core/features/storageOperations/abstractions.js";
|
|
2
|
+
import type { TenancyStorageOperations } from "@webiny/api-core/types/tenancy.js";
|
|
3
|
+
import { KnexClient } from "../feature/KnexClient/index.js";
|
|
4
|
+
import { SqlSchemaConfig } from "../schemaConfig/index.js";
|
|
5
|
+
/**
|
|
6
|
+
* The tenant registry's storage on the container's connection: the tenants table only. The request
|
|
7
|
+
* container reads the request's tenant from it, before any tenant scope (with the rest of the core
|
|
8
|
+
* storage) exists.
|
|
9
|
+
*/
|
|
10
|
+
declare class SqlTenancyStorageOperationsFactoryImpl implements TenancyStorageOperationsFactory.Interface {
|
|
11
|
+
private readonly knex;
|
|
12
|
+
private readonly schemaConfig?;
|
|
13
|
+
constructor(knex: KnexClient.Interface, schemaConfig?: SqlSchemaConfig.Interface | undefined);
|
|
14
|
+
create(): TenancyStorageOperations;
|
|
15
|
+
}
|
|
16
|
+
export declare const SqlTenancyStorageOperationsFactory: typeof SqlTenancyStorageOperationsFactoryImpl & {
|
|
17
|
+
__abstraction: import("@webiny/di").Abstraction<import("@webiny/api-core/features/storageOperations/abstractions.js").ITenancyStorageOperationsFactory>;
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { TenancyStorageOperationsFactory } from "@webiny/api-core/features/storageOperations/abstractions.js";
|
|
2
|
+
import { KnexClient } from "../feature/KnexClient/index.js";
|
|
3
|
+
import { SqlSchemaConfig } from "../schemaConfig/index.js";
|
|
4
|
+
import { TableManager } from "../TableManager.js";
|
|
5
|
+
import { createStorageOperations } from "../tenancy/index.js";
|
|
6
|
+
class SqlTenancyStorageOperationsFactoryImpl {
|
|
7
|
+
constructor(knex, schemaConfig){
|
|
8
|
+
this.knex = knex;
|
|
9
|
+
this.schemaConfig = schemaConfig;
|
|
10
|
+
}
|
|
11
|
+
create() {
|
|
12
|
+
const knex = this.knex.client;
|
|
13
|
+
const tableManager = new TableManager(knex, this.schemaConfig?.tableNamePrefix, {
|
|
14
|
+
schemaManaged: this.schemaConfig?.isManaged() === true
|
|
15
|
+
});
|
|
16
|
+
return createStorageOperations({
|
|
17
|
+
knex,
|
|
18
|
+
tableManager
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const SqlTenancyStorageOperationsFactory = TenancyStorageOperationsFactory.createImplementation({
|
|
23
|
+
implementation: SqlTenancyStorageOperationsFactoryImpl,
|
|
24
|
+
dependencies: [
|
|
25
|
+
KnexClient,
|
|
26
|
+
[
|
|
27
|
+
SqlSchemaConfig,
|
|
28
|
+
{
|
|
29
|
+
optional: true
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
]
|
|
33
|
+
});
|
|
34
|
+
export { SqlTenancyStorageOperationsFactory };
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=SqlTenancyStorageOperationsFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coreStorage/SqlTenancyStorageOperationsFactory.js","sources":["../../src/coreStorage/SqlTenancyStorageOperationsFactory.ts"],"sourcesContent":["// A deep import: the connection feature loads this, and the package root pulls in all of api-core.\nimport { TenancyStorageOperationsFactory } from \"@webiny/api-core/features/storageOperations/abstractions.js\";\nimport type { TenancyStorageOperations } from \"@webiny/api-core/types/tenancy.js\";\nimport { KnexClient } from \"~/feature/KnexClient/index.js\";\nimport { SqlSchemaConfig } from \"~/schemaConfig/index.js\";\nimport { TableManager } from \"~/TableManager.js\";\nimport { createStorageOperations as createTenancyStorageOperations } from \"~/tenancy/index.js\";\n\n/**\n * The tenant registry's storage on the container's connection: the tenants table only. The request\n * container reads the request's tenant from it, before any tenant scope (with the rest of the core\n * storage) exists.\n */\nclass SqlTenancyStorageOperationsFactoryImpl implements TenancyStorageOperationsFactory.Interface {\n public constructor(\n private readonly knex: KnexClient.Interface,\n private readonly schemaConfig?: SqlSchemaConfig.Interface\n ) {}\n\n public create(): TenancyStorageOperations {\n const knex = this.knex.client;\n const tableManager = new TableManager(knex, this.schemaConfig?.tableNamePrefix, {\n schemaManaged: this.schemaConfig?.isManaged() === true\n });\n return createTenancyStorageOperations({ knex, tableManager });\n }\n}\n\nexport const SqlTenancyStorageOperationsFactory =\n TenancyStorageOperationsFactory.createImplementation({\n implementation: SqlTenancyStorageOperationsFactoryImpl,\n dependencies: [KnexClient, [SqlSchemaConfig, { optional: true }]]\n });\n"],"names":["SqlTenancyStorageOperationsFactoryImpl","knex","schemaConfig","tableManager","TableManager","createTenancyStorageOperations","SqlTenancyStorageOperationsFactory","TenancyStorageOperationsFactory","KnexClient","SqlSchemaConfig"],"mappings":";;;;;AAaA,MAAMA;IACF,YACqBC,IAA0B,EAC1BC,YAAwC,CAC3D;aAFmBD,IAAI,GAAJA;aACAC,YAAY,GAAZA;IAClB;IAEI,SAAmC;QACtC,MAAMD,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM;QAC7B,MAAME,eAAe,IAAIC,aAAaH,MAAM,IAAI,CAAC,YAAY,EAAE,iBAAiB;YAC5E,eAAe,IAAI,CAAC,YAAY,EAAE,gBAAgB;QACtD;QACA,OAAOI,wBAA+B;YAAEJ;YAAME;QAAa;IAC/D;AACJ;AAEO,MAAMG,qCACTC,gCAAgC,oBAAoB,CAAC;IACjD,gBAAgBP;IAChB,cAAc;QAACQ;QAAY;YAACC;YAAiB;gBAAE,UAAU;YAAK;SAAE;KAAC;AACrE"}
|
package/createApiCoreSql.d.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import type { Knex } from "knex";
|
|
2
2
|
import type { ApiCoreStorageOperations } from "@webiny/api-core/types/core.js";
|
|
3
3
|
import { TableManager } from "./TableManager.js";
|
|
4
|
+
import type { ScopeTenant } from "@webiny/api-core/features/tenancy/ScopeTenant/index.js";
|
|
4
5
|
interface CreateApiCoreSqlParams {
|
|
5
6
|
knex: Knex;
|
|
6
7
|
tableNamePrefix?: string;
|
|
7
8
|
/** The schema was set up before the API took traffic: no table checks at runtime. */
|
|
8
9
|
schemaManaged?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* The tenant the storage acts on: its tenant scope's. The tenant records and the key-value store
|
|
12
|
+
* don't use it (the tenant registry is tenant-neutral, and the store is keyed by its own scope).
|
|
13
|
+
*/
|
|
14
|
+
scopeTenant: ScopeTenant.Interface;
|
|
9
15
|
}
|
|
10
16
|
/**
|
|
11
17
|
* The core storage operations (users, tenancy, security, key-value store) on `tableManager`, which
|
|
12
18
|
* they declare their tables on.
|
|
13
19
|
*/
|
|
14
|
-
export declare const createApiCoreSqlOperations: (knex: Knex, tableManager: TableManager) => ApiCoreStorageOperations;
|
|
20
|
+
export declare const createApiCoreSqlOperations: (knex: Knex, tableManager: TableManager, scopeTenant: ScopeTenant.Interface) => ApiCoreStorageOperations;
|
|
15
21
|
/**
|
|
16
22
|
* The core storage operations on a connection. Cheap to build: what the table checks learn is kept
|
|
17
23
|
* for the process (see `TableManager`), so they are built per request.
|
|
18
24
|
*/
|
|
19
|
-
export declare const createApiCoreSql: ({ knex, tableNamePrefix, schemaManaged }: CreateApiCoreSqlParams) => ApiCoreStorageOperations;
|
|
25
|
+
export declare const createApiCoreSql: ({ knex, tableNamePrefix, schemaManaged, scopeTenant }: CreateApiCoreSqlParams) => ApiCoreStorageOperations;
|
|
20
26
|
export {};
|
package/createApiCoreSql.js
CHANGED
|
@@ -3,10 +3,11 @@ import { createStorageOperations as index_js_createStorageOperations } from "./t
|
|
|
3
3
|
import { createStorageOperations as external_security_index_js_createStorageOperations } from "./security/index.js";
|
|
4
4
|
import { createStorageOperations as external_keyValueStore_index_js_createStorageOperations } from "./keyValueStore/index.js";
|
|
5
5
|
import { TableManager } from "./TableManager.js";
|
|
6
|
-
const createApiCoreSqlOperations = (knex, tableManager)=>({
|
|
6
|
+
const createApiCoreSqlOperations = (knex, tableManager, scopeTenant)=>({
|
|
7
7
|
usersStorageOperations: createStorageOperations({
|
|
8
8
|
knex,
|
|
9
|
-
tableManager
|
|
9
|
+
tableManager,
|
|
10
|
+
scopeTenant
|
|
10
11
|
}),
|
|
11
12
|
tenancyStorageOperations: index_js_createStorageOperations({
|
|
12
13
|
knex,
|
|
@@ -14,18 +15,19 @@ const createApiCoreSqlOperations = (knex, tableManager)=>({
|
|
|
14
15
|
}),
|
|
15
16
|
securityStorageOperations: external_security_index_js_createStorageOperations({
|
|
16
17
|
knex,
|
|
17
|
-
tableManager
|
|
18
|
+
tableManager,
|
|
19
|
+
scopeTenant
|
|
18
20
|
}),
|
|
19
21
|
keyValueStorageOperations: external_keyValueStore_index_js_createStorageOperations({
|
|
20
22
|
knex,
|
|
21
23
|
tableManager
|
|
22
24
|
})
|
|
23
25
|
});
|
|
24
|
-
const createApiCoreSql = ({ knex, tableNamePrefix, schemaManaged })=>{
|
|
26
|
+
const createApiCoreSql = ({ knex, tableNamePrefix, schemaManaged, scopeTenant })=>{
|
|
25
27
|
const tableManager = new TableManager(knex, tableNamePrefix, {
|
|
26
28
|
schemaManaged
|
|
27
29
|
});
|
|
28
|
-
return createApiCoreSqlOperations(knex, tableManager);
|
|
30
|
+
return createApiCoreSqlOperations(knex, tableManager, scopeTenant);
|
|
29
31
|
};
|
|
30
32
|
export { createApiCoreSql, createApiCoreSqlOperations };
|
|
31
33
|
|
package/createApiCoreSql.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createApiCoreSql.js","sources":["../src/createApiCoreSql.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type { ApiCoreStorageOperations } from \"@webiny/api-core/types/core.js\";\nimport { createStorageOperations as createUsersStorageOperations } from \"./adminUsers/index.js\";\nimport { createStorageOperations as createTenancyStorageOperations } from \"./tenancy/index.js\";\nimport { createStorageOperations as createSecurityStorageOperations } from \"./security/index.js\";\nimport { createStorageOperations as createKeyValueStorageOperations } from \"./keyValueStore/index.js\";\nimport { TableManager } from \"./TableManager.js\";\n\ninterface CreateApiCoreSqlParams {\n knex: Knex;\n tableNamePrefix?: string;\n /** The schema was set up before the API took traffic: no table checks at runtime. */\n schemaManaged?: boolean;\n}\n\n/**\n * The core storage operations (users, tenancy, security, key-value store) on `tableManager`, which\n * they declare their tables on.\n */\nexport const createApiCoreSqlOperations = (\n knex: Knex,\n tableManager: TableManager\n): ApiCoreStorageOperations => {\n return {\n usersStorageOperations: createUsersStorageOperations({ knex, tableManager }),\n tenancyStorageOperations: createTenancyStorageOperations({ knex, tableManager }),\n securityStorageOperations: createSecurityStorageOperations({
|
|
1
|
+
{"version":3,"file":"createApiCoreSql.js","sources":["../src/createApiCoreSql.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type { ApiCoreStorageOperations } from \"@webiny/api-core/types/core.js\";\nimport { createStorageOperations as createUsersStorageOperations } from \"./adminUsers/index.js\";\nimport { createStorageOperations as createTenancyStorageOperations } from \"./tenancy/index.js\";\nimport { createStorageOperations as createSecurityStorageOperations } from \"./security/index.js\";\nimport { createStorageOperations as createKeyValueStorageOperations } from \"./keyValueStore/index.js\";\nimport { TableManager } from \"./TableManager.js\";\nimport type { ScopeTenant } from \"@webiny/api-core/features/tenancy/ScopeTenant/index.js\";\n\ninterface CreateApiCoreSqlParams {\n knex: Knex;\n tableNamePrefix?: string;\n /** The schema was set up before the API took traffic: no table checks at runtime. */\n schemaManaged?: boolean;\n /**\n * The tenant the storage acts on: its tenant scope's. The tenant records and the key-value store\n * don't use it (the tenant registry is tenant-neutral, and the store is keyed by its own scope).\n */\n scopeTenant: ScopeTenant.Interface;\n}\n\n/**\n * The core storage operations (users, tenancy, security, key-value store) on `tableManager`, which\n * they declare their tables on.\n */\nexport const createApiCoreSqlOperations = (\n knex: Knex,\n tableManager: TableManager,\n scopeTenant: ScopeTenant.Interface\n): ApiCoreStorageOperations => {\n return {\n usersStorageOperations: createUsersStorageOperations({ knex, tableManager, scopeTenant }),\n tenancyStorageOperations: createTenancyStorageOperations({ knex, tableManager }),\n securityStorageOperations: createSecurityStorageOperations({\n knex,\n tableManager,\n scopeTenant\n }),\n keyValueStorageOperations: createKeyValueStorageOperations({ knex, tableManager })\n };\n};\n\n/**\n * The core storage operations on a connection. Cheap to build: what the table checks learn is kept\n * for the process (see `TableManager`), so they are built per request.\n */\nexport const createApiCoreSql = ({\n knex,\n tableNamePrefix,\n schemaManaged,\n scopeTenant\n}: CreateApiCoreSqlParams): ApiCoreStorageOperations => {\n const tableManager = new TableManager(knex, tableNamePrefix, { schemaManaged });\n return createApiCoreSqlOperations(knex, tableManager, scopeTenant);\n};\n"],"names":["createApiCoreSqlOperations","knex","tableManager","scopeTenant","createUsersStorageOperations","createTenancyStorageOperations","createSecurityStorageOperations","createKeyValueStorageOperations","createApiCoreSql","tableNamePrefix","schemaManaged","TableManager"],"mappings":";;;;;AAyBO,MAAMA,6BAA6B,CACtCC,MACAC,cACAC,cAEO;QACH,wBAAwBC,wBAA6B;YAAEH;YAAMC;YAAcC;QAAY;QACvF,0BAA0BE,iCAA+B;YAAEJ;YAAMC;QAAa;QAC9E,2BAA2BI,mDAAgC;YACvDL;YACAC;YACAC;QACJ;QACA,2BAA2BI,wDAAgC;YAAEN;YAAMC;QAAa;IACpF;AAOG,MAAMM,mBAAmB,CAAC,EAC7BP,IAAI,EACJQ,eAAe,EACfC,aAAa,EACbP,WAAW,EACU;IACrB,MAAMD,eAAe,IAAIS,aAAaV,MAAMQ,iBAAiB;QAAEC;IAAc;IAC7E,OAAOV,2BAA2BC,MAAMC,cAAcC;AAC1D"}
|
package/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type { ISqlSchemaSetupRequest, RequiredSqlSchemaSetup, RunSchemaSetupStep
|
|
|
12
12
|
export { getSchemaMemory } from "./schemaMemory/getSchemaMemory.js";
|
|
13
13
|
export { getConnectionKey } from "./schemaMemory/getConnectionKey.js";
|
|
14
14
|
export { ensureTable, createTableIfMissing, recordTable } from "./schemaMemory/ensureTable.js";
|
|
15
|
-
export type { EnsureTableParams, TableCreator } from "./schemaMemory/ensureTable.js";
|
|
15
|
+
export type { EnsureTableParams, TableCreator, TableUpgrade } from "./schemaMemory/ensureTable.js";
|
|
16
16
|
export type { ISchemaMemory } from "./schemaMemory/getSchemaMemory.js";
|
|
17
17
|
export type { SqlStorage } from "./storage/types.js";
|
|
18
18
|
export * from "./connectionRetry/index.js";
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { createRegisterExtensionPlugin } from \"@webiny/handler\";\nimport type { Knex } from \"knex\";\nimport { KnexClientFeature } from \"./feature/KnexClient/index.js\";\n\nexport { createApiCoreSql } from \"./createApiCoreSql.js\";\nexport { ApiCoreSqlFeature } from \"./coreStorage/index.js\";\nexport { SqlConnectionFeature, setSqlServiceManifestLoader } from \"./connection/index.js\";\nexport type { SqlConnectionConfig } from \"./connection/index.js\";\nexport { getSqlTablePrefix } from \"./getSqlTablePrefix.js\";\nexport { TableManager } from \"./TableManager.js\";\nexport type { TableManagerOptions } from \"./TableManager.js\";\nexport { SqlSchemaConfig } from \"./schemaConfig/index.js\";\nexport { SqlSchemaSetup, SqlSchemaSetupRunner, runSchemaSetupSteps } from \"./schemaSetup/index.js\";\nexport type {\n ISqlSchemaSetupRequest,\n RequiredSqlSchemaSetup,\n RunSchemaSetupStepsParams,\n SqlSchemaSetupRunParams\n} from \"./schemaSetup/index.js\";\nexport { getSchemaMemory } from \"./schemaMemory/getSchemaMemory.js\";\nexport { getConnectionKey } from \"./schemaMemory/getConnectionKey.js\";\nexport { ensureTable, createTableIfMissing, recordTable } from \"./schemaMemory/ensureTable.js\";\nexport type { EnsureTableParams, TableCreator } from \"./schemaMemory/ensureTable.js\";\nexport type { ISchemaMemory } from \"./schemaMemory/getSchemaMemory.js\";\nexport type { SqlStorage } from \"./storage/types.js\";\nexport * from \"./connectionRetry/index.js\";\nexport { SqlServiceManifestLoader, SqlServiceManifestWriter } from \"./serviceDiscovery/index.js\";\n\nexport { KnexClient } from \"./feature/KnexClient/index.js\";\n\ninterface IRegisterApiCoreSqlExtension {\n knex: Knex;\n}\n\nexport const registerSQLCore = ({ knex }: IRegisterApiCoreSqlExtension) => {\n return createRegisterExtensionPlugin(async context => {\n KnexClientFeature.register(context.container, knex);\n });\n};\n"],"names":["registerSQLCore","knex","createRegisterExtensionPlugin","context","KnexClientFeature"],"mappings":";;;AAkCO,MAAMA,kBAAkB,CAAC,EAAEC,IAAI,EAAgC,GAC3DC,8BAA8B,OAAMC;QACvCC,kBAAkB,QAAQ,CAACD,QAAQ,SAAS,EAAEF;IAClD"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { createRegisterExtensionPlugin } from \"@webiny/handler\";\nimport type { Knex } from \"knex\";\nimport { KnexClientFeature } from \"./feature/KnexClient/index.js\";\n\nexport { createApiCoreSql } from \"./createApiCoreSql.js\";\nexport { ApiCoreSqlFeature } from \"./coreStorage/index.js\";\nexport { SqlConnectionFeature, setSqlServiceManifestLoader } from \"./connection/index.js\";\nexport type { SqlConnectionConfig } from \"./connection/index.js\";\nexport { getSqlTablePrefix } from \"./getSqlTablePrefix.js\";\nexport { TableManager } from \"./TableManager.js\";\nexport type { TableManagerOptions } from \"./TableManager.js\";\nexport { SqlSchemaConfig } from \"./schemaConfig/index.js\";\nexport { SqlSchemaSetup, SqlSchemaSetupRunner, runSchemaSetupSteps } from \"./schemaSetup/index.js\";\nexport type {\n ISqlSchemaSetupRequest,\n RequiredSqlSchemaSetup,\n RunSchemaSetupStepsParams,\n SqlSchemaSetupRunParams\n} from \"./schemaSetup/index.js\";\nexport { getSchemaMemory } from \"./schemaMemory/getSchemaMemory.js\";\nexport { getConnectionKey } from \"./schemaMemory/getConnectionKey.js\";\nexport { ensureTable, createTableIfMissing, recordTable } from \"./schemaMemory/ensureTable.js\";\nexport type { EnsureTableParams, TableCreator, TableUpgrade } from \"./schemaMemory/ensureTable.js\";\nexport type { ISchemaMemory } from \"./schemaMemory/getSchemaMemory.js\";\nexport type { SqlStorage } from \"./storage/types.js\";\nexport * from \"./connectionRetry/index.js\";\nexport { SqlServiceManifestLoader, SqlServiceManifestWriter } from \"./serviceDiscovery/index.js\";\n\nexport { KnexClient } from \"./feature/KnexClient/index.js\";\n\ninterface IRegisterApiCoreSqlExtension {\n knex: Knex;\n}\n\nexport const registerSQLCore = ({ knex }: IRegisterApiCoreSqlExtension) => {\n return createRegisterExtensionPlugin(async context => {\n KnexClientFeature.register(context.container, knex);\n });\n};\n"],"names":["registerSQLCore","knex","createRegisterExtensionPlugin","context","KnexClientFeature"],"mappings":";;;AAkCO,MAAMA,kBAAkB,CAAC,EAAEC,IAAI,EAAgC,GAC3DC,8BAA8B,OAAMC;QACvCC,kBAAkB,QAAQ,CAACD,QAAQ,SAAS,EAAEF;IAClD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/api-core-sql",
|
|
3
|
-
"version": "6.6.0-alpha.
|
|
3
|
+
"version": "6.6.0-alpha.13",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.js",
|
|
@@ -21,18 +21,18 @@
|
|
|
21
21
|
"author": "Webiny Ltd.",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@webiny/api-core": "6.6.0-alpha.
|
|
25
|
-
"@webiny/error": "6.6.0-alpha.
|
|
26
|
-
"@webiny/feature": "6.6.0-alpha.
|
|
27
|
-
"@webiny/handler": "6.6.0-alpha.
|
|
24
|
+
"@webiny/api-core": "6.6.0-alpha.13",
|
|
25
|
+
"@webiny/error": "6.6.0-alpha.13",
|
|
26
|
+
"@webiny/feature": "6.6.0-alpha.13",
|
|
27
|
+
"@webiny/handler": "6.6.0-alpha.13",
|
|
28
28
|
"knex": "3.3.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@webiny/build-tools": "6.6.0-alpha.
|
|
32
|
-
"better-sqlite3": "13.0
|
|
31
|
+
"@webiny/build-tools": "6.6.0-alpha.13",
|
|
32
|
+
"better-sqlite3": "13.2.0",
|
|
33
33
|
"pg": "8.23.1",
|
|
34
34
|
"typescript": "7.0.2",
|
|
35
|
-
"vitest": "5.
|
|
35
|
+
"vitest": "5.1.1"
|
|
36
36
|
},
|
|
37
37
|
"publishConfig": {
|
|
38
38
|
"access": "public"
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { Knex } from "knex";
|
|
2
2
|
import type { ISchemaMemory } from "./getSchemaMemory.js";
|
|
3
3
|
export type TableCreator = (table: Knex.CreateTableBuilder) => void;
|
|
4
|
+
/**
|
|
5
|
+
* Brings a table that already existed up to date (the creator only runs for new tables).
|
|
6
|
+
* Must be idempotent: it runs once per process for every existing table.
|
|
7
|
+
*/
|
|
8
|
+
export type TableUpgrade = (knex: Knex, tableName: string) => Promise<void>;
|
|
4
9
|
export interface EnsureTableParams {
|
|
5
10
|
/** The connection or transaction to check and create the table on. */
|
|
6
11
|
knex: Knex;
|
|
@@ -8,6 +13,7 @@ export interface EnsureTableParams {
|
|
|
8
13
|
memory: ISchemaMemory;
|
|
9
14
|
tableName: string;
|
|
10
15
|
create: TableCreator;
|
|
16
|
+
upgrade?: TableUpgrade;
|
|
11
17
|
}
|
|
12
18
|
/**
|
|
13
19
|
* Creates the table if it doesn't exist yet, once per process: a table the memory knows is skipped,
|
|
@@ -22,7 +28,8 @@ export declare function ensureTable(params: EnsureTableParams): Promise<void>;
|
|
|
22
28
|
*/
|
|
23
29
|
export declare function recordTable(knex: Knex, memory: ISchemaMemory, tableName: string): void;
|
|
24
30
|
/**
|
|
25
|
-
* Creates the table if it doesn't exist
|
|
26
|
-
* check and the create: if it exists after a failed create,
|
|
31
|
+
* Creates the table if it doesn't exist, or runs `upgrade` on it if it does. Another process (or
|
|
32
|
+
* connection) may create it between the check and the create: if it exists after a failed create,
|
|
33
|
+
* that's success, and that process created it with the current creator.
|
|
27
34
|
*/
|
|
28
|
-
export declare function createTableIfMissing(knex: Knex, tableName: string, create: TableCreator): Promise<void>;
|
|
35
|
+
export declare function createTableIfMissing(knex: Knex, tableName: string, create: TableCreator, upgrade?: TableUpgrade): Promise<void>;
|
|
@@ -12,13 +12,13 @@ const handleOf = (knex)=>{
|
|
|
12
12
|
return `trx${id}`;
|
|
13
13
|
};
|
|
14
14
|
function ensureTable(params) {
|
|
15
|
-
const { knex, memory, tableName, create } = params;
|
|
15
|
+
const { knex, memory, tableName, create, upgrade } = params;
|
|
16
16
|
if (memory.known.has(tableName)) return Promise.resolve();
|
|
17
17
|
const generation = memory.generation;
|
|
18
18
|
const key = `${memory.key}#${generation}:${tableName}:${handleOf(knex)}`;
|
|
19
19
|
const pending = inFlight.get(key);
|
|
20
20
|
if (pending) return pending;
|
|
21
|
-
const check = createTableIfMissing(knex, tableName, create).then(()=>{
|
|
21
|
+
const check = createTableIfMissing(knex, tableName, create, upgrade).then(()=>{
|
|
22
22
|
if (memory.generation === generation) recordTable(knex, memory, tableName);
|
|
23
23
|
}).finally(()=>{
|
|
24
24
|
if (inFlight.get(key) === check) inFlight.delete(key);
|
|
@@ -29,8 +29,11 @@ function ensureTable(params) {
|
|
|
29
29
|
function recordTable(knex, memory, tableName) {
|
|
30
30
|
if (!knex.isTransaction) memory.known.add(tableName);
|
|
31
31
|
}
|
|
32
|
-
async function createTableIfMissing(knex, tableName, create) {
|
|
33
|
-
if (await knex.schema.hasTable(tableName))
|
|
32
|
+
async function createTableIfMissing(knex, tableName, create, upgrade) {
|
|
33
|
+
if (await knex.schema.hasTable(tableName)) {
|
|
34
|
+
if (upgrade) await upgrade(knex, tableName);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
34
37
|
try {
|
|
35
38
|
await knex.schema.createTable(tableName, create);
|
|
36
39
|
} catch (error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schemaMemory/ensureTable.js","sources":["../../src/schemaMemory/ensureTable.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type { ISchemaMemory } from \"./getSchemaMemory.js\";\n\nexport type TableCreator = (table: Knex.CreateTableBuilder) => void;\n\nexport interface EnsureTableParams {\n /** The connection or transaction to check and create the table on. */\n knex: Knex;\n /** What the process knows about the tables this one belongs to. */\n memory: ISchemaMemory;\n tableName: string;\n create: TableCreator;\n}\n\n/*\n * Checks in flight, per memory, table and handle, so concurrent first uses share one\n * hasTable+createTable instead of racing into \"table already exists\".\n */\nconst inFlight = new Map<string, Promise<void>>();\n\nconst transactionIds = new WeakMap<object, number>();\nlet lastTransactionId = 0;\n\n/*\n * Which handle a check runs on. A transaction shares its connection's key (`getConnectionKey`), but\n * a table it creates is visible to others only once it commits, and is gone if it rolls back. So a\n * check in a transaction is shared only with checks in that same transaction.\n */\nconst handleOf = (knex: Knex): string => {\n if (!knex.isTransaction) {\n return \"connection\";\n }\n let id = transactionIds.get(knex);\n if (id === undefined) {\n lastTransactionId += 1;\n id = lastTransactionId;\n transactionIds.set(knex, id);\n }\n return `trx${id}`;\n};\n\n/**\n * Creates the table if it doesn't exist yet, once per process: a table the memory knows is skipped,\n * and concurrent callers on the same handle wait for one check. What a transaction creates is\n * remembered only by `recordTable` after it commits, never here, and nothing started before the\n * memory was reset is recorded after it.\n */\nexport function ensureTable(params: EnsureTableParams): Promise<void> {\n const { knex, memory, tableName, create } = params;\n if (memory.known.has(tableName)) {\n return Promise.resolve();\n }\n\n const generation = memory.generation;\n const key = `${memory.key}#${generation}:${tableName}:${handleOf(knex)}`;\n const pending = inFlight.get(key);\n if (pending) {\n return pending;\n }\n\n const check: Promise<void> = createTableIfMissing(knex, tableName, create)\n .then(() => {\n if (memory.generation === generation) {\n recordTable(knex, memory, tableName);\n }\n })\n .finally(() => {\n if (inFlight.get(key) === check) {\n inFlight.delete(key);\n }\n });\n inFlight.set(key, check);\n return check;\n}\n\n/**\n * Records that the table exists, unless `knex` is a transaction: it can still roll back, and the\n * table with it, so only a committed table is known.\n */\nexport function recordTable(knex: Knex, memory: ISchemaMemory, tableName: string): void {\n if (!knex.isTransaction) {\n memory.known.add(tableName);\n }\n}\n\n/**\n * Creates the table if it doesn't exist. Another process (or connection) may create it between the
|
|
1
|
+
{"version":3,"file":"schemaMemory/ensureTable.js","sources":["../../src/schemaMemory/ensureTable.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type { ISchemaMemory } from \"./getSchemaMemory.js\";\n\nexport type TableCreator = (table: Knex.CreateTableBuilder) => void;\n\n/**\n * Brings a table that already existed up to date (the creator only runs for new tables).\n * Must be idempotent: it runs once per process for every existing table.\n */\nexport type TableUpgrade = (knex: Knex, tableName: string) => Promise<void>;\n\nexport interface EnsureTableParams {\n /** The connection or transaction to check and create the table on. */\n knex: Knex;\n /** What the process knows about the tables this one belongs to. */\n memory: ISchemaMemory;\n tableName: string;\n create: TableCreator;\n upgrade?: TableUpgrade;\n}\n\n/*\n * Checks in flight, per memory, table and handle, so concurrent first uses share one\n * hasTable+createTable instead of racing into \"table already exists\".\n */\nconst inFlight = new Map<string, Promise<void>>();\n\nconst transactionIds = new WeakMap<object, number>();\nlet lastTransactionId = 0;\n\n/*\n * Which handle a check runs on. A transaction shares its connection's key (`getConnectionKey`), but\n * a table it creates is visible to others only once it commits, and is gone if it rolls back. So a\n * check in a transaction is shared only with checks in that same transaction.\n */\nconst handleOf = (knex: Knex): string => {\n if (!knex.isTransaction) {\n return \"connection\";\n }\n let id = transactionIds.get(knex);\n if (id === undefined) {\n lastTransactionId += 1;\n id = lastTransactionId;\n transactionIds.set(knex, id);\n }\n return `trx${id}`;\n};\n\n/**\n * Creates the table if it doesn't exist yet, once per process: a table the memory knows is skipped,\n * and concurrent callers on the same handle wait for one check. What a transaction creates is\n * remembered only by `recordTable` after it commits, never here, and nothing started before the\n * memory was reset is recorded after it.\n */\nexport function ensureTable(params: EnsureTableParams): Promise<void> {\n const { knex, memory, tableName, create, upgrade } = params;\n if (memory.known.has(tableName)) {\n return Promise.resolve();\n }\n\n const generation = memory.generation;\n const key = `${memory.key}#${generation}:${tableName}:${handleOf(knex)}`;\n const pending = inFlight.get(key);\n if (pending) {\n return pending;\n }\n\n const check: Promise<void> = createTableIfMissing(knex, tableName, create, upgrade)\n .then(() => {\n if (memory.generation === generation) {\n recordTable(knex, memory, tableName);\n }\n })\n .finally(() => {\n if (inFlight.get(key) === check) {\n inFlight.delete(key);\n }\n });\n inFlight.set(key, check);\n return check;\n}\n\n/**\n * Records that the table exists, unless `knex` is a transaction: it can still roll back, and the\n * table with it, so only a committed table is known.\n */\nexport function recordTable(knex: Knex, memory: ISchemaMemory, tableName: string): void {\n if (!knex.isTransaction) {\n memory.known.add(tableName);\n }\n}\n\n/**\n * Creates the table if it doesn't exist, or runs `upgrade` on it if it does. Another process (or\n * connection) may create it between the check and the create: if it exists after a failed create,\n * that's success, and that process created it with the current creator.\n */\nexport async function createTableIfMissing(\n knex: Knex,\n tableName: string,\n create: TableCreator,\n upgrade?: TableUpgrade\n): Promise<void> {\n if (await knex.schema.hasTable(tableName)) {\n if (upgrade) {\n await upgrade(knex, tableName);\n }\n return;\n }\n try {\n await knex.schema.createTable(tableName, create);\n } catch (error) {\n if (await knex.schema.hasTable(tableName)) {\n return;\n }\n throw error;\n }\n}\n"],"names":["inFlight","Map","transactionIds","WeakMap","lastTransactionId","handleOf","knex","id","undefined","ensureTable","params","memory","tableName","create","upgrade","Promise","generation","key","pending","check","createTableIfMissing","recordTable","error"],"mappings":"AAyBA,MAAMA,WAAW,IAAIC;AAErB,MAAMC,iBAAiB,IAAIC;AAC3B,IAAIC,oBAAoB;AAOxB,MAAMC,WAAW,CAACC;IACd,IAAI,CAACA,KAAK,aAAa,EACnB,OAAO;IAEX,IAAIC,KAAKL,eAAe,GAAG,CAACI;IAC5B,IAAIC,AAAOC,WAAPD,IAAkB;QAClBH,qBAAqB;QACrBG,KAAKH;QACLF,eAAe,GAAG,CAACI,MAAMC;IAC7B;IACA,OAAO,CAAC,GAAG,EAAEA,IAAI;AACrB;AAQO,SAASE,YAAYC,MAAyB;IACjD,MAAM,EAAEJ,IAAI,EAAEK,MAAM,EAAEC,SAAS,EAAEC,MAAM,EAAEC,OAAO,EAAE,GAAGJ;IACrD,IAAIC,OAAO,KAAK,CAAC,GAAG,CAACC,YACjB,OAAOG,QAAQ,OAAO;IAG1B,MAAMC,aAAaL,OAAO,UAAU;IACpC,MAAMM,MAAM,GAAGN,OAAO,GAAG,CAAC,CAAC,EAAEK,WAAW,CAAC,EAAEJ,UAAU,CAAC,EAAEP,SAASC,OAAO;IACxE,MAAMY,UAAUlB,SAAS,GAAG,CAACiB;IAC7B,IAAIC,SACA,OAAOA;IAGX,MAAMC,QAAuBC,qBAAqBd,MAAMM,WAAWC,QAAQC,SACtE,IAAI,CAAC;QACF,IAAIH,OAAO,UAAU,KAAKK,YACtBK,YAAYf,MAAMK,QAAQC;IAElC,GACC,OAAO,CAAC;QACL,IAAIZ,SAAS,GAAG,CAACiB,SAASE,OACtBnB,SAAS,MAAM,CAACiB;IAExB;IACJjB,SAAS,GAAG,CAACiB,KAAKE;IAClB,OAAOA;AACX;AAMO,SAASE,YAAYf,IAAU,EAAEK,MAAqB,EAAEC,SAAiB;IAC5E,IAAI,CAACN,KAAK,aAAa,EACnBK,OAAO,KAAK,CAAC,GAAG,CAACC;AAEzB;AAOO,eAAeQ,qBAClBd,IAAU,EACVM,SAAiB,EACjBC,MAAoB,EACpBC,OAAsB;IAEtB,IAAI,MAAMR,KAAK,MAAM,CAAC,QAAQ,CAACM,YAAY;QACvC,IAAIE,SACA,MAAMA,QAAQR,MAAMM;QAExB;IACJ;IACA,IAAI;QACA,MAAMN,KAAK,MAAM,CAAC,WAAW,CAACM,WAAWC;IAC7C,EAAE,OAAOS,OAAO;QACZ,IAAI,MAAMhB,KAAK,MAAM,CAAC,QAAQ,CAACM,YAC3B;QAEJ,MAAMU;IACV;AACJ"}
|
package/security/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { Knex } from "knex";
|
|
2
2
|
import type { SecurityStorageOperations } from "@webiny/api-core/types/security.js";
|
|
3
|
+
import type { ScopeTenant } from "@webiny/api-core/features/tenancy/ScopeTenant/index.js";
|
|
3
4
|
import type { TableManager } from "../TableManager.js";
|
|
4
5
|
interface CreateStorageOperationsParams {
|
|
5
6
|
knex: Knex;
|
|
6
7
|
tableManager: TableManager;
|
|
8
|
+
/** The tenant the storage acts on: its scope's. */
|
|
9
|
+
scopeTenant: ScopeTenant.Interface;
|
|
7
10
|
}
|
|
8
11
|
export declare const createStorageOperations: (params: CreateStorageOperationsParams) => SecurityStorageOperations;
|
|
9
12
|
export {};
|
package/security/index.js
CHANGED
|
@@ -4,7 +4,11 @@ const ROLES_TABLE = "webiny_core_roles";
|
|
|
4
4
|
const TEAMS_TABLE = "webiny_core_teams";
|
|
5
5
|
const API_KEYS_TABLE = "webiny_core_api_keys";
|
|
6
6
|
const createStorageOperations = (params)=>{
|
|
7
|
-
const { knex, tableManager } = params;
|
|
7
|
+
const { knex, tableManager, scopeTenant } = params;
|
|
8
|
+
const inScope = (record)=>({
|
|
9
|
+
...record,
|
|
10
|
+
tenant: scopeTenant.getId()
|
|
11
|
+
});
|
|
8
12
|
const ensureRolesTable = tableManager.define(ROLES_TABLE, (table)=>{
|
|
9
13
|
table.text("id").notNullable();
|
|
10
14
|
table.text("tenant").notNullable();
|
|
@@ -57,6 +61,7 @@ const createStorageOperations = (params)=>{
|
|
|
57
61
|
const apiKeysQuery = ()=>knex(tableManager.resolve(API_KEYS_TABLE));
|
|
58
62
|
return {
|
|
59
63
|
async getRole ({ where: { tenant, id, slug } }) {
|
|
64
|
+
tenant = scopeTenant.getId();
|
|
60
65
|
await ensureRolesTable();
|
|
61
66
|
try {
|
|
62
67
|
if (id) {
|
|
@@ -77,6 +82,7 @@ const createStorageOperations = (params)=>{
|
|
|
77
82
|
}
|
|
78
83
|
},
|
|
79
84
|
async listRoles ({ where: { tenant, id_in, slug_in }, sort }) {
|
|
85
|
+
tenant = scopeTenant.getId();
|
|
80
86
|
await ensureRolesTable();
|
|
81
87
|
try {
|
|
82
88
|
const qb = rolesQuery().where("tenant", tenant);
|
|
@@ -93,6 +99,7 @@ const createStorageOperations = (params)=>{
|
|
|
93
99
|
}
|
|
94
100
|
},
|
|
95
101
|
async createRole ({ role }) {
|
|
102
|
+
role = inScope(role);
|
|
96
103
|
await ensureRolesTable();
|
|
97
104
|
try {
|
|
98
105
|
await rolesQuery().insert({
|
|
@@ -112,6 +119,7 @@ const createStorageOperations = (params)=>{
|
|
|
112
119
|
}
|
|
113
120
|
},
|
|
114
121
|
async updateRole ({ role }) {
|
|
122
|
+
role = inScope(role);
|
|
115
123
|
await ensureRolesTable();
|
|
116
124
|
try {
|
|
117
125
|
await rolesQuery().where("tenant", role.tenant).andWhere("id", role.id).update({
|
|
@@ -129,6 +137,7 @@ const createStorageOperations = (params)=>{
|
|
|
129
137
|
}
|
|
130
138
|
},
|
|
131
139
|
async deleteRole ({ role }) {
|
|
140
|
+
role = inScope(role);
|
|
132
141
|
await ensureRolesTable();
|
|
133
142
|
try {
|
|
134
143
|
await rolesQuery().where("tenant", role.tenant).andWhere("id", role.id).delete();
|
|
@@ -143,6 +152,7 @@ const createStorageOperations = (params)=>{
|
|
|
143
152
|
}
|
|
144
153
|
},
|
|
145
154
|
async getTeam ({ where: { tenant, id, slug } }) {
|
|
155
|
+
tenant = scopeTenant.getId();
|
|
146
156
|
await ensureTeamsTable();
|
|
147
157
|
try {
|
|
148
158
|
if (id) {
|
|
@@ -163,6 +173,7 @@ const createStorageOperations = (params)=>{
|
|
|
163
173
|
}
|
|
164
174
|
},
|
|
165
175
|
async listTeams ({ where: { tenant, id_in, slug_in }, sort }) {
|
|
176
|
+
tenant = scopeTenant.getId();
|
|
166
177
|
await ensureTeamsTable();
|
|
167
178
|
try {
|
|
168
179
|
const qb = teamsQuery().where("tenant", tenant);
|
|
@@ -179,6 +190,7 @@ const createStorageOperations = (params)=>{
|
|
|
179
190
|
}
|
|
180
191
|
},
|
|
181
192
|
async createTeam ({ team }) {
|
|
193
|
+
team = inScope(team);
|
|
182
194
|
await ensureTeamsTable();
|
|
183
195
|
try {
|
|
184
196
|
await teamsQuery().insert({
|
|
@@ -198,6 +210,7 @@ const createStorageOperations = (params)=>{
|
|
|
198
210
|
}
|
|
199
211
|
},
|
|
200
212
|
async updateTeam ({ team }) {
|
|
213
|
+
team = inScope(team);
|
|
201
214
|
await ensureTeamsTable();
|
|
202
215
|
try {
|
|
203
216
|
await teamsQuery().where("tenant", team.tenant).andWhere("id", team.id).update({
|
|
@@ -215,6 +228,7 @@ const createStorageOperations = (params)=>{
|
|
|
215
228
|
}
|
|
216
229
|
},
|
|
217
230
|
async deleteTeam ({ team }) {
|
|
231
|
+
team = inScope(team);
|
|
218
232
|
await ensureTeamsTable();
|
|
219
233
|
try {
|
|
220
234
|
await teamsQuery().where("tenant", team.tenant).andWhere("id", team.id).delete();
|
|
@@ -229,6 +243,7 @@ const createStorageOperations = (params)=>{
|
|
|
229
243
|
}
|
|
230
244
|
},
|
|
231
245
|
async getApiKey ({ id, tenant }) {
|
|
246
|
+
tenant = scopeTenant.getId();
|
|
232
247
|
await ensureApiKeysTable();
|
|
233
248
|
try {
|
|
234
249
|
const row = await apiKeysQuery().where("tenant", tenant).andWhere("id", id).first();
|
|
@@ -244,6 +259,7 @@ const createStorageOperations = (params)=>{
|
|
|
244
259
|
}
|
|
245
260
|
},
|
|
246
261
|
async getApiKeyByToken ({ tenant, token }) {
|
|
262
|
+
tenant = scopeTenant.getId();
|
|
247
263
|
await ensureApiKeysTable();
|
|
248
264
|
try {
|
|
249
265
|
const row = await apiKeysQuery().where("tenant", tenant).andWhere("token", token).first();
|
|
@@ -259,6 +275,7 @@ const createStorageOperations = (params)=>{
|
|
|
259
275
|
}
|
|
260
276
|
},
|
|
261
277
|
async getApiKeyBySlug ({ tenant, slug }) {
|
|
278
|
+
tenant = scopeTenant.getId();
|
|
262
279
|
await ensureApiKeysTable();
|
|
263
280
|
try {
|
|
264
281
|
const row = await apiKeysQuery().where("tenant", tenant).andWhere("slug", slug).first();
|
|
@@ -274,6 +291,7 @@ const createStorageOperations = (params)=>{
|
|
|
274
291
|
}
|
|
275
292
|
},
|
|
276
293
|
async listApiKeys ({ where: { tenant }, sort }) {
|
|
294
|
+
tenant = scopeTenant.getId();
|
|
277
295
|
await ensureApiKeysTable();
|
|
278
296
|
try {
|
|
279
297
|
const rows = await apiKeysQuery().where("tenant", tenant);
|
|
@@ -287,6 +305,7 @@ const createStorageOperations = (params)=>{
|
|
|
287
305
|
}
|
|
288
306
|
},
|
|
289
307
|
async createApiKey ({ apiKey }) {
|
|
308
|
+
apiKey = inScope(apiKey);
|
|
290
309
|
await ensureApiKeysTable();
|
|
291
310
|
try {
|
|
292
311
|
await apiKeysQuery().insert({
|
|
@@ -307,6 +326,7 @@ const createStorageOperations = (params)=>{
|
|
|
307
326
|
}
|
|
308
327
|
},
|
|
309
328
|
async updateApiKey ({ apiKey }) {
|
|
329
|
+
apiKey = inScope(apiKey);
|
|
310
330
|
await ensureApiKeysTable();
|
|
311
331
|
try {
|
|
312
332
|
await apiKeysQuery().where("tenant", apiKey.tenant).andWhere("id", apiKey.id).update({
|
|
@@ -325,6 +345,7 @@ const createStorageOperations = (params)=>{
|
|
|
325
345
|
}
|
|
326
346
|
},
|
|
327
347
|
async deleteApiKey ({ apiKey }) {
|
|
348
|
+
apiKey = inScope(apiKey);
|
|
328
349
|
await ensureApiKeysTable();
|
|
329
350
|
try {
|
|
330
351
|
await apiKeysQuery().where("tenant", apiKey.tenant).andWhere("id", apiKey.id).delete();
|
package/security/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"security/index.js","sources":["../../src/security/index.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type {\n SecurityStorageOperations,\n StorageApiKey,\n StorageRole,\n StorageTeam\n} from \"@webiny/api-core/types/security.js\";\nimport WebinyError from \"@webiny/error\";\nimport type { TableManager } from \"~/TableManager.js\";\nimport { sortItems } from \"~/sortItems.js\";\n\nconst ROLES_TABLE = \"webiny_core_roles\";\nconst TEAMS_TABLE = \"webiny_core_teams\";\nconst API_KEYS_TABLE = \"webiny_core_api_keys\";\n\ninterface IRoleRow {\n id: string;\n tenant: string;\n slug: string;\n data: string;\n}\n\ninterface ITeamRow {\n id: string;\n tenant: string;\n slug: string;\n data: string;\n}\n\ninterface IApiKeyRow {\n id: string;\n tenant: string;\n token: string;\n slug: string;\n data: string;\n}\n\ninterface CreateStorageOperationsParams {\n knex: Knex;\n tableManager: TableManager;\n}\n\nexport const createStorageOperations = (\n params: CreateStorageOperationsParams\n): SecurityStorageOperations => {\n const { knex, tableManager } = params;\n\n const ensureRolesTable = tableManager.define(ROLES_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n\n const ensureTeamsTable = tableManager.define(TEAMS_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n\n const ensureApiKeysTable = tableManager.define(API_KEYS_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"token\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"token\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n\n const rolesQuery = () => knex<IRoleRow>(tableManager.resolve(ROLES_TABLE));\n const teamsQuery = () => knex<ITeamRow>(tableManager.resolve(TEAMS_TABLE));\n const apiKeysQuery = () => knex<IApiKeyRow>(tableManager.resolve(API_KEYS_TABLE));\n\n return {\n /* Roles */\n async getRole({ where: { tenant, id, slug } }) {\n await ensureRolesTable();\n\n try {\n if (id) {\n const row = await rolesQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"id\", id)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageRole) : null;\n }\n\n const row = await rolesQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug as string)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageRole) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load role.\",\n code: \"GET_ROLE_ERROR\",\n data: { id, slug }\n });\n }\n },\n\n async listRoles({ where: { tenant, id_in, slug_in }, sort }) {\n await ensureRolesTable();\n\n try {\n const qb = rolesQuery().where(\"tenant\", tenant);\n\n if (Array.isArray(id_in)) {\n qb.whereIn(\"id\", id_in);\n } else if (Array.isArray(slug_in)) {\n qb.whereIn(\"slug\", slug_in);\n }\n\n const rows = await qb;\n const items = rows.map(row => JSON.parse(row.data) as StorageRole);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list roles.\",\n code: \"LIST_ROLE_ERROR\"\n });\n }\n },\n\n async createRole({ role }) {\n await ensureRolesTable();\n\n try {\n await rolesQuery().insert({\n id: role.id,\n tenant: role.tenant,\n slug: role.slug,\n data: JSON.stringify(role)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create role.\",\n code: \"CREATE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n async updateRole({ role }) {\n await ensureRolesTable();\n\n try {\n await rolesQuery()\n .where(\"tenant\", role.tenant)\n .andWhere(\"id\", role.id)\n .update({\n slug: role.slug,\n data: JSON.stringify(role)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update role.\",\n code: \"UPDATE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n async deleteRole({ role }) {\n await ensureRolesTable();\n\n try {\n await rolesQuery().where(\"tenant\", role.tenant).andWhere(\"id\", role.id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete role.\",\n code: \"DELETE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n /* Teams */\n async getTeam({ where: { tenant, id, slug } }) {\n await ensureTeamsTable();\n\n try {\n if (id) {\n const row = await teamsQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"id\", id)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageTeam) : null;\n }\n\n const row = await teamsQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug as string)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageTeam) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load team.\",\n code: \"GET_TEAM_ERROR\",\n data: { id, slug }\n });\n }\n },\n\n async listTeams({ where: { tenant, id_in, slug_in }, sort }) {\n await ensureTeamsTable();\n\n try {\n const qb = teamsQuery().where(\"tenant\", tenant);\n\n if (Array.isArray(id_in)) {\n qb.whereIn(\"id\", id_in);\n } else if (Array.isArray(slug_in)) {\n qb.whereIn(\"slug\", slug_in);\n }\n\n const rows = await qb;\n const items = rows.map(row => JSON.parse(row.data) as StorageTeam);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list teams.\",\n code: \"LIST_TEAM_ERROR\"\n });\n }\n },\n\n async createTeam({ team }) {\n await ensureTeamsTable();\n\n try {\n await teamsQuery().insert({\n id: team.id,\n tenant: team.tenant,\n slug: team.slug,\n data: JSON.stringify(team)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create team.\",\n code: \"CREATE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n async updateTeam({ team }) {\n await ensureTeamsTable();\n\n try {\n await teamsQuery()\n .where(\"tenant\", team.tenant)\n .andWhere(\"id\", team.id)\n .update({\n slug: team.slug,\n data: JSON.stringify(team)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update team.\",\n code: \"UPDATE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n async deleteTeam({ team }) {\n await ensureTeamsTable();\n\n try {\n await teamsQuery().where(\"tenant\", team.tenant).andWhere(\"id\", team.id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete team.\",\n code: \"DELETE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n /* API Keys */\n async getApiKey({ id, tenant }) {\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery().where(\"tenant\", tenant).andWhere(\"id\", id).first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key.\",\n code: \"GET_API_KEY_ERROR\",\n data: { id }\n });\n }\n },\n\n async getApiKeyByToken({ tenant, token }) {\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"token\", token)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key by token.\",\n code: \"GET_BY_TOKEN_API_KEY_ERROR\",\n data: { token }\n });\n }\n },\n\n async getApiKeyBySlug({ tenant, slug }) {\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key by slug.\",\n code: \"GET_BY_SLUG_API_KEY_ERROR\",\n data: { slug }\n });\n }\n },\n\n async listApiKeys({ where: { tenant }, sort }) {\n await ensureApiKeysTable();\n\n try {\n const rows = await apiKeysQuery().where(\"tenant\", tenant);\n const items = rows.map(row => JSON.parse(row.data) as StorageApiKey);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list api keys.\",\n code: \"LIST_API_KEY_ERROR\"\n });\n }\n },\n\n async createApiKey({ apiKey }) {\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery().insert({\n id: apiKey.id,\n tenant: apiKey.tenant,\n token: apiKey.token,\n slug: apiKey.slug,\n data: JSON.stringify(apiKey)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create api key.\",\n code: \"CREATE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n },\n\n async updateApiKey({ apiKey }) {\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery()\n .where(\"tenant\", apiKey.tenant)\n .andWhere(\"id\", apiKey.id)\n .update({\n token: apiKey.token,\n slug: apiKey.slug,\n data: JSON.stringify(apiKey)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update api key.\",\n code: \"UPDATE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n },\n\n async deleteApiKey({ apiKey }) {\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery()\n .where(\"tenant\", apiKey.tenant)\n .andWhere(\"id\", apiKey.id)\n .delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete api key.\",\n code: \"DELETE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n }\n };\n};\n"],"names":["ROLES_TABLE","TEAMS_TABLE","API_KEYS_TABLE","createStorageOperations","params","knex","tableManager","ensureRolesTable","table","ensureTeamsTable","ensureApiKeysTable","rolesQuery","teamsQuery","apiKeysQuery","tenant","id","slug","row","JSON","err","WebinyError","id_in","slug_in","sort","qb","Array","rows","items","sortItems","role","team","token","apiKey"],"mappings":";;AAWA,MAAMA,cAAc;AACpB,MAAMC,cAAc;AACpB,MAAMC,iBAAiB;AA6BhB,MAAMC,0BAA0B,CACnCC;IAEA,MAAM,EAAEC,IAAI,EAAEC,YAAY,EAAE,GAAGF;IAE/B,MAAMG,mBAAmBD,aAAa,MAAM,CAACN,aAAaQ,CAAAA;QACtDA,MAAM,IAAI,CAAC,MAAM,WAAW;QAC5BA,MAAM,IAAI,CAAC,UAAU,WAAW;QAChCA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAC9BA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAE9BA,MAAM,OAAO,CAAC;YAAC;YAAU;SAAK;QAC9BA,MAAM,MAAM,CAAC;YAAC;YAAU;SAAO;IACnC;IAEA,MAAMC,mBAAmBH,aAAa,MAAM,CAACL,aAAaO,CAAAA;QACtDA,MAAM,IAAI,CAAC,MAAM,WAAW;QAC5BA,MAAM,IAAI,CAAC,UAAU,WAAW;QAChCA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAC9BA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAE9BA,MAAM,OAAO,CAAC;YAAC;YAAU;SAAK;Q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,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAEa;oBAAO;gBACnB;YACJ;QACJ;IACJ;AACJ"}
|
|
1
|
+
{"version":3,"file":"security/index.js","sources":["../../src/security/index.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type {\n SecurityStorageOperations,\n StorageApiKey,\n StorageRole,\n StorageTeam\n} from \"@webiny/api-core/types/security.js\";\nimport WebinyError from \"@webiny/error\";\nimport type { ScopeTenant } from \"@webiny/api-core/features/tenancy/ScopeTenant/index.js\";\nimport type { TableManager } from \"~/TableManager.js\";\nimport { sortItems } from \"~/sortItems.js\";\n\nconst ROLES_TABLE = \"webiny_core_roles\";\nconst TEAMS_TABLE = \"webiny_core_teams\";\nconst API_KEYS_TABLE = \"webiny_core_api_keys\";\n\ninterface IRoleRow {\n id: string;\n tenant: string;\n slug: string;\n data: string;\n}\n\ninterface ITeamRow {\n id: string;\n tenant: string;\n slug: string;\n data: string;\n}\n\ninterface IApiKeyRow {\n id: string;\n tenant: string;\n token: string;\n slug: string;\n data: string;\n}\n\ninterface CreateStorageOperationsParams {\n knex: Knex;\n tableManager: TableManager;\n /** The tenant the storage acts on: its scope's. */\n scopeTenant: ScopeTenant.Interface;\n}\n\nexport const createStorageOperations = (\n params: CreateStorageOperationsParams\n): SecurityStorageOperations => {\n const { knex, tableManager, scopeTenant } = params;\n\n /** The record with its scope's tenant, whatever tenant it carries. */\n const inScope = <T extends { tenant?: string | null }>(record: T): T & { tenant: string } => {\n return { ...record, tenant: scopeTenant.getId() };\n };\n\n const ensureRolesTable = tableManager.define(ROLES_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n\n const ensureTeamsTable = tableManager.define(TEAMS_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n\n const ensureApiKeysTable = tableManager.define(API_KEYS_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"token\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"token\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n\n const rolesQuery = () => knex<IRoleRow>(tableManager.resolve(ROLES_TABLE));\n const teamsQuery = () => knex<ITeamRow>(tableManager.resolve(TEAMS_TABLE));\n const apiKeysQuery = () => knex<IApiKeyRow>(tableManager.resolve(API_KEYS_TABLE));\n\n return {\n /* Roles */\n async getRole({ where: { tenant, id, slug } }) {\n tenant = scopeTenant.getId();\n await ensureRolesTable();\n\n try {\n if (id) {\n const row = await rolesQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"id\", id)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageRole) : null;\n }\n\n const row = await rolesQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug as string)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageRole) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load role.\",\n code: \"GET_ROLE_ERROR\",\n data: { id, slug }\n });\n }\n },\n\n async listRoles({ where: { tenant, id_in, slug_in }, sort }) {\n tenant = scopeTenant.getId();\n await ensureRolesTable();\n\n try {\n const qb = rolesQuery().where(\"tenant\", tenant);\n\n if (Array.isArray(id_in)) {\n qb.whereIn(\"id\", id_in);\n } else if (Array.isArray(slug_in)) {\n qb.whereIn(\"slug\", slug_in);\n }\n\n const rows = await qb;\n const items = rows.map(row => JSON.parse(row.data) as StorageRole);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list roles.\",\n code: \"LIST_ROLE_ERROR\"\n });\n }\n },\n\n async createRole({ role }) {\n role = inScope(role);\n await ensureRolesTable();\n\n try {\n await rolesQuery().insert({\n id: role.id,\n tenant: role.tenant,\n slug: role.slug,\n data: JSON.stringify(role)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create role.\",\n code: \"CREATE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n async updateRole({ role }) {\n role = inScope(role);\n await ensureRolesTable();\n\n try {\n await rolesQuery()\n .where(\"tenant\", role.tenant)\n .andWhere(\"id\", role.id)\n .update({\n slug: role.slug,\n data: JSON.stringify(role)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update role.\",\n code: \"UPDATE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n async deleteRole({ role }) {\n role = inScope(role);\n await ensureRolesTable();\n\n try {\n await rolesQuery().where(\"tenant\", role.tenant).andWhere(\"id\", role.id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete role.\",\n code: \"DELETE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n /* Teams */\n async getTeam({ where: { tenant, id, slug } }) {\n tenant = scopeTenant.getId();\n await ensureTeamsTable();\n\n try {\n if (id) {\n const row = await teamsQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"id\", id)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageTeam) : null;\n }\n\n const row = await teamsQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug as string)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageTeam) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load team.\",\n code: \"GET_TEAM_ERROR\",\n data: { id, slug }\n });\n }\n },\n\n async listTeams({ where: { tenant, id_in, slug_in }, sort }) {\n tenant = scopeTenant.getId();\n await ensureTeamsTable();\n\n try {\n const qb = teamsQuery().where(\"tenant\", tenant);\n\n if (Array.isArray(id_in)) {\n qb.whereIn(\"id\", id_in);\n } else if (Array.isArray(slug_in)) {\n qb.whereIn(\"slug\", slug_in);\n }\n\n const rows = await qb;\n const items = rows.map(row => JSON.parse(row.data) as StorageTeam);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list teams.\",\n code: \"LIST_TEAM_ERROR\"\n });\n }\n },\n\n async createTeam({ team }) {\n team = inScope(team);\n await ensureTeamsTable();\n\n try {\n await teamsQuery().insert({\n id: team.id,\n tenant: team.tenant,\n slug: team.slug,\n data: JSON.stringify(team)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create team.\",\n code: \"CREATE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n async updateTeam({ team }) {\n team = inScope(team);\n await ensureTeamsTable();\n\n try {\n await teamsQuery()\n .where(\"tenant\", team.tenant)\n .andWhere(\"id\", team.id)\n .update({\n slug: team.slug,\n data: JSON.stringify(team)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update team.\",\n code: \"UPDATE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n async deleteTeam({ team }) {\n team = inScope(team);\n await ensureTeamsTable();\n\n try {\n await teamsQuery().where(\"tenant\", team.tenant).andWhere(\"id\", team.id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete team.\",\n code: \"DELETE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n /* API Keys */\n async getApiKey({ id, tenant }) {\n tenant = scopeTenant.getId();\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery().where(\"tenant\", tenant).andWhere(\"id\", id).first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key.\",\n code: \"GET_API_KEY_ERROR\",\n data: { id }\n });\n }\n },\n\n async getApiKeyByToken({ tenant, token }) {\n tenant = scopeTenant.getId();\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"token\", token)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key by token.\",\n code: \"GET_BY_TOKEN_API_KEY_ERROR\",\n data: { token }\n });\n }\n },\n\n async getApiKeyBySlug({ tenant, slug }) {\n tenant = scopeTenant.getId();\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key by slug.\",\n code: \"GET_BY_SLUG_API_KEY_ERROR\",\n data: { slug }\n });\n }\n },\n\n async listApiKeys({ where: { tenant }, sort }) {\n tenant = scopeTenant.getId();\n await ensureApiKeysTable();\n\n try {\n const rows = await apiKeysQuery().where(\"tenant\", tenant);\n const items = rows.map(row => JSON.parse(row.data) as StorageApiKey);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list api keys.\",\n code: \"LIST_API_KEY_ERROR\"\n });\n }\n },\n\n async createApiKey({ apiKey }) {\n apiKey = inScope(apiKey);\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery().insert({\n id: apiKey.id,\n tenant: apiKey.tenant,\n token: apiKey.token,\n slug: apiKey.slug,\n data: JSON.stringify(apiKey)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create api key.\",\n code: \"CREATE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n },\n\n async updateApiKey({ apiKey }) {\n apiKey = inScope(apiKey);\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery()\n .where(\"tenant\", apiKey.tenant)\n .andWhere(\"id\", apiKey.id)\n .update({\n token: apiKey.token,\n slug: apiKey.slug,\n data: JSON.stringify(apiKey)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update api key.\",\n code: \"UPDATE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n },\n\n async deleteApiKey({ apiKey }) {\n apiKey = inScope(apiKey);\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery()\n .where(\"tenant\", apiKey.tenant)\n .andWhere(\"id\", apiKey.id)\n .delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete api key.\",\n code: \"DELETE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n }\n };\n};\n"],"names":["ROLES_TABLE","TEAMS_TABLE","API_KEYS_TABLE","createStorageOperations","params","knex","tableManager","scopeTenant","inScope","record","ensureRolesTable","table","ensureTeamsTable","ensureApiKeysTable","rolesQuery","teamsQuery","apiKeysQuery","tenant","id","slug","row","JSON","err","WebinyError","id_in","slug_in","sort","qb","Array","rows","items","sortItems","role","team","token","apiKey"],"mappings":";;AAYA,MAAMA,cAAc;AACpB,MAAMC,cAAc;AACpB,MAAMC,iBAAiB;AA+BhB,MAAMC,0BAA0B,CACnCC;IAEA,MAAM,EAAEC,IAAI,EAAEC,YAAY,EAAEC,WAAW,EAAE,GAAGH;IAG5C,MAAMI,UAAU,CAAuCC,SAC5C;YAAE,GAAGA,MAAM;YAAE,QAAQF,YAAY,KAAK;QAAG;IAGpD,MAAMG,mBAAmBJ,aAAa,MAAM,CAACN,aAAaW,CAAAA;QACtDA,MAAM,IAAI,CAAC,MAAM,WAAW;QAC5BA,MAAM,IAAI,CAAC,UAAU,WAAW;QAChCA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAC9BA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAE9BA,MAAM,OAAO,CAAC;YAAC;YAAU;SAAK;QAC9BA,MAAM,MAAM,CAAC;YAAC;YAAU;SAAO;IACnC;IAEA,MAAMC,mBAAmBN,aAAa,MAAM,CAACL,aAAaU,CAAAA;QACtDA,MAAM,IAAI,CAAC,MAAM,WAAW;QAC5BA,MAAM,IAAI,CAAC,UAAU,WAAW;QAChCA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAC9BA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAE9BA,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