@webiny/api-core-sql 6.6.0-alpha.12 → 6.6.0-alpha.14

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 (65) hide show
  1. package/TableManager.d.ts +5 -4
  2. package/TableManager.js +11 -7
  3. package/TableManager.js.map +1 -1
  4. package/adminUsers/index.d.ts +3 -0
  5. package/adminUsers/index.js +55 -3
  6. package/adminUsers/index.js.map +1 -1
  7. package/connection/SqlConnectionFeature.d.ts +10 -6
  8. package/connection/SqlConnectionFeature.js +5 -1
  9. package/connection/SqlConnectionFeature.js.map +1 -1
  10. package/coreStorage/CoreSqlSchemaSetup.js +2 -1
  11. package/coreStorage/CoreSqlSchemaSetup.js.map +1 -1
  12. package/coreStorage/SqlApiCoreStorageOperationsFactory.d.ts +1 -1
  13. package/coreStorage/SqlApiCoreStorageOperationsFactory.js +2 -1
  14. package/coreStorage/SqlApiCoreStorageOperationsFactory.js.map +1 -1
  15. package/coreStorage/SqlTenancyStorageOperationsFactory.d.ts +18 -0
  16. package/coreStorage/SqlTenancyStorageOperationsFactory.js +36 -0
  17. package/coreStorage/SqlTenancyStorageOperationsFactory.js.map +1 -0
  18. package/coreStorage/SqlTenantStorageReadiness.d.ts +19 -0
  19. package/coreStorage/SqlTenantStorageReadiness.js +48 -0
  20. package/coreStorage/SqlTenantStorageReadiness.js.map +1 -0
  21. package/coreStorage/feature.d.ts +4 -3
  22. package/coreStorage/feature.js +4 -0
  23. package/coreStorage/feature.js.map +1 -1
  24. package/createApiCoreSql.d.ts +8 -2
  25. package/createApiCoreSql.js +7 -5
  26. package/createApiCoreSql.js.map +1 -1
  27. package/feature/SqlDialect/abstractions.d.ts +15 -0
  28. package/feature/SqlDialect/abstractions.js +5 -0
  29. package/feature/SqlDialect/abstractions.js.map +1 -0
  30. package/feature/SqlDialect/index.d.ts +2 -0
  31. package/feature/SqlDialect/index.js +1 -0
  32. package/index.d.ts +13 -4
  33. package/index.js +9 -3
  34. package/index.js.map +1 -1
  35. package/package.json +8 -8
  36. package/schemaMemory/ensureTable.d.ts +10 -3
  37. package/schemaMemory/ensureTable.js +7 -4
  38. package/schemaMemory/ensureTable.js.map +1 -1
  39. package/schemaSetup/abstractions.d.ts +14 -2
  40. package/schemaSetup/abstractions.js.map +1 -1
  41. package/schemaSetup/index.d.ts +3 -1
  42. package/schemaSetup/index.js +1 -0
  43. package/schemaSetup/runSchemaSetupSteps.d.ts +9 -0
  44. package/schemaSetup/runSchemaSetupSteps.js +6 -2
  45. package/schemaSetup/runSchemaSetupSteps.js.map +1 -1
  46. package/schemaSetup/runSqlSchemaSetups.d.ts +22 -0
  47. package/schemaSetup/runSqlSchemaSetups.js +24 -0
  48. package/schemaSetup/runSqlSchemaSetups.js.map +1 -0
  49. package/scopeStorage/abstractions.d.ts +19 -0
  50. package/scopeStorage/abstractions.js +5 -0
  51. package/scopeStorage/abstractions.js.map +1 -0
  52. package/scopeStorage/index.d.ts +3 -0
  53. package/scopeStorage/index.js +2 -0
  54. package/scopeStorage/isPostgresScope.d.ts +14 -0
  55. package/scopeStorage/isPostgresScope.js +8 -0
  56. package/scopeStorage/isPostgresScope.js.map +1 -0
  57. package/security/index.d.ts +3 -0
  58. package/security/index.js +22 -1
  59. package/security/index.js.map +1 -1
  60. package/storage/sqlTenantStorage.d.ts +44 -0
  61. package/storage/sqlTenantStorage.js +32 -0
  62. package/storage/sqlTenantStorage.js.map +1 -0
  63. package/storage/types.d.ts +14 -5
  64. package/tenancy/index.js +50 -8
  65. package/tenancy/index.js.map +1 -1
@@ -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 {};
@@ -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
 
@@ -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({ knex, tableManager }),\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}: CreateApiCoreSqlParams): ApiCoreStorageOperations => {\n const tableManager = new TableManager(knex, tableNamePrefix, { schemaManaged });\n return createApiCoreSqlOperations(knex, tableManager);\n};\n"],"names":["createApiCoreSqlOperations","knex","tableManager","createUsersStorageOperations","createTenancyStorageOperations","createSecurityStorageOperations","createKeyValueStorageOperations","createApiCoreSql","tableNamePrefix","schemaManaged","TableManager"],"mappings":";;;;;AAmBO,MAAMA,6BAA6B,CACtCC,MACAC,eAEO;QACH,wBAAwBC,wBAA6B;YAAEF;YAAMC;QAAa;QAC1E,0BAA0BE,iCAA+B;YAAEH;YAAMC;QAAa;QAC9E,2BAA2BG,mDAAgC;YAAEJ;YAAMC;QAAa;QAChF,2BAA2BI,wDAAgC;YAAEL;YAAMC;QAAa;IACpF;AAOG,MAAMK,mBAAmB,CAAC,EAC7BN,IAAI,EACJO,eAAe,EACfC,aAAa,EACQ;IACrB,MAAMP,eAAe,IAAIQ,aAAaT,MAAMO,iBAAiB;QAAEC;IAAc;IAC7E,OAAOT,2BAA2BC,MAAMC;AAC5C"}
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"}
@@ -0,0 +1,15 @@
1
+ /** The SQL database families the API runs on. */
2
+ export type SqlDialectName = "postgres" | "sqlite";
3
+ export interface ISqlDialect {
4
+ /** The database family of the connection (`KnexClient`) registered next to it. */
5
+ readonly name: SqlDialectName;
6
+ }
7
+ /**
8
+ * Which database the SQL connection talks to. Registered with the connection (`SqlConnectionFeature`),
9
+ * by the storage that knows it, so code that branches on the database never asks the driver.
10
+ */
11
+ export declare const SqlDialect: import("@webiny/di").Abstraction<ISqlDialect>;
12
+ export declare namespace SqlDialect {
13
+ type Interface = ISqlDialect;
14
+ type Name = SqlDialectName;
15
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const SqlDialect = createAbstraction("Db/Sql/SqlDialect");
3
+ export { SqlDialect };
4
+
5
+ //# sourceMappingURL=abstractions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feature/SqlDialect/abstractions.js","sources":["../../../src/feature/SqlDialect/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\n\n/** The SQL database families the API runs on. */\nexport type SqlDialectName = \"postgres\" | \"sqlite\";\n\nexport interface ISqlDialect {\n /** The database family of the connection (`KnexClient`) registered next to it. */\n readonly name: SqlDialectName;\n}\n\n/**\n * Which database the SQL connection talks to. Registered with the connection (`SqlConnectionFeature`),\n * by the storage that knows it, so code that branches on the database never asks the driver.\n */\nexport const SqlDialect = createAbstraction<ISqlDialect>(\"Db/Sql/SqlDialect\");\n\nexport namespace SqlDialect {\n export type Interface = ISqlDialect;\n export type Name = SqlDialectName;\n}\n"],"names":["SqlDialect","createAbstraction"],"mappings":";AAcO,MAAMA,aAAaC,kBAA+B"}
@@ -0,0 +1,2 @@
1
+ export { SqlDialect } from "./abstractions.js";
2
+ export type { ISqlDialect, SqlDialectName } from "./abstractions.js";
@@ -0,0 +1 @@
1
+ export { SqlDialect } from "./abstractions.js";
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { Knex } from "knex";
2
+ import { SqlDialect } from "./feature/SqlDialect/index.js";
2
3
  export { createApiCoreSql } from "./createApiCoreSql.js";
3
4
  export { ApiCoreSqlFeature } from "./coreStorage/index.js";
4
5
  export { SqlConnectionFeature, setSqlServiceManifestLoader } from "./connection/index.js";
@@ -7,18 +8,26 @@ export { getSqlTablePrefix } from "./getSqlTablePrefix.js";
7
8
  export { TableManager } from "./TableManager.js";
8
9
  export type { TableManagerOptions } from "./TableManager.js";
9
10
  export { SqlSchemaConfig } from "./schemaConfig/index.js";
10
- export { SqlSchemaSetup, SqlSchemaSetupRunner, runSchemaSetupSteps } from "./schemaSetup/index.js";
11
- export type { ISqlSchemaSetupRequest, RequiredSqlSchemaSetup, RunSchemaSetupStepsParams, SqlSchemaSetupRunParams } from "./schemaSetup/index.js";
11
+ export { SqlSchemaSetup, SqlSchemaSetupRunner, runSchemaSetupSteps, runSqlSchemaSetups } from "./schemaSetup/index.js";
12
+ export type { ISqlSchemaSetupRequest, ISqlSchemaSetupRequestOptions, RunSqlSchemaSetupsParams, SqlSchemaSetupResult, RequiredSqlSchemaSetup, RunSchemaSetupStepsParams, SqlSchemaSetupRunParams } from "./schemaSetup/index.js";
12
13
  export { getSchemaMemory } from "./schemaMemory/getSchemaMemory.js";
13
14
  export { getConnectionKey } from "./schemaMemory/getConnectionKey.js";
14
15
  export { ensureTable, createTableIfMissing, recordTable } from "./schemaMemory/ensureTable.js";
15
- export type { EnsureTableParams, TableCreator } from "./schemaMemory/ensureTable.js";
16
+ export type { EnsureTableParams, TableCreator, TableUpgrade } from "./schemaMemory/ensureTable.js";
16
17
  export type { ISchemaMemory } from "./schemaMemory/getSchemaMemory.js";
17
18
  export type { SqlStorage } from "./storage/types.js";
19
+ export { createSqlRequestStorage, createSqlTenantStorageBackend, registerSqlScopeStorages, registerTenantStorageBackend } from "./storage/sqlTenantStorage.js";
20
+ export type { CreateSqlTenantStorageBackendParams, RegisterSqlStorageParts, SqlStorageIdentityParams } from "./storage/sqlTenantStorage.js";
21
+ export { SqlScopeStorage, isPostgresScope } from "./scopeStorage/index.js";
22
+ export type { ISqlScopeStorage } from "./scopeStorage/index.js";
18
23
  export * from "./connectionRetry/index.js";
19
24
  export { SqlServiceManifestLoader, SqlServiceManifestWriter } from "./serviceDiscovery/index.js";
20
25
  export { KnexClient } from "./feature/KnexClient/index.js";
26
+ export { SqlDialect };
27
+ export type { ISqlDialect, SqlDialectName } from "./feature/SqlDialect/index.js";
21
28
  interface IRegisterApiCoreSqlExtension {
22
29
  knex: Knex;
30
+ /** The connection's database family, registered as `SqlDialect` (`isPostgresScope` reads it). */
31
+ dialect: SqlDialect.Name;
23
32
  }
24
- export declare const registerSQLCore: ({ knex }: IRegisterApiCoreSqlExtension) => import("@webiny/handler").RegisterExtensionPlugin<import("@webiny/handler/types.js").Context>;
33
+ export declare const registerSQLCore: ({ knex, dialect }: IRegisterApiCoreSqlExtension) => import("@webiny/handler").RegisterExtensionPlugin<import("@webiny/handler/types.js").Context>;
package/index.js CHANGED
@@ -1,8 +1,12 @@
1
1
  import { createRegisterExtensionPlugin } from "@webiny/handler";
2
2
  import { KnexClient, KnexClientFeature } from "./feature/KnexClient/index.js";
3
+ import { SqlDialect } from "./feature/SqlDialect/index.js";
3
4
  export * from "./connectionRetry/index.js";
4
- const registerSQLCore = ({ knex })=>createRegisterExtensionPlugin(async (context)=>{
5
+ const registerSQLCore = ({ knex, dialect })=>createRegisterExtensionPlugin(async (context)=>{
5
6
  KnexClientFeature.register(context.container, knex);
7
+ context.container.registerInstance(SqlDialect, {
8
+ name: dialect
9
+ });
6
10
  });
7
11
  export { createApiCoreSql } from "./createApiCoreSql.js";
8
12
  export { ApiCoreSqlFeature } from "./coreStorage/index.js";
@@ -10,11 +14,13 @@ export { SqlConnectionFeature, setSqlServiceManifestLoader } from "./connection/
10
14
  export { getSqlTablePrefix } from "./getSqlTablePrefix.js";
11
15
  export { TableManager } from "./TableManager.js";
12
16
  export { SqlSchemaConfig } from "./schemaConfig/index.js";
13
- export { SqlSchemaSetup, SqlSchemaSetupRunner, runSchemaSetupSteps } from "./schemaSetup/index.js";
17
+ export { SqlSchemaSetup, SqlSchemaSetupRunner, runSchemaSetupSteps, runSqlSchemaSetups } from "./schemaSetup/index.js";
14
18
  export { getSchemaMemory } from "./schemaMemory/getSchemaMemory.js";
15
19
  export { getConnectionKey } from "./schemaMemory/getConnectionKey.js";
16
20
  export { createTableIfMissing, ensureTable, recordTable } from "./schemaMemory/ensureTable.js";
21
+ export { createSqlRequestStorage, createSqlTenantStorageBackend, registerSqlScopeStorages, registerTenantStorageBackend } from "./storage/sqlTenantStorage.js";
22
+ export { SqlScopeStorage, isPostgresScope } from "./scopeStorage/index.js";
17
23
  export { SqlServiceManifestLoader, SqlServiceManifestWriter } from "./serviceDiscovery/index.js";
18
- export { KnexClient, registerSQLCore };
24
+ export { KnexClient, SqlDialect, registerSQLCore };
19
25
 
20
26
  //# sourceMappingURL=index.js.map
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\";\nimport { SqlDialect } from \"./feature/SqlDialect/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 {\n SqlSchemaSetup,\n SqlSchemaSetupRunner,\n runSchemaSetupSteps,\n runSqlSchemaSetups\n} from \"./schemaSetup/index.js\";\nexport type {\n ISqlSchemaSetupRequest,\n ISqlSchemaSetupRequestOptions,\n RunSqlSchemaSetupsParams,\n SqlSchemaSetupResult,\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 {\n createSqlRequestStorage,\n createSqlTenantStorageBackend,\n registerSqlScopeStorages,\n registerTenantStorageBackend\n} from \"./storage/sqlTenantStorage.js\";\nexport type {\n CreateSqlTenantStorageBackendParams,\n RegisterSqlStorageParts,\n SqlStorageIdentityParams\n} from \"./storage/sqlTenantStorage.js\";\nexport { SqlScopeStorage, isPostgresScope } from \"./scopeStorage/index.js\";\nexport type { ISqlScopeStorage } from \"./scopeStorage/index.js\";\nexport * from \"./connectionRetry/index.js\";\nexport { SqlServiceManifestLoader, SqlServiceManifestWriter } from \"./serviceDiscovery/index.js\";\n\nexport { KnexClient } from \"./feature/KnexClient/index.js\";\nexport { SqlDialect };\nexport type { ISqlDialect, SqlDialectName } from \"./feature/SqlDialect/index.js\";\n\ninterface IRegisterApiCoreSqlExtension {\n knex: Knex;\n /** The connection's database family, registered as `SqlDialect` (`isPostgresScope` reads it). */\n dialect: SqlDialect.Name;\n}\n\nexport const registerSQLCore = ({ knex, dialect }: IRegisterApiCoreSqlExtension) => {\n return createRegisterExtensionPlugin(async context => {\n KnexClientFeature.register(context.container, knex);\n context.container.registerInstance(SqlDialect, { name: dialect });\n });\n};\n"],"names":["registerSQLCore","knex","dialect","createRegisterExtensionPlugin","context","KnexClientFeature","SqlDialect"],"mappings":";;;;AA4DO,MAAMA,kBAAkB,CAAC,EAAEC,IAAI,EAAEC,OAAO,EAAgC,GACpEC,8BAA8B,OAAMC;QACvCC,kBAAkB,QAAQ,CAACD,QAAQ,SAAS,EAAEH;QAC9CG,QAAQ,SAAS,CAAC,gBAAgB,CAACE,YAAY;YAAE,MAAMJ;QAAQ;IACnE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/api-core-sql",
3
- "version": "6.6.0-alpha.12",
3
+ "version": "6.6.0-alpha.14",
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.12",
25
- "@webiny/error": "6.6.0-alpha.12",
26
- "@webiny/feature": "6.6.0-alpha.12",
27
- "@webiny/handler": "6.6.0-alpha.12",
24
+ "@webiny/api-core": "6.6.0-alpha.14",
25
+ "@webiny/error": "6.6.0-alpha.14",
26
+ "@webiny/feature": "6.6.0-alpha.14",
27
+ "@webiny/handler": "6.6.0-alpha.14",
28
28
  "knex": "3.3.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@webiny/build-tools": "6.6.0-alpha.12",
32
- "better-sqlite3": "13.0.3",
31
+ "@webiny/build-tools": "6.6.0-alpha.14",
32
+ "better-sqlite3": "13.2.0",
33
33
  "pg": "8.23.1",
34
34
  "typescript": "7.0.2",
35
- "vitest": "5.0.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. Another process (or connection) may create it between the
26
- * check and the create: if it exists after a failed create, that's success.
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)) return;
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\n * check and the create: if it exists after a failed create, that's success.\n */\nexport async function createTableIfMissing(\n knex: Knex,\n tableName: string,\n create: TableCreator\n): Promise<void> {\n if (await knex.schema.hasTable(tableName)) {\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","Promise","generation","key","pending","check","createTableIfMissing","recordTable","error"],"mappings":"AAkBA,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,EAAE,GAAGH;IAC5C,IAAIC,OAAO,KAAK,CAAC,GAAG,CAACC,YACjB,OAAOE,QAAQ,OAAO;IAG1B,MAAMC,aAAaJ,OAAO,UAAU;IACpC,MAAMK,MAAM,GAAGL,OAAO,GAAG,CAAC,CAAC,EAAEI,WAAW,CAAC,EAAEH,UAAU,CAAC,EAAEP,SAASC,OAAO;IACxE,MAAMW,UAAUjB,SAAS,GAAG,CAACgB;IAC7B,IAAIC,SACA,OAAOA;IAGX,MAAMC,QAAuBC,qBAAqBb,MAAMM,WAAWC,QAC9D,IAAI,CAAC;QACF,IAAIF,OAAO,UAAU,KAAKI,YACtBK,YAAYd,MAAMK,QAAQC;IAElC,GACC,OAAO,CAAC;QACL,IAAIZ,SAAS,GAAG,CAACgB,SAASE,OACtBlB,SAAS,MAAM,CAACgB;IAExB;IACJhB,SAAS,GAAG,CAACgB,KAAKE;IAClB,OAAOA;AACX;AAMO,SAASE,YAAYd,IAAU,EAAEK,MAAqB,EAAEC,SAAiB;IAC5E,IAAI,CAACN,KAAK,aAAa,EACnBK,OAAO,KAAK,CAAC,GAAG,CAACC;AAEzB;AAMO,eAAeO,qBAClBb,IAAU,EACVM,SAAiB,EACjBC,MAAoB;IAEpB,IAAI,MAAMP,KAAK,MAAM,CAAC,QAAQ,CAACM,YAC3B;IAEJ,IAAI;QACA,MAAMN,KAAK,MAAM,CAAC,WAAW,CAACM,WAAWC;IAC7C,EAAE,OAAOQ,OAAO;QACZ,IAAI,MAAMf,KAAK,MAAM,CAAC,QAAQ,CAACM,YAC3B;QAEJ,MAAMS;IACV;AACJ"}
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"}
@@ -1,5 +1,6 @@
1
1
  import type { Knex } from "knex";
2
2
  import type { Container } from "@webiny/feature/api";
3
+ import type { TenantStorageBackend } from "@webiny/api-core/features/tenancy/TenantStorages/abstractions.js";
3
4
  export interface ISqlSchemaSetup {
4
5
  /**
5
6
  * What the step sets up, as a short id (`core`, `websockets`, `cms`, `aco`). The runner reports
@@ -26,13 +27,20 @@ export declare const SqlSchemaSetup: import("@webiny/di").Abstraction<ISqlSchema
26
27
  export declare namespace SqlSchemaSetup {
27
28
  type Interface = ISqlSchemaSetup;
28
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
+ }
29
37
  /**
30
38
  * What a request registers, as the transport builds every request's container: its storages (every
31
39
  * `RequestStorage`), the API's features and the project's extensions. The schema setup builds its
32
40
  * container with it, so it sets up exactly the tables the API uses, with their settings.
33
41
  */
34
42
  export interface ISqlSchemaSetupRequest {
35
- register(container: Container): Promise<void>;
43
+ register(container: Container, options?: ISqlSchemaSetupRequestOptions): Promise<void>;
36
44
  }
37
45
  export interface SqlSchemaSetupRunParams {
38
46
  /** Registers a request's parts in the setup's container. */
@@ -44,6 +52,8 @@ export interface SqlSchemaSetupRunParams {
44
52
  knex?: Knex;
45
53
  }
46
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;
47
57
  /**
48
58
  * Creates every table, index and function the API uses: opens the setup's transaction (on
49
59
  * Postgres, behind an advisory lock), builds a container on it the way a request's is built,
@@ -54,7 +64,8 @@ export interface ISqlSchemaSetupRunner {
54
64
  run(params: SqlSchemaSetupRunParams): Promise<string[]>;
55
65
  }
56
66
  /**
57
- * Runs the schema setup of the SQL storage. Each storage registers its implementation in the root:
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):
58
69
  * it knows how its engine runs DDL safely (a transaction behind an advisory lock on Postgres, a plain
59
70
  * transaction on SQLite) and which modules it can't run without. The transport runs it before the
60
71
  * API takes traffic: the standalone server at start, the Lambda API on its schema-setup event.
@@ -63,5 +74,6 @@ export declare const SqlSchemaSetupRunner: import("@webiny/di").Abstraction<ISql
63
74
  export declare namespace SqlSchemaSetupRunner {
64
75
  type Interface = ISqlSchemaSetupRunner;
65
76
  type Request = ISqlSchemaSetupRequest;
77
+ type RequestOptions = ISqlSchemaSetupRequestOptions;
66
78
  type RunParams = SqlSchemaSetupRunParams;
67
79
  }
@@ -1 +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\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\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): 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 /**\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 the SQL storage. Each storage registers its implementation in the root:\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 RunParams = SqlSchemaSetupRunParams;\n}\n"],"names":["SqlSchemaSetup","createAbstraction","SqlSchemaSetupRunner"],"mappings":";AA2BO,MAAMA,iBAAiBC,kBAAmC;AA0C1D,MAAMC,uBAAuBD,kBAChC"}
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"}
@@ -1,4 +1,6 @@
1
1
  export { SqlSchemaSetup, SqlSchemaSetupRunner } from "./abstractions.js";
2
- export type { ISqlSchemaSetupRequest, SqlSchemaSetupRunParams } 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";
3
5
  export { runSchemaSetupSteps } from "./runSchemaSetupSteps.js";
4
6
  export type { RequiredSqlSchemaSetup, RunSchemaSetupStepsParams } from "./runSchemaSetupSteps.js";
@@ -1,2 +1,3 @@
1
1
  export { SqlSchemaSetup, SqlSchemaSetupRunner } from "./abstractions.js";
2
+ export { runSqlSchemaSetups } from "./runSqlSchemaSetups.js";
2
3
  export { runSchemaSetupSteps } from "./runSchemaSetupSteps.js";
@@ -1,5 +1,7 @@
1
1
  import type { Knex } from "knex";
2
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";
3
5
  import { type ISqlSchemaSetupRequest } from "./abstractions.js";
4
6
  export interface RequiredSqlSchemaSetup {
5
7
  /** The step's name (`SqlSchemaSetup.name`). */
@@ -12,11 +14,18 @@ export interface RunSchemaSetupStepsParams {
12
14
  root: Container;
13
15
  /** The setup's connection: a transaction the runner opened. */
14
16
  knex: Knex;
17
+ /** The connection's database family. */
18
+ dialect: SqlDialect.Name;
15
19
  tableNamePrefix?: string;
16
20
  /** Registers a request's parts in the setup's container. */
17
21
  request: ISqlSchemaSetupRequest;
18
22
  /** Steps the storage can't run without (a module the project must add). */
19
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;
20
29
  }
21
30
  /**
22
31
  * The part of schema setup every SQL storage shares, run inside the runner's transaction. It builds
@@ -1,14 +1,18 @@
1
1
  import { SqlConnectionFeature } from "../connection/SqlConnectionFeature.js";
2
2
  import { SqlSchemaSetup } from "./abstractions.js";
3
3
  async function runSchemaSetupSteps(params) {
4
- const { root, knex, tableNamePrefix, request, required = [] } = params;
4
+ const { root, knex, dialect, tableNamePrefix, request, required = [], backend } = params;
5
5
  const container = root.createChildContainer();
6
6
  SqlConnectionFeature.register(container, {
7
7
  knex,
8
+ dialect,
8
9
  tableNamePrefix,
9
10
  schemaManaged: false
10
11
  });
11
- await request.register(container);
12
+ if (backend) await request.register(container, {
13
+ backend
14
+ });
15
+ else await request.register(container);
12
16
  const steps = resolveSteps(container, required);
13
17
  for (const step of required)if (!steps.some((item)=>item.name === step.name)) throw new Error(step.message);
14
18
  const done = [];
@@ -1 +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 { 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 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\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, tableNamePrefix, request, required = [] } = params;\n\n const container = root.createChildContainer();\n SqlConnectionFeature.register(container, { knex, tableNamePrefix, schemaManaged: false });\n await request.register(container);\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","tableNamePrefix","request","required","container","SqlConnectionFeature","steps","resolveSteps","step","item","Error","done","SqlSchemaSetup","error","cause","String","needs"],"mappings":";;AAqCO,eAAeA,oBAAoBC,MAAiC;IACvE,MAAM,EAAEC,IAAI,EAAEC,IAAI,EAAEC,eAAe,EAAEC,OAAO,EAAEC,WAAW,EAAE,EAAE,GAAGL;IAEhE,MAAMM,YAAYL,KAAK,oBAAoB;IAC3CM,qBAAqB,QAAQ,CAACD,WAAW;QAAEJ;QAAMC;QAAiB,eAAe;IAAM;IACvF,MAAMC,QAAQ,QAAQ,CAACE;IAEvB,MAAME,QAAQC,aAAaH,WAAWD;IACtC,KAAK,MAAMK,QAAQL,SACf,IAAI,CAACG,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,EAAED,QAAkC;IAC1E,IAAI;QACA,OAAOC,UAAU,UAAU,CAACQ;IAChC,EAAE,OAAOC,OAAO;QACZ,IAAI,CAACV,SAAS,MAAM,EAChB,MAAMU;QAEV,MAAMC,QAAQD,iBAAiBH,QAAQG,MAAM,OAAO,GAAGE,OAAOF;QAC9D,MAAMG,QAAQb,SAAS,GAAG,CAACK,CAAAA,OAAQA,KAAK,OAAO,EAAE,IAAI,CAAC;QACtD,MAAM,IAAIE,MAAM,CAAC,wCAAwC,EAAEI,MAAM,GAAG,EAAEE,OAAO,EAAE;YAC3E,OAAOH;QACX;IACJ;AACJ"}
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";