@webiny/api-core-sql 0.0.0-unstable.0156a4c312

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.
Files changed (47) hide show
  1. package/ApiCoreSqlFeature.d.ts +18 -0
  2. package/ApiCoreSqlFeature.js +18 -0
  3. package/ApiCoreSqlFeature.js.map +1 -0
  4. package/LICENSE +21 -0
  5. package/README.md +11 -0
  6. package/TableManager.d.ts +19 -0
  7. package/TableManager.js +46 -0
  8. package/TableManager.js.map +1 -0
  9. package/adminUsers/index.d.ts +9 -0
  10. package/adminUsers/index.js +120 -0
  11. package/adminUsers/index.js.map +1 -0
  12. package/createApiCoreSql.d.ts +8 -0
  13. package/createApiCoreSql.js +32 -0
  14. package/createApiCoreSql.js.map +1 -0
  15. package/feature/KnexClient/KnexClient.d.ts +9 -0
  16. package/feature/KnexClient/KnexClient.js +8 -0
  17. package/feature/KnexClient/KnexClient.js.map +1 -0
  18. package/feature/KnexClient/abstractions.d.ts +8 -0
  19. package/feature/KnexClient/abstractions.js +5 -0
  20. package/feature/KnexClient/abstractions.js.map +1 -0
  21. package/feature/KnexClient/index.d.ts +6 -0
  22. package/feature/KnexClient/index.js +15 -0
  23. package/feature/KnexClient/index.js.map +1 -0
  24. package/getSqlTablePrefix.d.ts +1 -0
  25. package/getSqlTablePrefix.js +4 -0
  26. package/getSqlTablePrefix.js.map +1 -0
  27. package/index.d.ts +11 -0
  28. package/index.js +12 -0
  29. package/index.js.map +1 -0
  30. package/keyValueStore/index.d.ts +9 -0
  31. package/keyValueStore/index.js +221 -0
  32. package/keyValueStore/index.js.map +1 -0
  33. package/package.json +42 -0
  34. package/security/index.d.ts +9 -0
  35. package/security/index.js +345 -0
  36. package/security/index.js.map +1 -0
  37. package/serviceDiscovery/SqlServiceManifestLoader.d.ts +13 -0
  38. package/serviceDiscovery/SqlServiceManifestLoader.js +25 -0
  39. package/serviceDiscovery/SqlServiceManifestLoader.js.map +1 -0
  40. package/serviceDiscovery/index.d.ts +1 -0
  41. package/serviceDiscovery/index.js +1 -0
  42. package/sortItems.d.ts +1 -0
  43. package/sortItems.js +31 -0
  44. package/sortItems.js.map +1 -0
  45. package/tenancy/index.d.ts +9 -0
  46. package/tenancy/index.js +111 -0
  47. package/tenancy/index.js.map +1 -0
@@ -0,0 +1,18 @@
1
+ import type { Knex } from "knex";
2
+ export interface ApiCoreSqlConfig {
3
+ knex: Knex;
4
+ tableNamePrefix?: string;
5
+ }
6
+ /**
7
+ * Registers the SQL implementation of ApiCoreStorageOperationsFactory. ApiCoreFeature.register
8
+ * resolves + builds it. Mirrors ApiCoreDdbFeature.
9
+ *
10
+ * The storage operations are built once, here, and `create()` hands out the same object. It runs on
11
+ * every request (ApiCoreFeature is per-request), and building per request created a new TableManager
12
+ * each time: every one stayed in the global test registry, so a long-running server leaked one per
13
+ * request, and each started with no verified tables. The operations hold no per-request state.
14
+ */
15
+ export declare const ApiCoreSqlFeature: {
16
+ name: string;
17
+ register(container: import("@webiny/di").Container, context: ApiCoreSqlConfig): void;
18
+ };
@@ -0,0 +1,18 @@
1
+ import { createFeature } from "@webiny/feature/api";
2
+ import { ApiCoreStorageOperationsFactory } from "@webiny/api-core";
3
+ import { createApiCoreSql } from "./createApiCoreSql.js";
4
+ const ApiCoreSqlFeature = createFeature({
5
+ name: "ApiCoreSql",
6
+ register (container, { knex, tableNamePrefix }) {
7
+ const storageOperations = createApiCoreSql({
8
+ knex,
9
+ tableNamePrefix
10
+ });
11
+ container.registerInstance(ApiCoreStorageOperationsFactory, {
12
+ create: ()=>storageOperations
13
+ });
14
+ }
15
+ });
16
+ export { ApiCoreSqlFeature };
17
+
18
+ //# sourceMappingURL=ApiCoreSqlFeature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApiCoreSqlFeature.js","sources":["../src/ApiCoreSqlFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport type { Knex } from \"knex\";\nimport { ApiCoreStorageOperationsFactory } from \"@webiny/api-core\";\nimport { createApiCoreSql } from \"./createApiCoreSql.js\";\n\nexport interface ApiCoreSqlConfig {\n knex: Knex;\n tableNamePrefix?: string;\n}\n\n/**\n * Registers the SQL implementation of ApiCoreStorageOperationsFactory. ApiCoreFeature.register\n * resolves + builds it. Mirrors ApiCoreDdbFeature.\n *\n * The storage operations are built once, here, and `create()` hands out the same object. It runs on\n * every request (ApiCoreFeature is per-request), and building per request created a new TableManager\n * each time: every one stayed in the global test registry, so a long-running server leaked one per\n * request, and each started with no verified tables. The operations hold no per-request state.\n */\nexport const ApiCoreSqlFeature = createFeature<ApiCoreSqlConfig>({\n name: \"ApiCoreSql\",\n register(container, { knex, tableNamePrefix }) {\n const storageOperations = createApiCoreSql({ knex, tableNamePrefix });\n\n container.registerInstance(ApiCoreStorageOperationsFactory, {\n create: () => storageOperations\n });\n }\n});\n"],"names":["ApiCoreSqlFeature","createFeature","container","knex","tableNamePrefix","storageOperations","createApiCoreSql","ApiCoreStorageOperationsFactory"],"mappings":";;;AAmBO,MAAMA,oBAAoBC,cAAgC;IAC7D,MAAM;IACN,UAASC,SAAS,EAAE,EAAEC,IAAI,EAAEC,eAAe,EAAE;QACzC,MAAMC,oBAAoBC,iBAAiB;YAAEH;YAAMC;QAAgB;QAEnEF,UAAU,gBAAgB,CAACK,iCAAiC;YACxD,QAAQ,IAAMF;QAClB;IACJ;AACJ"}
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # @webiny/api-core-sql
2
+
3
+ > [!NOTE]
4
+ > This package is part of the [Webiny](https://www.webiny.com) monorepo.
5
+ > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
6
+
7
+ 📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
8
+
9
+ ---
10
+
11
+ _This README file is automatically generated during the publish process._
@@ -0,0 +1,19 @@
1
+ import type { Knex } from "knex";
2
+ interface ITableManager {
3
+ reset(): void;
4
+ resolve(tableName: string): string;
5
+ ensure(tableName: string, creator: (table: Knex.CreateTableBuilder) => void): Promise<void>;
6
+ }
7
+ export declare class TableManager implements ITableManager {
8
+ private readonly knex;
9
+ private readonly prefix;
10
+ private readonly verified;
11
+ private readonly pending;
12
+ constructor(knex: Knex, prefix?: string);
13
+ reset(): void;
14
+ resolve(tableName: string): string;
15
+ ensure(tableName: string, creator: (table: Knex.CreateTableBuilder) => void): Promise<void>;
16
+ private ensureTable;
17
+ private createTable;
18
+ }
19
+ export {};
@@ -0,0 +1,46 @@
1
+ class TableManager {
2
+ constructor(knex, prefix = ""){
3
+ this.verified = new Set();
4
+ this.pending = new Map();
5
+ this.knex = knex;
6
+ this.prefix = prefix ? `${prefix}_` : "";
7
+ const g = globalThis;
8
+ const managers = g.__sqlTableManagers ??= [];
9
+ managers.push(this);
10
+ }
11
+ reset() {
12
+ this.verified.clear();
13
+ this.pending.clear();
14
+ }
15
+ resolve(tableName) {
16
+ return `${this.prefix}${tableName}`;
17
+ }
18
+ async ensure(tableName, creator) {
19
+ const resolved = this.resolve(tableName);
20
+ if (this.verified.has(resolved)) return;
21
+ const pending = this.pending.get(resolved);
22
+ if (pending) return pending;
23
+ const check = this.ensureTable(resolved, creator).finally(()=>{
24
+ if (this.pending.get(resolved) === check) this.pending.delete(resolved);
25
+ });
26
+ this.pending.set(resolved, check);
27
+ return check;
28
+ }
29
+ async ensureTable(resolved, creator) {
30
+ const exists = await this.knex.schema.hasTable(resolved);
31
+ if (!exists) await this.createTable(resolved, creator);
32
+ this.verified.add(resolved);
33
+ }
34
+ async createTable(resolved, creator) {
35
+ try {
36
+ await this.knex.schema.createTable(resolved, creator);
37
+ } catch (err) {
38
+ const exists = await this.knex.schema.hasTable(resolved);
39
+ if (exists) return;
40
+ throw err;
41
+ }
42
+ }
43
+ }
44
+ export { TableManager };
45
+
46
+ //# sourceMappingURL=TableManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TableManager.js","sources":["../src/TableManager.ts"],"sourcesContent":["import type { Knex } from \"knex\";\n\ninterface ITableManager {\n reset(): void;\n resolve(tableName: string): string;\n ensure(tableName: string, creator: (table: Knex.CreateTableBuilder) => void): Promise<void>;\n}\n\nexport class TableManager implements ITableManager {\n private readonly knex: Knex;\n private readonly prefix: string;\n private readonly verified = new Set<string>();\n private readonly pending = new Map<string, Promise<void>>();\n\n constructor(knex: Knex, prefix: string = \"\") {\n this.knex = knex;\n this.prefix = prefix ? `${prefix}_` : \"\";\n\n const g = globalThis as Record<string, unknown>;\n const managers = (g.__sqlTableManagers ??= []) as ITableManager[];\n managers.push(this);\n }\n\n public reset(): void {\n this.verified.clear();\n this.pending.clear();\n }\n\n public resolve(tableName: string): string {\n return `${this.prefix}${tableName}`;\n }\n\n public async ensure(\n tableName: string,\n creator: (table: Knex.CreateTableBuilder) => void\n ): Promise<void> {\n const resolved = this.resolve(tableName);\n\n if (this.verified.has(resolved)) {\n return;\n }\n\n // One TableManager serves every request, so concurrent first requests share a single\n // hasTable+createTable instead of racing into \"table already exists\". Cleared on failure so\n // a later call can retry.\n const pending = this.pending.get(resolved);\n if (pending) {\n return pending;\n }\n\n const check = this.ensureTable(resolved, creator).finally(() => {\n // Only remove our own entry: after reset() a newer check may own this table's slot.\n if (this.pending.get(resolved) === check) {\n this.pending.delete(resolved);\n }\n });\n this.pending.set(resolved, check);\n\n return check;\n }\n\n private async ensureTable(\n resolved: string,\n creator: (table: Knex.CreateTableBuilder) => void\n ): Promise<void> {\n const exists = await this.knex.schema.hasTable(resolved);\n\n if (!exists) {\n await this.createTable(resolved, creator);\n }\n\n this.verified.add(resolved);\n }\n\n private async createTable(\n resolved: string,\n creator: (table: Knex.CreateTableBuilder) => void\n ): Promise<void> {\n try {\n await this.knex.schema.createTable(resolved, creator);\n } catch (err) {\n // Another process (or connection) may have created it in the meantime. If it exists now,\n // that's success; otherwise the failure is real.\n const exists = await this.knex.schema.hasTable(resolved);\n if (exists) {\n return;\n }\n throw err;\n }\n }\n}\n"],"names":["TableManager","knex","prefix","Set","Map","g","globalThis","managers","tableName","creator","resolved","pending","check","exists","err"],"mappings":"AAQO,MAAMA;IAMT,YAAYC,IAAU,EAAEC,SAAiB,EAAE,CAAE;aAH5B,QAAQ,GAAG,IAAIC;aACf,OAAO,GAAG,IAAIC;QAG3B,IAAI,CAAC,IAAI,GAAGH;QACZ,IAAI,CAAC,MAAM,GAAGC,SAAS,GAAGA,OAAO,CAAC,CAAC,GAAG;QAEtC,MAAMG,IAAIC;QACV,MAAMC,WAAYF,EAAE,kBAAkB,KAAK,EAAE;QAC7CE,SAAS,IAAI,CAAC,IAAI;IACtB;IAEO,QAAc;QACjB,IAAI,CAAC,QAAQ,CAAC,KAAK;QACnB,IAAI,CAAC,OAAO,CAAC,KAAK;IACtB;IAEO,QAAQC,SAAiB,EAAU;QACtC,OAAO,GAAG,IAAI,CAAC,MAAM,GAAGA,WAAW;IACvC;IAEA,MAAa,OACTA,SAAiB,EACjBC,OAAiD,EACpC;QACb,MAAMC,WAAW,IAAI,CAAC,OAAO,CAACF;QAE9B,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACE,WAClB;QAMJ,MAAMC,UAAU,IAAI,CAAC,OAAO,CAAC,GAAG,CAACD;QACjC,IAAIC,SACA,OAAOA;QAGX,MAAMC,QAAQ,IAAI,CAAC,WAAW,CAACF,UAAUD,SAAS,OAAO,CAAC;YAEtD,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAACC,cAAcE,OAC/B,IAAI,CAAC,OAAO,CAAC,MAAM,CAACF;QAE5B;QACA,IAAI,CAAC,OAAO,CAAC,GAAG,CAACA,UAAUE;QAE3B,OAAOA;IACX;IAEA,MAAc,YACVF,QAAgB,EAChBD,OAAiD,EACpC;QACb,MAAMI,SAAS,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAACH;QAE/C,IAAI,CAACG,QACD,MAAM,IAAI,CAAC,WAAW,CAACH,UAAUD;QAGrC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACC;IACtB;IAEA,MAAc,YACVA,QAAgB,EAChBD,OAAiD,EACpC;QACb,IAAI;YACA,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAACC,UAAUD;QACjD,EAAE,OAAOK,KAAK;YAGV,MAAMD,SAAS,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAACH;YAC/C,IAAIG,QACA;YAEJ,MAAMC;QACV;IACJ;AACJ"}
@@ -0,0 +1,9 @@
1
+ import type { Knex } from "knex";
2
+ import type { AdminUsersStorageOperations } from "@webiny/api-core/types/users.js";
3
+ import type { TableManager } from "../TableManager.js";
4
+ interface CreateStorageOperationsParams {
5
+ knex: Knex;
6
+ tableManager: TableManager;
7
+ }
8
+ export declare const createStorageOperations: (params: CreateStorageOperationsParams) => AdminUsersStorageOperations;
9
+ export {};
@@ -0,0 +1,120 @@
1
+ import error from "@webiny/error";
2
+ import { sortItems } from "../sortItems.js";
3
+ const TABLE_NAME = "webiny_core_admin_users";
4
+ const userToRow = (user)=>({
5
+ id: user.id,
6
+ tenant: user.tenant,
7
+ email: user.email,
8
+ data: JSON.stringify(user)
9
+ });
10
+ const rowToUser = (row)=>JSON.parse(row.data);
11
+ const createStorageOperations = (params)=>{
12
+ const { knex, tableManager } = params;
13
+ const ensureTable = ()=>tableManager.ensure(TABLE_NAME, (table)=>{
14
+ table.text("id").notNullable();
15
+ table.text("tenant").notNullable();
16
+ table.text("email").notNullable();
17
+ table.text("data").notNullable();
18
+ table.primary([
19
+ "tenant",
20
+ "id"
21
+ ]);
22
+ table.unique([
23
+ "tenant",
24
+ "email"
25
+ ]);
26
+ });
27
+ const query = ()=>knex(tableManager.resolve(TABLE_NAME));
28
+ return {
29
+ async getUser (params) {
30
+ await ensureTable();
31
+ const { where: { tenant, id, email } } = params;
32
+ try {
33
+ if (id) {
34
+ const row = await query().where("tenant", tenant).andWhere("id", id).first();
35
+ return row ? rowToUser(row) : null;
36
+ }
37
+ const row = await query().where("tenant", tenant).andWhere("email", email).first();
38
+ return row ? rowToUser(row) : null;
39
+ } catch (err) {
40
+ throw error.from(err, {
41
+ message: "Could not load user.",
42
+ code: "GET_ADMIN_USERS_ERROR",
43
+ data: {
44
+ id,
45
+ email
46
+ }
47
+ });
48
+ }
49
+ },
50
+ async listUsers (params) {
51
+ await ensureTable();
52
+ const { where, sort } = params;
53
+ try {
54
+ const rows = await query().where("tenant", where.tenant);
55
+ let items = rows.map((row)=>rowToUser(row));
56
+ items = sortItems(items, sort);
57
+ const { id_in, teams_in } = where;
58
+ return items.filter((item)=>{
59
+ if (Array.isArray(id_in) && !id_in.includes(item.id)) return false;
60
+ if (Array.isArray(teams_in)) return (item.teams || []).some((team)=>teams_in.includes(team));
61
+ return true;
62
+ });
63
+ } catch (err) {
64
+ throw error.from(err, {
65
+ message: "Could not list users.",
66
+ code: "LIST_ADMIN_USERS_ERROR"
67
+ });
68
+ }
69
+ },
70
+ async createUser ({ user }) {
71
+ await ensureTable();
72
+ try {
73
+ const row = userToRow(user);
74
+ await query().insert(row);
75
+ return user;
76
+ } catch (err) {
77
+ throw error.from(err, {
78
+ message: "Could not create admin user.",
79
+ code: "CREATE_ADMIN_USER_ERROR",
80
+ data: {
81
+ user
82
+ }
83
+ });
84
+ }
85
+ },
86
+ async updateUser ({ user }) {
87
+ await ensureTable();
88
+ try {
89
+ const row = userToRow(user);
90
+ await query().where("tenant", user.tenant).andWhere("id", user.id).update(row);
91
+ return user;
92
+ } catch (err) {
93
+ throw error.from(err, {
94
+ message: "Could not update user.",
95
+ code: "UPDATE_ADMIN_USER_ERROR",
96
+ data: {
97
+ user
98
+ }
99
+ });
100
+ }
101
+ },
102
+ async deleteUser ({ user }) {
103
+ await ensureTable();
104
+ try {
105
+ await query().where("tenant", user.tenant).andWhere("id", user.id).delete();
106
+ } catch (err) {
107
+ throw error.from(err, {
108
+ message: "Could not delete user.",
109
+ code: "DELETE_ADMIN_USER_ERROR",
110
+ data: {
111
+ user
112
+ }
113
+ });
114
+ }
115
+ }
116
+ };
117
+ };
118
+ export { createStorageOperations };
119
+
120
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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 = () => {\n return tableManager.ensure(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\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,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gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAEd;oBAAK;gBACjB;YACJ;QACJ;IACJ;AACJ"}
@@ -0,0 +1,8 @@
1
+ import type { Knex } from "knex";
2
+ import type { ApiCoreStorageOperations } from "@webiny/api-core/types/core.js";
3
+ interface CreateApiCoreSqlParams {
4
+ knex: Knex;
5
+ tableNamePrefix?: string;
6
+ }
7
+ export declare const createApiCoreSql: ({ knex, tableNamePrefix }: CreateApiCoreSqlParams) => ApiCoreStorageOperations;
8
+ export {};
@@ -0,0 +1,32 @@
1
+ import { ServiceDiscovery } from "@webiny/api-core/features/serviceDiscovery/index.js";
2
+ import { createStorageOperations } from "./adminUsers/index.js";
3
+ import { createStorageOperations as index_js_createStorageOperations } from "./tenancy/index.js";
4
+ import { createStorageOperations as external_security_index_js_createStorageOperations } from "./security/index.js";
5
+ import { createStorageOperations as external_keyValueStore_index_js_createStorageOperations } from "./keyValueStore/index.js";
6
+ import { TableManager } from "./TableManager.js";
7
+ import { SqlServiceManifestLoader } from "./serviceDiscovery/index.js";
8
+ const createApiCoreSql = ({ knex, tableNamePrefix })=>{
9
+ const tableManager = new TableManager(knex, tableNamePrefix);
10
+ ServiceDiscovery.setLoader(new SqlServiceManifestLoader(knex, tableManager));
11
+ return {
12
+ usersStorageOperations: createStorageOperations({
13
+ knex,
14
+ tableManager
15
+ }),
16
+ tenancyStorageOperations: index_js_createStorageOperations({
17
+ knex,
18
+ tableManager
19
+ }),
20
+ securityStorageOperations: external_security_index_js_createStorageOperations({
21
+ knex,
22
+ tableManager
23
+ }),
24
+ keyValueStorageOperations: external_keyValueStore_index_js_createStorageOperations({
25
+ knex,
26
+ tableManager
27
+ })
28
+ };
29
+ };
30
+ export { createApiCoreSql };
31
+
32
+ //# sourceMappingURL=createApiCoreSql.js.map
@@ -0,0 +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 { ServiceDiscovery } from \"@webiny/api-core/features/serviceDiscovery/index.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 { SqlServiceManifestLoader } from \"./serviceDiscovery/index.js\";\n\ninterface CreateApiCoreSqlParams {\n knex: Knex;\n tableNamePrefix?: string;\n}\n\nexport const createApiCoreSql = ({\n knex,\n tableNamePrefix\n}: CreateApiCoreSqlParams): ApiCoreStorageOperations => {\n const tableManager = new TableManager(knex, tableNamePrefix);\n\n ServiceDiscovery.setLoader(new SqlServiceManifestLoader(knex, tableManager));\n\n return {\n usersStorageOperations: createUsersStorageOperations({\n knex,\n tableManager\n }),\n tenancyStorageOperations: createTenancyStorageOperations({\n knex,\n tableManager\n }),\n securityStorageOperations: createSecurityStorageOperations({\n knex,\n tableManager\n }),\n keyValueStorageOperations: createKeyValueStorageOperations({\n knex,\n tableManager\n })\n };\n};\n"],"names":["createApiCoreSql","knex","tableNamePrefix","tableManager","TableManager","ServiceDiscovery","SqlServiceManifestLoader","createUsersStorageOperations","createTenancyStorageOperations","createSecurityStorageOperations","createKeyValueStorageOperations"],"mappings":";;;;;;;AAeO,MAAMA,mBAAmB,CAAC,EAC7BC,IAAI,EACJC,eAAe,EACM;IACrB,MAAMC,eAAe,IAAIC,aAAaH,MAAMC;IAE5CG,iBAAiB,SAAS,CAAC,IAAIC,yBAAyBL,MAAME;IAE9D,OAAO;QACH,wBAAwBI,wBAA6B;YACjDN;YACAE;QACJ;QACA,0BAA0BK,iCAA+B;YACrDP;YACAE;QACJ;QACA,2BAA2BM,mDAAgC;YACvDR;YACAE;QACJ;QACA,2BAA2BO,wDAAgC;YACvDT;YACAE;QACJ;IACJ;AACJ"}
@@ -0,0 +1,9 @@
1
+ import type { Knex } from "knex";
2
+ import type { KnexClient as Abstraction } from "./abstractions.js";
3
+ export interface IKnexClientParams {
4
+ knex: Knex;
5
+ }
6
+ export declare class KnexClient implements Abstraction.Interface {
7
+ readonly client: Knex<any, any[]>;
8
+ constructor(params: IKnexClientParams);
9
+ }
@@ -0,0 +1,8 @@
1
+ class KnexClient {
2
+ constructor(params){
3
+ this.client = params.knex;
4
+ }
5
+ }
6
+ export { KnexClient };
7
+
8
+ //# sourceMappingURL=KnexClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feature/KnexClient/KnexClient.js","sources":["../../../src/feature/KnexClient/KnexClient.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type { KnexClient as Abstraction } from \"./abstractions.js\";\n\nexport interface IKnexClientParams {\n knex: Knex;\n}\n\nexport class KnexClient implements Abstraction.Interface {\n public readonly client;\n\n public constructor(params: IKnexClientParams) {\n this.client = params.knex;\n }\n}\n"],"names":["KnexClient","params"],"mappings":"AAOO,MAAMA;IAGT,YAAmBC,MAAyB,CAAE;QAC1C,IAAI,CAAC,MAAM,GAAGA,OAAO,IAAI;IAC7B;AACJ"}
@@ -0,0 +1,8 @@
1
+ import type { Knex } from "knex";
2
+ export interface IKnexClient {
3
+ readonly client: Knex;
4
+ }
5
+ export declare const KnexClient: import("@webiny/di").Abstraction<IKnexClient>;
6
+ export declare namespace KnexClient {
7
+ type Interface = IKnexClient;
8
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const KnexClient = createAbstraction("Db/Sql/KnexClient");
3
+ export { KnexClient };
4
+
5
+ //# sourceMappingURL=abstractions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feature/KnexClient/abstractions.js","sources":["../../../src/feature/KnexClient/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Knex } from \"knex\";\n\nexport interface IKnexClient {\n readonly client: Knex;\n}\n\nexport const KnexClient = createAbstraction<IKnexClient>(\"Db/Sql/KnexClient\");\n\nexport namespace KnexClient {\n export type Interface = IKnexClient;\n}\n"],"names":["KnexClient","createAbstraction"],"mappings":";AAOO,MAAMA,aAAaC,kBAA+B"}
@@ -0,0 +1,6 @@
1
+ import type { Knex } from "knex";
2
+ export { KnexClient } from "./abstractions.js";
3
+ export declare const KnexClientFeature: {
4
+ name: string;
5
+ register(container: import("@webiny/di").Container, context: Knex<any, any[]>): void;
6
+ };
@@ -0,0 +1,15 @@
1
+ import { createFeature } from "@webiny/feature/api";
2
+ import { KnexClient } from "./abstractions.js";
3
+ import { KnexClient as external_KnexClient_js_KnexClient } from "./KnexClient.js";
4
+ const KnexClientFeature = createFeature({
5
+ name: "Db/Sql/KnexClientFeature",
6
+ register (container, knex) {
7
+ const client = new external_KnexClient_js_KnexClient({
8
+ knex
9
+ });
10
+ container.registerInstance(KnexClient, client);
11
+ }
12
+ });
13
+ export { KnexClient, KnexClientFeature };
14
+
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feature/KnexClient/index.js","sources":["../../../src/feature/KnexClient/index.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport type { Knex } from \"knex\";\nimport { KnexClient as KnexClientAbstraction } from \"./abstractions.js\";\nimport { KnexClient } from \"./KnexClient.js\";\n\nexport { KnexClient } from \"./abstractions.js\";\n\nexport const KnexClientFeature = createFeature<Knex>({\n name: \"Db/Sql/KnexClientFeature\",\n register(container, knex) {\n const client = new KnexClient({ knex });\n container.registerInstance(KnexClientAbstraction, client);\n }\n});\n"],"names":["KnexClientFeature","createFeature","container","knex","client","KnexClient","KnexClientAbstraction"],"mappings":";;;AAOO,MAAMA,oBAAoBC,cAAoB;IACjD,MAAM;IACN,UAASC,SAAS,EAAEC,IAAI;QACpB,MAAMC,SAAS,IAAIC,kCAAW;YAAEF;QAAK;QACrCD,UAAU,gBAAgB,CAACI,YAAuBF;IACtD;AACJ"}
@@ -0,0 +1 @@
1
+ export declare const getSqlTablePrefix: () => string;
@@ -0,0 +1,4 @@
1
+ const getSqlTablePrefix = ()=>process.env.SQL_TABLE_PREFIX || process.env.WEBINY_SQL_TABLE_PREFIX || "";
2
+ export { getSqlTablePrefix };
3
+
4
+ //# sourceMappingURL=getSqlTablePrefix.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getSqlTablePrefix.js","sources":["../src/getSqlTablePrefix.ts"],"sourcesContent":["export const getSqlTablePrefix = (): string => {\n return process.env.SQL_TABLE_PREFIX || process.env.WEBINY_SQL_TABLE_PREFIX || \"\";\n};\n"],"names":["getSqlTablePrefix","process"],"mappings":"AAAO,MAAMA,oBAAoB,IACtBC,QAAQ,GAAG,CAAC,gBAAgB,IAAIA,QAAQ,GAAG,CAAC,uBAAuB,IAAI"}
package/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { Knex } from "knex";
2
+ export { createApiCoreSql } from "./createApiCoreSql.js";
3
+ export { ApiCoreSqlFeature } from "./ApiCoreSqlFeature.js";
4
+ export type { ApiCoreSqlConfig } from "./ApiCoreSqlFeature.js";
5
+ export { getSqlTablePrefix } from "./getSqlTablePrefix.js";
6
+ export { SqlServiceManifestLoader } from "./serviceDiscovery/index.js";
7
+ export { KnexClient } from "./feature/KnexClient/index.js";
8
+ interface IRegisterApiCoreSqlExtension {
9
+ knex: Knex;
10
+ }
11
+ export declare const registerSQLCore: ({ knex }: IRegisterApiCoreSqlExtension) => import("@webiny/handler").RegisterExtensionPlugin<import("@webiny/handler/types.js").Context>;
package/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import { createRegisterExtensionPlugin } from "@webiny/handler";
2
+ import { KnexClient, KnexClientFeature } from "./feature/KnexClient/index.js";
3
+ const registerSQLCore = ({ knex })=>createRegisterExtensionPlugin(async (context)=>{
4
+ KnexClientFeature.register(context.container, knex);
5
+ });
6
+ export { createApiCoreSql } from "./createApiCoreSql.js";
7
+ export { ApiCoreSqlFeature } from "./ApiCoreSqlFeature.js";
8
+ export { getSqlTablePrefix } from "./getSqlTablePrefix.js";
9
+ export { SqlServiceManifestLoader } from "./serviceDiscovery/index.js";
10
+ export { KnexClient, registerSQLCore };
11
+
12
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +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 \"./ApiCoreSqlFeature.js\";\nexport type { ApiCoreSqlConfig } from \"./ApiCoreSqlFeature.js\";\nexport { getSqlTablePrefix } from \"./getSqlTablePrefix.js\";\nexport { SqlServiceManifestLoader } 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":";;AAgBO,MAAMA,kBAAkB,CAAC,EAAEC,IAAI,EAAgC,GAC3DC,8BAA8B,OAAMC;QACvCC,kBAAkB,QAAQ,CAACD,QAAQ,SAAS,EAAEF;IAClD"}
@@ -0,0 +1,9 @@
1
+ import type { Knex } from "knex";
2
+ import type { IKeyValueStorageOperations } from "@webiny/api-core/features/keyValueStore/abstractions.js";
3
+ import type { TableManager } from "../TableManager.js";
4
+ interface CreateStorageOperationsParams {
5
+ knex: Knex;
6
+ tableManager: TableManager;
7
+ }
8
+ export declare const createStorageOperations: (params: CreateStorageOperationsParams) => IKeyValueStorageOperations;
9
+ export {};