@webiny/api-core-sql 0.0.0-unstable.33e4fd9647 → 0.0.0-unstable.48a4e5b6c1

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 (107) hide show
  1. package/TableManager.d.ts +30 -12
  2. package/TableManager.js +34 -30
  3. package/TableManager.js.map +1 -1
  4. package/adminUsers/index.d.ts +3 -0
  5. package/adminUsers/index.js +74 -19
  6. package/adminUsers/index.js.map +1 -1
  7. package/connection/SqlConnectionFeature.d.ts +32 -0
  8. package/connection/SqlConnectionFeature.js +30 -0
  9. package/connection/SqlConnectionFeature.js.map +1 -0
  10. package/connection/index.d.ts +2 -0
  11. package/connection/index.js +1 -0
  12. package/connectionRetry/index.d.ts +3 -0
  13. package/connectionRetry/index.js +2 -0
  14. package/connectionRetry/isConnectionError.d.ts +1 -0
  15. package/connectionRetry/isConnectionError.js +28 -0
  16. package/connectionRetry/isConnectionError.js.map +1 -0
  17. package/connectionRetry/withPgConnectionRetry.d.ts +49 -0
  18. package/connectionRetry/withPgConnectionRetry.js +175 -0
  19. package/connectionRetry/withPgConnectionRetry.js.map +1 -0
  20. package/coreStorage/CoreSqlSchemaSetup.d.ts +18 -0
  21. package/coreStorage/CoreSqlSchemaSetup.js +36 -0
  22. package/coreStorage/CoreSqlSchemaSetup.js.map +1 -0
  23. package/coreStorage/SqlApiCoreStorageOperationsFactory.d.ts +15 -0
  24. package/coreStorage/SqlApiCoreStorageOperationsFactory.js +33 -0
  25. package/coreStorage/SqlApiCoreStorageOperationsFactory.js.map +1 -0
  26. package/coreStorage/SqlTenancyStorageOperationsFactory.d.ts +18 -0
  27. package/coreStorage/SqlTenancyStorageOperationsFactory.js +36 -0
  28. package/coreStorage/SqlTenancyStorageOperationsFactory.js.map +1 -0
  29. package/coreStorage/SqlTenantStorageReadiness.d.ts +19 -0
  30. package/coreStorage/SqlTenantStorageReadiness.js +48 -0
  31. package/coreStorage/SqlTenantStorageReadiness.js.map +1 -0
  32. package/coreStorage/feature.d.ts +14 -0
  33. package/coreStorage/feature.js +17 -0
  34. package/coreStorage/feature.js.map +1 -0
  35. package/coreStorage/index.d.ts +1 -0
  36. package/coreStorage/index.js +1 -0
  37. package/createApiCoreSql.d.ts +19 -1
  38. package/createApiCoreSql.js +12 -10
  39. package/createApiCoreSql.js.map +1 -1
  40. package/feature/SqlDialect/abstractions.d.ts +15 -0
  41. package/feature/SqlDialect/abstractions.js +5 -0
  42. package/feature/SqlDialect/abstractions.js.map +1 -0
  43. package/feature/SqlDialect/index.d.ts +2 -0
  44. package/feature/SqlDialect/index.js +1 -0
  45. package/index.d.ts +26 -4
  46. package/index.js +18 -4
  47. package/index.js.map +1 -1
  48. package/keyValueStore/index.js +19 -17
  49. package/keyValueStore/index.js.map +1 -1
  50. package/package.json +9 -8
  51. package/schemaConfig/abstractions.d.ts +22 -0
  52. package/schemaConfig/abstractions.js +5 -0
  53. package/schemaConfig/abstractions.js.map +1 -0
  54. package/schemaConfig/index.d.ts +1 -0
  55. package/schemaConfig/index.js +1 -0
  56. package/schemaMemory/ensureTable.d.ts +35 -0
  57. package/schemaMemory/ensureTable.js +46 -0
  58. package/schemaMemory/ensureTable.js.map +1 -0
  59. package/schemaMemory/getConnectionKey.d.ts +8 -0
  60. package/schemaMemory/getConnectionKey.js +15 -0
  61. package/schemaMemory/getConnectionKey.js.map +1 -0
  62. package/schemaMemory/getSchemaMemory.d.ts +37 -0
  63. package/schemaMemory/getSchemaMemory.js +53 -0
  64. package/schemaMemory/getSchemaMemory.js.map +1 -0
  65. package/schemaSetup/abstractions.d.ts +79 -0
  66. package/schemaSetup/abstractions.js +6 -0
  67. package/schemaSetup/abstractions.js.map +1 -0
  68. package/schemaSetup/index.d.ts +6 -0
  69. package/schemaSetup/index.js +3 -0
  70. package/schemaSetup/runSchemaSetupSteps.d.ts +43 -0
  71. package/schemaSetup/runSchemaSetupSteps.js +39 -0
  72. package/schemaSetup/runSchemaSetupSteps.js.map +1 -0
  73. package/schemaSetup/runSqlSchemaSetups.d.ts +22 -0
  74. package/schemaSetup/runSqlSchemaSetups.js +24 -0
  75. package/schemaSetup/runSqlSchemaSetups.js.map +1 -0
  76. package/scopeStorage/abstractions.d.ts +19 -0
  77. package/scopeStorage/abstractions.js +5 -0
  78. package/scopeStorage/abstractions.js.map +1 -0
  79. package/scopeStorage/index.d.ts +3 -0
  80. package/scopeStorage/index.js +2 -0
  81. package/scopeStorage/isPostgresScope.d.ts +14 -0
  82. package/scopeStorage/isPostgresScope.js +8 -0
  83. package/scopeStorage/isPostgresScope.js.map +1 -0
  84. package/security/index.d.ts +3 -0
  85. package/security/index.js +68 -47
  86. package/security/index.js.map +1 -1
  87. package/serviceDiscovery/SqlServiceManifestLoader.d.ts +1 -1
  88. package/serviceDiscovery/SqlServiceManifestLoader.js +3 -8
  89. package/serviceDiscovery/SqlServiceManifestLoader.js.map +1 -1
  90. package/serviceDiscovery/SqlServiceManifestWriter.d.ts +16 -0
  91. package/serviceDiscovery/SqlServiceManifestWriter.js +27 -0
  92. package/serviceDiscovery/SqlServiceManifestWriter.js.map +1 -0
  93. package/serviceDiscovery/index.d.ts +1 -0
  94. package/serviceDiscovery/index.js +1 -0
  95. package/serviceDiscovery/serviceManifestsTable.d.ts +13 -0
  96. package/serviceDiscovery/serviceManifestsTable.js +8 -0
  97. package/serviceDiscovery/serviceManifestsTable.js.map +1 -0
  98. package/storage/sqlTenantStorage.d.ts +44 -0
  99. package/storage/sqlTenantStorage.js +32 -0
  100. package/storage/sqlTenantStorage.js.map +1 -0
  101. package/storage/types.d.ts +31 -0
  102. package/storage/types.js +0 -0
  103. package/tenancy/index.js +59 -17
  104. package/tenancy/index.js.map +1 -1
  105. package/ApiCoreSqlFeature.d.ts +0 -18
  106. package/ApiCoreSqlFeature.js +0 -18
  107. package/ApiCoreSqlFeature.js.map +0 -1
@@ -0,0 +1,79 @@
1
+ import type { Knex } from "knex";
2
+ import type { Container } from "@webiny/feature/api";
3
+ import type { TenantStorageBackend } from "@webiny/api-core/features/tenancy/TenantStorages/abstractions.js";
4
+ export interface ISqlSchemaSetup {
5
+ /**
6
+ * What the step sets up, as a short id (`core`, `websockets`, `cms`, `aco`). The runner reports
7
+ * the steps it ran by these names, and can check that a module it needs registered its step.
8
+ */
9
+ readonly name: string;
10
+ /**
11
+ * Brings the step's tables, indexes and functions up to date, whatever the schema-managed
12
+ * setting. Safe to run again: what already exists is kept. It never drops a table or a column,
13
+ * but a step may migrate in place. The Postgres CMS, for one, adds columns
14
+ * (`ALTER TABLE … ADD COLUMN IF NOT EXISTS`), changes column statistics, replaces its trigger
15
+ * functions, replaces the old count triggers and rebuilds the count rows from the search table
16
+ * once. The database user that runs setup needs the rights for that.
17
+ */
18
+ setup(): Promise<void>;
19
+ }
20
+ /**
21
+ * One step of the schema setup. Every part that owns tables registers its own step in a request's
22
+ * container: the storage's parts (core, audit logs, websockets), the transport's login storage, and
23
+ * the modules registered through the project's extensions (the Postgres CMS, the folder catalog).
24
+ * The setup runner runs every step it finds, so it doesn't need to know which modules exist.
25
+ */
26
+ export declare const SqlSchemaSetup: import("@webiny/di").Abstraction<ISqlSchemaSetup>;
27
+ export declare namespace SqlSchemaSetup {
28
+ type Interface = ISqlSchemaSetup;
29
+ }
30
+ export interface ISqlSchemaSetupRequestOptions {
31
+ /**
32
+ * The storage to register, as a tenant scope on it would: an extra storage's backend, bound to
33
+ * the setup's connection. Default: the default storage's.
34
+ */
35
+ backend?: TenantStorageBackend.Interface;
36
+ }
37
+ /**
38
+ * What a request registers, as the transport builds every request's container: its storages (every
39
+ * `RequestStorage`), the API's features and the project's extensions. The schema setup builds its
40
+ * container with it, so it sets up exactly the tables the API uses, with their settings.
41
+ */
42
+ export interface ISqlSchemaSetupRequest {
43
+ register(container: Container, options?: ISqlSchemaSetupRequestOptions): Promise<void>;
44
+ }
45
+ export interface SqlSchemaSetupRunParams {
46
+ /** Registers a request's parts in the setup's container. */
47
+ request: ISqlSchemaSetupRequest;
48
+ /**
49
+ * The connection to set the schema up on. Defaults to the storage's. The caller closes it.
50
+ * On Lambda, setup goes to the database's writer, not through the proxy the API uses.
51
+ */
52
+ knex?: Knex;
53
+ }
54
+ export interface ISqlSchemaSetupRunner {
55
+ /** The id of the storage it sets up: `default`, or an extra storage's. Default: `default`. */
56
+ readonly storage?: string;
57
+ /**
58
+ * Creates every table, index and function the API uses: opens the setup's transaction (on
59
+ * Postgres, behind an advisory lock), builds a container on it the way a request's is built,
60
+ * and runs every `SqlSchemaSetup` step in it. Keeps what exists, and is safe to run on every
61
+ * start or deploy (see `ISqlSchemaSetup.setup` for what a step may migrate). Afterwards the storage is schema-managed. Returns the names of the steps it
62
+ * ran.
63
+ */
64
+ run(params: SqlSchemaSetupRunParams): Promise<string[]>;
65
+ }
66
+ /**
67
+ * Runs the schema setup of one SQL storage. Each storage registers its implementation in the root,
68
+ * the default storage and every extra one (`runSqlSchemaSetups` runs them all):
69
+ * it knows how its engine runs DDL safely (a transaction behind an advisory lock on Postgres, a plain
70
+ * transaction on SQLite) and which modules it can't run without. The transport runs it before the
71
+ * API takes traffic: the standalone server at start, the Lambda API on its schema-setup event.
72
+ */
73
+ export declare const SqlSchemaSetupRunner: import("@webiny/di").Abstraction<ISqlSchemaSetupRunner>;
74
+ export declare namespace SqlSchemaSetupRunner {
75
+ type Interface = ISqlSchemaSetupRunner;
76
+ type Request = ISqlSchemaSetupRequest;
77
+ type RequestOptions = ISqlSchemaSetupRequestOptions;
78
+ type RunParams = SqlSchemaSetupRunParams;
79
+ }
@@ -0,0 +1,6 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const SqlSchemaSetup = createAbstraction("Db/Sql/SchemaSetup");
3
+ const SqlSchemaSetupRunner = createAbstraction("Db/Sql/SchemaSetupRunner");
4
+ export { SqlSchemaSetup, SqlSchemaSetupRunner };
5
+
6
+ //# sourceMappingURL=abstractions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemaSetup/abstractions.js","sources":["../../src/schemaSetup/abstractions.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type { Container } from \"@webiny/feature/api\";\nimport { createAbstraction } from \"@webiny/feature/api\";\n// A deep import: the package root pulls in all of api-core.\nimport type { TenantStorageBackend } from \"@webiny/api-core/features/tenancy/TenantStorages/abstractions.js\";\n\nexport interface ISqlSchemaSetup {\n /**\n * What the step sets up, as a short id (`core`, `websockets`, `cms`, `aco`). The runner reports\n * the steps it ran by these names, and can check that a module it needs registered its step.\n */\n readonly name: string;\n /**\n * Brings the step's tables, indexes and functions up to date, whatever the schema-managed\n * setting. Safe to run again: what already exists is kept. It never drops a table or a column,\n * but a step may migrate in place. The Postgres CMS, for one, adds columns\n * (`ALTER TABLE … ADD COLUMN IF NOT EXISTS`), changes column statistics, replaces its trigger\n * functions, replaces the old count triggers and rebuilds the count rows from the search table\n * once. The database user that runs setup needs the rights for that.\n */\n setup(): Promise<void>;\n}\n\n/**\n * One step of the schema setup. Every part that owns tables registers its own step in a request's\n * container: the storage's parts (core, audit logs, websockets), the transport's login storage, and\n * the modules registered through the project's extensions (the Postgres CMS, the folder catalog).\n * The setup runner runs every step it finds, so it doesn't need to know which modules exist.\n */\nexport const SqlSchemaSetup = createAbstraction<ISqlSchemaSetup>(\"Db/Sql/SchemaSetup\");\n\nexport namespace SqlSchemaSetup {\n export type Interface = ISqlSchemaSetup;\n}\n\nexport interface ISqlSchemaSetupRequestOptions {\n /**\n * The storage to register, as a tenant scope on it would: an extra storage's backend, bound to\n * the setup's connection. Default: the default storage's.\n */\n backend?: TenantStorageBackend.Interface;\n}\n\n/**\n * What a request registers, as the transport builds every request's container: its storages (every\n * `RequestStorage`), the API's features and the project's extensions. The schema setup builds its\n * container with it, so it sets up exactly the tables the API uses, with their settings.\n */\nexport interface ISqlSchemaSetupRequest {\n register(container: Container, options?: ISqlSchemaSetupRequestOptions): Promise<void>;\n}\n\nexport interface SqlSchemaSetupRunParams {\n /** Registers a request's parts in the setup's container. */\n request: ISqlSchemaSetupRequest;\n /**\n * The connection to set the schema up on. Defaults to the storage's. The caller closes it.\n * On Lambda, setup goes to the database's writer, not through the proxy the API uses.\n */\n knex?: Knex;\n}\n\nexport interface ISqlSchemaSetupRunner {\n /** The id of the storage it sets up: `default`, or an extra storage's. Default: `default`. */\n readonly storage?: string;\n /**\n * Creates every table, index and function the API uses: opens the setup's transaction (on\n * Postgres, behind an advisory lock), builds a container on it the way a request's is built,\n * and runs every `SqlSchemaSetup` step in it. Keeps what exists, and is safe to run on every\n * start or deploy (see `ISqlSchemaSetup.setup` for what a step may migrate). Afterwards the storage is schema-managed. Returns the names of the steps it\n * ran.\n */\n run(params: SqlSchemaSetupRunParams): Promise<string[]>;\n}\n\n/**\n * Runs the schema setup of one SQL storage. Each storage registers its implementation in the root,\n * the default storage and every extra one (`runSqlSchemaSetups` runs them all):\n * it knows how its engine runs DDL safely (a transaction behind an advisory lock on Postgres, a plain\n * transaction on SQLite) and which modules it can't run without. The transport runs it before the\n * API takes traffic: the standalone server at start, the Lambda API on its schema-setup event.\n */\nexport const SqlSchemaSetupRunner = createAbstraction<ISqlSchemaSetupRunner>(\n \"Db/Sql/SchemaSetupRunner\"\n);\n\nexport namespace SqlSchemaSetupRunner {\n export type Interface = ISqlSchemaSetupRunner;\n export type Request = ISqlSchemaSetupRequest;\n export type RequestOptions = ISqlSchemaSetupRequestOptions;\n export type RunParams = SqlSchemaSetupRunParams;\n}\n"],"names":["SqlSchemaSetup","createAbstraction","SqlSchemaSetupRunner"],"mappings":";AA6BO,MAAMA,iBAAiBC,kBAAmC;AAqD1D,MAAMC,uBAAuBD,kBAChC"}
@@ -0,0 +1,6 @@
1
+ export { SqlSchemaSetup, SqlSchemaSetupRunner } from "./abstractions.js";
2
+ export type { ISqlSchemaSetupRequest, ISqlSchemaSetupRequestOptions, SqlSchemaSetupRunParams } from "./abstractions.js";
3
+ export { runSqlSchemaSetups } from "./runSqlSchemaSetups.js";
4
+ export type { RunSqlSchemaSetupsParams, SqlSchemaSetupResult } from "./runSqlSchemaSetups.js";
5
+ export { runSchemaSetupSteps } from "./runSchemaSetupSteps.js";
6
+ export type { RequiredSqlSchemaSetup, RunSchemaSetupStepsParams } from "./runSchemaSetupSteps.js";
@@ -0,0 +1,3 @@
1
+ export { SqlSchemaSetup, SqlSchemaSetupRunner } from "./abstractions.js";
2
+ export { runSqlSchemaSetups } from "./runSqlSchemaSetups.js";
3
+ export { runSchemaSetupSteps } from "./runSchemaSetupSteps.js";
@@ -0,0 +1,43 @@
1
+ import type { Knex } from "knex";
2
+ import type { Container } from "@webiny/feature/api";
3
+ import type { SqlDialect } from "../feature/SqlDialect/index.js";
4
+ import type { TenantStorageBackend } from "@webiny/api-core/features/tenancy/TenantStorages/abstractions.js";
5
+ import { type ISqlSchemaSetupRequest } from "./abstractions.js";
6
+ export interface RequiredSqlSchemaSetup {
7
+ /** The step's name (`SqlSchemaSetup.name`). */
8
+ name: string;
9
+ /** The error when no step of that name is registered: what to add to the project. */
10
+ message: string;
11
+ }
12
+ export interface RunSchemaSetupStepsParams {
13
+ /** The root container the storage is registered in. The setup's container is its child. */
14
+ root: Container;
15
+ /** The setup's connection: a transaction the runner opened. */
16
+ knex: Knex;
17
+ /** The connection's database family. */
18
+ dialect: SqlDialect.Name;
19
+ tableNamePrefix?: string;
20
+ /** Registers a request's parts in the setup's container. */
21
+ request: ISqlSchemaSetupRequest;
22
+ /** Steps the storage can't run without (a module the project must add). */
23
+ required?: RequiredSqlSchemaSetup[];
24
+ /**
25
+ * An extra storage's backend, bound to the setup's connection: the request registers it instead
26
+ * of the default storage's, so the setup creates the tables of the parts a scope on it has.
27
+ */
28
+ backend?: TenantStorageBackend.Interface;
29
+ }
30
+ /**
31
+ * The part of schema setup every SQL storage shares, run inside the runner's transaction. It builds
32
+ * a container on the setup's connection the way a request's is built, and runs every schema setup
33
+ * step in it.
34
+ *
35
+ * The container is a child of the root, so it has what a request has (the logger, the storages'
36
+ * `RequestStorage`). The setup's connection is registered in it first, with schema-managed off:
37
+ * every storage the request registers runs on it, including the ones built while the request
38
+ * registers and the modules' singletons, which are built in the container that registers them.
39
+ *
40
+ * Checks `required` before running anything, so a missing module fails before any DDL. Returns the
41
+ * names of the steps it ran, in order.
42
+ */
43
+ export declare function runSchemaSetupSteps(params: RunSchemaSetupStepsParams): Promise<string[]>;
@@ -0,0 +1,39 @@
1
+ import { SqlConnectionFeature } from "../connection/SqlConnectionFeature.js";
2
+ import { SqlSchemaSetup } from "./abstractions.js";
3
+ async function runSchemaSetupSteps(params) {
4
+ const { root, knex, dialect, tableNamePrefix, request, required = [], backend } = params;
5
+ const container = root.createChildContainer();
6
+ SqlConnectionFeature.register(container, {
7
+ knex,
8
+ dialect,
9
+ tableNamePrefix,
10
+ schemaManaged: false
11
+ });
12
+ if (backend) await request.register(container, {
13
+ backend
14
+ });
15
+ else await request.register(container);
16
+ const steps = resolveSteps(container, required);
17
+ for (const step of required)if (!steps.some((item)=>item.name === step.name)) throw new Error(step.message);
18
+ const done = [];
19
+ for (const step of steps){
20
+ await step.setup();
21
+ done.push(step.name);
22
+ }
23
+ return done;
24
+ }
25
+ function resolveSteps(container, required) {
26
+ try {
27
+ return container.resolveAll(SqlSchemaSetup);
28
+ } catch (error) {
29
+ if (!required.length) throw error;
30
+ const cause = error instanceof Error ? error.message : String(error);
31
+ const needs = required.map((step)=>step.message).join(" ");
32
+ throw new Error(`Schema setup could not build its steps (${cause}). ${needs}`, {
33
+ cause: error
34
+ });
35
+ }
36
+ }
37
+ export { runSchemaSetupSteps };
38
+
39
+ //# sourceMappingURL=runSchemaSetupSteps.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemaSetup/runSchemaSetupSteps.js","sources":["../../src/schemaSetup/runSchemaSetupSteps.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type { Container } from \"@webiny/feature/api\";\nimport { SqlConnectionFeature } from \"~/connection/SqlConnectionFeature.js\";\nimport type { SqlDialect } from \"~/feature/SqlDialect/index.js\";\nimport type { TenantStorageBackend } from \"@webiny/api-core/features/tenancy/TenantStorages/abstractions.js\";\nimport { SqlSchemaSetup, type ISqlSchemaSetupRequest } from \"./abstractions.js\";\n\nexport interface RequiredSqlSchemaSetup {\n /** The step's name (`SqlSchemaSetup.name`). */\n name: string;\n /** The error when no step of that name is registered: what to add to the project. */\n message: string;\n}\n\nexport interface RunSchemaSetupStepsParams {\n /** The root container the storage is registered in. The setup's container is its child. */\n root: Container;\n /** The setup's connection: a transaction the runner opened. */\n knex: Knex;\n /** The connection's database family. */\n dialect: SqlDialect.Name;\n tableNamePrefix?: string;\n /** Registers a request's parts in the setup's container. */\n request: ISqlSchemaSetupRequest;\n /** Steps the storage can't run without (a module the project must add). */\n required?: RequiredSqlSchemaSetup[];\n /**\n * An extra storage's backend, bound to the setup's connection: the request registers it instead\n * of the default storage's, so the setup creates the tables of the parts a scope on it has.\n */\n backend?: TenantStorageBackend.Interface;\n}\n\n/**\n * The part of schema setup every SQL storage shares, run inside the runner's transaction. It builds\n * a container on the setup's connection the way a request's is built, and runs every schema setup\n * step in it.\n *\n * The container is a child of the root, so it has what a request has (the logger, the storages'\n * `RequestStorage`). The setup's connection is registered in it first, with schema-managed off:\n * every storage the request registers runs on it, including the ones built while the request\n * registers and the modules' singletons, which are built in the container that registers them.\n *\n * Checks `required` before running anything, so a missing module fails before any DDL. Returns the\n * names of the steps it ran, in order.\n */\nexport async function runSchemaSetupSteps(params: RunSchemaSetupStepsParams): Promise<string[]> {\n const { root, knex, dialect, tableNamePrefix, request, required = [], backend } = params;\n\n const container = root.createChildContainer();\n SqlConnectionFeature.register(container, {\n knex,\n dialect,\n tableNamePrefix,\n schemaManaged: false\n });\n if (backend) {\n await request.register(container, { backend });\n } else {\n await request.register(container);\n }\n\n const steps = resolveSteps(container, required);\n for (const step of required) {\n if (!steps.some(item => item.name === step.name)) {\n throw new Error(step.message);\n }\n }\n\n const done: string[] = [];\n for (const step of steps) {\n await step.setup();\n done.push(step.name);\n }\n return done;\n}\n\n/**\n * Builds the steps. A step whose module needs another one that is missing (the folder catalog needs\n * the CMS's table names) can't be built, and that fails before any step has a name to check. So\n * the failure says which modules the storage needs, with the cause.\n */\nfunction resolveSteps(container: Container, required: RequiredSqlSchemaSetup[]) {\n try {\n return container.resolveAll(SqlSchemaSetup);\n } catch (error) {\n if (!required.length) {\n throw error;\n }\n const cause = error instanceof Error ? error.message : String(error);\n const needs = required.map(step => step.message).join(\" \");\n throw new Error(`Schema setup could not build its steps (${cause}). ${needs}`, {\n cause: error\n });\n }\n}\n"],"names":["runSchemaSetupSteps","params","root","knex","dialect","tableNamePrefix","request","required","backend","container","SqlConnectionFeature","steps","resolveSteps","step","item","Error","done","SqlSchemaSetup","error","cause","String","needs"],"mappings":";;AA8CO,eAAeA,oBAAoBC,MAAiC;IACvE,MAAM,EAAEC,IAAI,EAAEC,IAAI,EAAEC,OAAO,EAAEC,eAAe,EAAEC,OAAO,EAAEC,WAAW,EAAE,EAAEC,OAAO,EAAE,GAAGP;IAElF,MAAMQ,YAAYP,KAAK,oBAAoB;IAC3CQ,qBAAqB,QAAQ,CAACD,WAAW;QACrCN;QACAC;QACAC;QACA,eAAe;IACnB;IACA,IAAIG,SACA,MAAMF,QAAQ,QAAQ,CAACG,WAAW;QAAED;IAAQ;SAE5C,MAAMF,QAAQ,QAAQ,CAACG;IAG3B,MAAME,QAAQC,aAAaH,WAAWF;IACtC,KAAK,MAAMM,QAAQN,SACf,IAAI,CAACI,MAAM,IAAI,CAACG,CAAAA,OAAQA,KAAK,IAAI,KAAKD,KAAK,IAAI,GAC3C,MAAM,IAAIE,MAAMF,KAAK,OAAO;IAIpC,MAAMG,OAAiB,EAAE;IACzB,KAAK,MAAMH,QAAQF,MAAO;QACtB,MAAME,KAAK,KAAK;QAChBG,KAAK,IAAI,CAACH,KAAK,IAAI;IACvB;IACA,OAAOG;AACX;AAOA,SAASJ,aAAaH,SAAoB,EAAEF,QAAkC;IAC1E,IAAI;QACA,OAAOE,UAAU,UAAU,CAACQ;IAChC,EAAE,OAAOC,OAAO;QACZ,IAAI,CAACX,SAAS,MAAM,EAChB,MAAMW;QAEV,MAAMC,QAAQD,iBAAiBH,QAAQG,MAAM,OAAO,GAAGE,OAAOF;QAC9D,MAAMG,QAAQd,SAAS,GAAG,CAACM,CAAAA,OAAQA,KAAK,OAAO,EAAE,IAAI,CAAC;QACtD,MAAM,IAAIE,MAAM,CAAC,wCAAwC,EAAEI,MAAM,GAAG,EAAEE,OAAO,EAAE;YAC3E,OAAOH;QACX;IACJ;AACJ"}
@@ -0,0 +1,22 @@
1
+ import type { Container } from "@webiny/feature/api";
2
+ import { type ISqlSchemaSetupRequest } from "./abstractions.js";
3
+ export interface RunSqlSchemaSetupsParams {
4
+ /** The root container the storages are registered in. */
5
+ root: Container;
6
+ /** Registers a request's parts in each setup's container. */
7
+ request: ISqlSchemaSetupRequest;
8
+ }
9
+ export interface SqlSchemaSetupResult {
10
+ /** The storage's id: `default`, or an extra storage's. */
11
+ storage: string;
12
+ /** The names of the steps its setup ran, in order. */
13
+ steps: string[];
14
+ }
15
+ /**
16
+ * Runs the schema setup of every SQL storage registered in the root: the default storage first, then
17
+ * each extra one in the order it was registered. Each storage's runner opens its own transaction on
18
+ * its own connection (behind its own advisory lock on Postgres), so a storage's setup never waits for
19
+ * another's. The first failure stops the rest: a server must not start with a storage whose tables
20
+ * may be missing.
21
+ */
22
+ export declare function runSqlSchemaSetups(params: RunSqlSchemaSetupsParams): Promise<SqlSchemaSetupResult[]>;
@@ -0,0 +1,24 @@
1
+ import { TenantStorageId } from "@webiny/api-core/features/tenancy/TenantStorages/TenantStorageId.js";
2
+ import { SqlSchemaSetupRunner } from "./abstractions.js";
3
+ async function runSqlSchemaSetups(params) {
4
+ const runners = params.root.resolveAll(SqlSchemaSetupRunner);
5
+ const storageOf = (runner)=>TenantStorageId.from(runner.storage);
6
+ const ordered = [
7
+ ...runners.filter((runner)=>storageOf(runner).isDefault()),
8
+ ...runners.filter((runner)=>!storageOf(runner).isDefault())
9
+ ];
10
+ const results = [];
11
+ for (const runner of ordered){
12
+ const steps = await runner.run({
13
+ request: params.request
14
+ });
15
+ results.push({
16
+ storage: storageOf(runner).toString(),
17
+ steps
18
+ });
19
+ }
20
+ return results;
21
+ }
22
+ export { runSqlSchemaSetups };
23
+
24
+ //# sourceMappingURL=runSqlSchemaSetups.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schemaSetup/runSqlSchemaSetups.js","sources":["../../src/schemaSetup/runSqlSchemaSetups.ts"],"sourcesContent":["import type { Container } from \"@webiny/feature/api\";\nimport { TenantStorageId } from \"@webiny/api-core/features/tenancy/TenantStorages/TenantStorageId.js\";\nimport { SqlSchemaSetupRunner, type ISqlSchemaSetupRequest } from \"./abstractions.js\";\n\nexport interface RunSqlSchemaSetupsParams {\n /** The root container the storages are registered in. */\n root: Container;\n /** Registers a request's parts in each setup's container. */\n request: ISqlSchemaSetupRequest;\n}\n\nexport interface SqlSchemaSetupResult {\n /** The storage's id: `default`, or an extra storage's. */\n storage: string;\n /** The names of the steps its setup ran, in order. */\n steps: string[];\n}\n\n/**\n * Runs the schema setup of every SQL storage registered in the root: the default storage first, then\n * each extra one in the order it was registered. Each storage's runner opens its own transaction on\n * its own connection (behind its own advisory lock on Postgres), so a storage's setup never waits for\n * another's. The first failure stops the rest: a server must not start with a storage whose tables\n * may be missing.\n */\nexport async function runSqlSchemaSetups(\n params: RunSqlSchemaSetupsParams\n): Promise<SqlSchemaSetupResult[]> {\n const runners = params.root.resolveAll(SqlSchemaSetupRunner);\n const storageOf = (runner: SqlSchemaSetupRunner.Interface) => {\n return TenantStorageId.from(runner.storage);\n };\n const ordered = [\n ...runners.filter(runner => storageOf(runner).isDefault()),\n ...runners.filter(runner => !storageOf(runner).isDefault())\n ];\n\n const results: SqlSchemaSetupResult[] = [];\n for (const runner of ordered) {\n const steps = await runner.run({ request: params.request });\n results.push({ storage: storageOf(runner).toString(), steps });\n }\n return results;\n}\n"],"names":["runSqlSchemaSetups","params","runners","SqlSchemaSetupRunner","storageOf","runner","TenantStorageId","ordered","results","steps"],"mappings":";;AAyBO,eAAeA,mBAClBC,MAAgC;IAEhC,MAAMC,UAAUD,OAAO,IAAI,CAAC,UAAU,CAACE;IACvC,MAAMC,YAAY,CAACC,SACRC,gBAAgB,IAAI,CAACD,OAAO,OAAO;IAE9C,MAAME,UAAU;WACTL,QAAQ,MAAM,CAACG,CAAAA,SAAUD,UAAUC,QAAQ,SAAS;WACpDH,QAAQ,MAAM,CAACG,CAAAA,SAAU,CAACD,UAAUC,QAAQ,SAAS;KAC3D;IAED,MAAMG,UAAkC,EAAE;IAC1C,KAAK,MAAMH,UAAUE,QAAS;QAC1B,MAAME,QAAQ,MAAMJ,OAAO,GAAG,CAAC;YAAE,SAASJ,OAAO,OAAO;QAAC;QACzDO,QAAQ,IAAI,CAAC;YAAE,SAASJ,UAAUC,QAAQ,QAAQ;YAAII;QAAM;IAChE;IACA,OAAOD;AACX"}
@@ -0,0 +1,19 @@
1
+ import type { Container } from "@webiny/feature/api";
2
+ export interface ISqlScopeStorage {
3
+ /**
4
+ * Registers a part's storage in a tenant scope on a SQL storage, on the scope's connection and
5
+ * table settings (`KnexClient`, `SqlSchemaConfig`), which the storage registered first.
6
+ */
7
+ register(container: Container): void | Promise<void>;
8
+ }
9
+ /**
10
+ * A part of the API that keeps its data on whichever SQL storage a tenant runs on, added by the
11
+ * transport rather than by the storage: the standalone server's login (credentials, reset codes).
12
+ * Registered in the root, once per process. Every SQL storage, the default and each extra one,
13
+ * registers all of them in each tenant scope it serves, after its own storage parts. A scope on a
14
+ * storage that isn't SQL gets none.
15
+ */
16
+ export declare const SqlScopeStorage: import("@webiny/di").Abstraction<ISqlScopeStorage>;
17
+ export declare namespace SqlScopeStorage {
18
+ type Interface = ISqlScopeStorage;
19
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const SqlScopeStorage = createAbstraction("Db/Sql/ScopeStorage");
3
+ export { SqlScopeStorage };
4
+
5
+ //# sourceMappingURL=abstractions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scopeStorage/abstractions.js","sources":["../../src/scopeStorage/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { Container } from \"@webiny/feature/api\";\n\nexport interface ISqlScopeStorage {\n /**\n * Registers a part's storage in a tenant scope on a SQL storage, on the scope's connection and\n * table settings (`KnexClient`, `SqlSchemaConfig`), which the storage registered first.\n */\n register(container: Container): void | Promise<void>;\n}\n\n/**\n * A part of the API that keeps its data on whichever SQL storage a tenant runs on, added by the\n * transport rather than by the storage: the standalone server's login (credentials, reset codes).\n * Registered in the root, once per process. Every SQL storage, the default and each extra one,\n * registers all of them in each tenant scope it serves, after its own storage parts. A scope on a\n * storage that isn't SQL gets none.\n */\nexport const SqlScopeStorage = createAbstraction<ISqlScopeStorage>(\"Db/Sql/ScopeStorage\");\n\nexport namespace SqlScopeStorage {\n export type Interface = ISqlScopeStorage;\n}\n"],"names":["SqlScopeStorage","createAbstraction"],"mappings":";AAkBO,MAAMA,kBAAkBC,kBAAoC"}
@@ -0,0 +1,3 @@
1
+ export { SqlScopeStorage } from "./abstractions.js";
2
+ export type { ISqlScopeStorage } from "./abstractions.js";
3
+ export { isPostgresScope } from "./isPostgresScope.js";
@@ -0,0 +1,2 @@
1
+ export { SqlScopeStorage } from "./abstractions.js";
2
+ export { isPostgresScope } from "./isPostgresScope.js";
@@ -0,0 +1,14 @@
1
+ import type { Container } from "@webiny/feature/api";
2
+ /**
3
+ * Whether the container's SQL connection is Postgres: the connection of the tenant scope's storage,
4
+ * or of the schema setup the container was built for. `false` when the container has no SQL
5
+ * connection at all (a scope on DynamoDB).
6
+ *
7
+ * Reads the `SqlDialect` the storage registered with its connection. A scope on an extra storage
8
+ * registers its own over the default storage's in the root, and `resolve` returns the nearest.
9
+ *
10
+ * Modules that only run on Postgres (`<HeadlessCmsPg />`, `<AcoPg />`) come with the project's
11
+ * extensions, which register in every scope. A deploy whose default storage is SQLite or DynamoDB
12
+ * and that declares an extra Postgres storage registers them only where this is `true`.
13
+ */
14
+ export declare function isPostgresScope(container: Container): boolean;
@@ -0,0 +1,8 @@
1
+ import { SqlDialect } from "../feature/SqlDialect/index.js";
2
+ function isPostgresScope(container) {
3
+ if (0 === container.resolveAll(SqlDialect).length) return false;
4
+ return "postgres" === container.resolve(SqlDialect).name;
5
+ }
6
+ export { isPostgresScope };
7
+
8
+ //# sourceMappingURL=isPostgresScope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scopeStorage/isPostgresScope.js","sources":["../../src/scopeStorage/isPostgresScope.ts"],"sourcesContent":["import type { Container } from \"@webiny/feature/api\";\nimport { SqlDialect } from \"~/feature/SqlDialect/index.js\";\n\n/**\n * Whether the container's SQL connection is Postgres: the connection of the tenant scope's storage,\n * or of the schema setup the container was built for. `false` when the container has no SQL\n * connection at all (a scope on DynamoDB).\n *\n * Reads the `SqlDialect` the storage registered with its connection. A scope on an extra storage\n * registers its own over the default storage's in the root, and `resolve` returns the nearest.\n *\n * Modules that only run on Postgres (`<HeadlessCmsPg />`, `<AcoPg />`) come with the project's\n * extensions, which register in every scope. A deploy whose default storage is SQLite or DynamoDB\n * and that declares an extra Postgres storage registers them only where this is `true`.\n */\nexport function isPostgresScope(container: Container): boolean {\n if (container.resolveAll(SqlDialect).length === 0) {\n return false;\n }\n return container.resolve(SqlDialect).name === \"postgres\";\n}\n"],"names":["isPostgresScope","container","SqlDialect"],"mappings":";AAeO,SAASA,gBAAgBC,SAAoB;IAChD,IAAIA,AAA4C,MAA5CA,UAAU,UAAU,CAACC,YAAY,MAAM,EACvC,OAAO;IAEX,OAAOD,AAAuC,eAAvCA,UAAU,OAAO,CAACC,YAAY,IAAI;AAC7C"}
@@ -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,59 +4,64 @@ 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;
8
- const ensureRolesTable = ()=>tableManager.ensure(ROLES_TABLE, (table)=>{
9
- table.text("id").notNullable();
10
- table.text("tenant").notNullable();
11
- table.text("slug").notNullable();
12
- table.text("data").notNullable();
13
- table.primary([
14
- "tenant",
15
- "id"
16
- ]);
17
- table.unique([
18
- "tenant",
19
- "slug"
20
- ]);
21
- });
22
- const ensureTeamsTable = ()=>tableManager.ensure(TEAMS_TABLE, (table)=>{
23
- table.text("id").notNullable();
24
- table.text("tenant").notNullable();
25
- table.text("slug").notNullable();
26
- table.text("data").notNullable();
27
- table.primary([
28
- "tenant",
29
- "id"
30
- ]);
31
- table.unique([
32
- "tenant",
33
- "slug"
34
- ]);
35
- });
36
- const ensureApiKeysTable = ()=>tableManager.ensure(API_KEYS_TABLE, (table)=>{
37
- table.text("id").notNullable();
38
- table.text("tenant").notNullable();
39
- table.text("token").notNullable();
40
- table.text("slug").notNullable();
41
- table.text("data").notNullable();
42
- table.primary([
43
- "tenant",
44
- "id"
45
- ]);
46
- table.unique([
47
- "tenant",
48
- "token"
49
- ]);
50
- table.unique([
51
- "tenant",
52
- "slug"
53
- ]);
7
+ const { knex, tableManager, scopeTenant } = params;
8
+ const inScope = (record)=>({
9
+ ...record,
10
+ tenant: scopeTenant.getId()
54
11
  });
12
+ const ensureRolesTable = tableManager.define(ROLES_TABLE, (table)=>{
13
+ table.text("id").notNullable();
14
+ table.text("tenant").notNullable();
15
+ table.text("slug").notNullable();
16
+ table.text("data").notNullable();
17
+ table.primary([
18
+ "tenant",
19
+ "id"
20
+ ]);
21
+ table.unique([
22
+ "tenant",
23
+ "slug"
24
+ ]);
25
+ });
26
+ const ensureTeamsTable = tableManager.define(TEAMS_TABLE, (table)=>{
27
+ table.text("id").notNullable();
28
+ table.text("tenant").notNullable();
29
+ table.text("slug").notNullable();
30
+ table.text("data").notNullable();
31
+ table.primary([
32
+ "tenant",
33
+ "id"
34
+ ]);
35
+ table.unique([
36
+ "tenant",
37
+ "slug"
38
+ ]);
39
+ });
40
+ const ensureApiKeysTable = tableManager.define(API_KEYS_TABLE, (table)=>{
41
+ table.text("id").notNullable();
42
+ table.text("tenant").notNullable();
43
+ table.text("token").notNullable();
44
+ table.text("slug").notNullable();
45
+ table.text("data").notNullable();
46
+ table.primary([
47
+ "tenant",
48
+ "id"
49
+ ]);
50
+ table.unique([
51
+ "tenant",
52
+ "token"
53
+ ]);
54
+ table.unique([
55
+ "tenant",
56
+ "slug"
57
+ ]);
58
+ });
55
59
  const rolesQuery = ()=>knex(tableManager.resolve(ROLES_TABLE));
56
60
  const teamsQuery = ()=>knex(tableManager.resolve(TEAMS_TABLE));
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();