@webiny/api-composition-standalone-sql 6.6.0-alpha.13 → 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.
@@ -6,29 +6,38 @@
6
6
  * @webiny/api-storage-pg, each built from its connection) and adds the self-hosted identity provider
7
7
  * (JWT IdP + SQL credential storage) on the storage's connection.
8
8
  *
9
- * At start, before the server accepts a request, it runs the storage's schema setup
10
- * (`SqlSchemaSetupRunner`) on a container built the way a request's is, so the storage, the login
11
- * tables and the modules among the project's extensions set up theirs. Then the storage is
12
- * schema-managed: no request checks for or creates tables. The JWT signing secret is read from
9
+ * Extra storages (`storages`) are SQL storages built with an `id`: only the tenants whose
10
+ * `settings.storage` names one run on it. Root and the tenant registry stay on the default storage.
11
+ *
12
+ * At start, before the server accepts a request, it runs every storage's schema setup
13
+ * (`runSqlSchemaSetups`) on a container built the way a scope on that storage is, so the storage,
14
+ * the login tables and the modules among the project's extensions set up theirs. Then each storage
15
+ * is schema-managed: no request checks for or creates tables. The JWT signing secret is read from
13
16
  * `WEBINY_SELF_HOSTED_AUTH_SECRET` when the handler is created.
14
17
  */
15
18
  import type { Container } from "@webiny/di";
16
19
  import { type SqlStorage } from "@webiny/api-core-sql";
17
20
  import { type CreateWebinyApiHandlerConfig as BaseConfig } from "@webiny/api-composition-standalone";
18
21
  export type CreateSqlApiHandlerConfig = Pick<BaseConfig, "extensions"> & {
19
- /** The SQL storage of the API, built from its connection. */
22
+ /** The SQL storage of the API, built from its connection: the default storage, root's. */
20
23
  storage: SqlStorage;
24
+ /**
25
+ * Extra storages tenants can be placed on, each built with its own `id` (`createPgStorage`,
26
+ * `createSqliteStorage`). A tenant runs on one when its `settings.storage` names it.
27
+ */
28
+ storages?: SqlStorage[];
21
29
  };
22
30
  /**
23
- * What the handler puts in the root container, once per process: the storage (the connection and
24
- * its settings, its `RequestStorage` and its schema setup runner), the login storage's
25
- * `RequestStorage`, and the self-hosted JWT identity provider. The IdP is in the root so the
26
- * RequestIdentityLoader (driven by HttpIdentityLoaderDecorator) can resolve it; it only needs the
27
- * token issuer, and the login use cases it registers resolve the credential storage from the
28
- * request's container.
31
+ * What the handler puts in the root container, once per process: the default storage (the
32
+ * connection and its settings, its `RequestStorage` and its schema setup runner), each extra storage
33
+ * (its backend and its runner), the login storage's part of every SQL storage (`SqlScopeStorage`),
34
+ * and the self-hosted JWT identity provider. The IdP is in the root so the RequestIdentityLoader
35
+ * (driven by HttpIdentityLoaderDecorator) can resolve it; it only needs the token issuer, and the
36
+ * login use cases it registers resolve the credential storage from the tenant's scope.
29
37
  */
30
38
  export declare function registerSqlApiRoot(container: Container, params: {
31
39
  storage: SqlStorage;
40
+ storages?: SqlStorage[];
32
41
  signingSecret: string;
33
42
  }): void;
34
43
  export declare function createSqlApiHandler(config: CreateSqlApiHandlerConfig): Promise<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>>;
@@ -1,33 +1,47 @@
1
- import { RequestStorage } from "@webiny/api-core";
2
- import { SqlSchemaSetupRunner } from "@webiny/api-core-sql";
1
+ import { TenantStorageId } from "@webiny/api-core/features/tenancy/TenantStorages/index.js";
2
+ import { SqlScopeStorage, runSqlSchemaSetups } from "@webiny/api-core-sql";
3
3
  import { ApiRequestRegistration } from "@webiny/api-composition-core";
4
4
  import { createWebinyApiHandler } from "@webiny/api-composition-standalone";
5
5
  import { SelfHostedAuthApiFeature } from "@webiny/self-hosted-auth";
6
6
  import { SelfHostedAuthSqlFeature } from "@webiny/self-hosted-auth-sql";
7
7
  import { readSigningSecret } from "./readSigningSecret.js";
8
- const SelfHostedAuthRequestStorage = {
8
+ const SelfHostedAuthScopeStorage = {
9
9
  register (container) {
10
10
  SelfHostedAuthSqlFeature.register(container);
11
11
  }
12
12
  };
13
+ const assertStorageIds = (storage, storages)=>{
14
+ if (!TenantStorageId.from(storage.id).isDefault()) throw new Error(`The API's storage is the default storage and takes no id, but it has the id "${storage.id}".`);
15
+ const seen = new Set();
16
+ for (const extra of storages){
17
+ if (TenantStorageId.from(extra.id).isDefault()) throw new Error(`An extra storage needs an id of its own: "${TenantStorageId.DEFAULT.toString()}" is the default storage's.`);
18
+ if (seen.has(extra.id)) throw new Error(`Two extra storages have the id "${extra.id}".`);
19
+ seen.add(extra.id);
20
+ }
21
+ };
13
22
  function registerSqlApiRoot(container, params) {
23
+ const storages = params.storages ?? [];
24
+ assertStorageIds(params.storage, storages);
14
25
  params.storage.register(container);
15
- container.registerInstance(RequestStorage, SelfHostedAuthRequestStorage);
26
+ for (const extra of storages)extra.register(container);
27
+ container.registerInstance(SqlScopeStorage, SelfHostedAuthScopeStorage);
16
28
  SelfHostedAuthApiFeature.register(container, {
17
29
  signingSecret: params.signingSecret
18
30
  });
19
31
  }
20
32
  function createSqlApiHandler(config) {
21
- const { storage } = config;
33
+ const { storage, storages } = config;
22
34
  const signingSecret = readSigningSecret(process.env);
23
35
  return createWebinyApiHandler({
24
36
  extensions: config.extensions,
25
37
  registerRootStorage: async (container)=>{
26
38
  registerSqlApiRoot(container, {
27
39
  storage,
40
+ storages,
28
41
  signingSecret
29
42
  });
30
- await container.resolve(SqlSchemaSetupRunner).run({
43
+ await runSqlSchemaSetups({
44
+ root: container,
31
45
  request: container.resolve(ApiRequestRegistration)
32
46
  });
33
47
  }
@@ -1 +1 @@
1
- {"version":3,"file":"createWebinyApiHandler.js","sources":["../src/createWebinyApiHandler.ts"],"sourcesContent":["/**\n * Webiny API handler for the standalone Node server transport on a SQL storage.\n *\n * Thin variant over the transport base (@webiny/api-composition-standalone): takes the storage\n * (`createSqliteStorage` from @webiny/api-storage-sqlite, or `createPgStorage` from\n * @webiny/api-storage-pg, each built from its connection) and adds the self-hosted identity provider\n * (JWT IdP + SQL credential storage) on the storage's connection.\n *\n * At start, before the server accepts a request, it runs the storage's schema setup\n * (`SqlSchemaSetupRunner`) on a container built the way a request's is, so the storage, the login\n * tables and the modules among the project's extensions set up theirs. Then the storage is\n * schema-managed: no request checks for or creates tables. The JWT signing secret is read from\n * `WEBINY_SELF_HOSTED_AUTH_SECRET` when the handler is created.\n */\nimport type { Container } from \"@webiny/di\";\nimport { RequestStorage } from \"@webiny/api-core\";\nimport { SqlSchemaSetupRunner, type SqlStorage } from \"@webiny/api-core-sql\";\nimport { ApiRequestRegistration } from \"@webiny/api-composition-core\";\nimport {\n createWebinyApiHandler as createBaseHandler,\n type CreateWebinyApiHandlerConfig as BaseConfig\n} from \"@webiny/api-composition-standalone\";\nimport { SelfHostedAuthApiFeature } from \"@webiny/self-hosted-auth\";\nimport { SelfHostedAuthSqlFeature } from \"@webiny/self-hosted-auth-sql\";\nimport { readSigningSecret } from \"~/readSigningSecret.js\";\n\nexport type CreateSqlApiHandlerConfig = Pick<BaseConfig, \"extensions\"> & {\n /** The SQL storage of the API, built from its connection. */\n storage: SqlStorage;\n};\n\n/*\n * The login storage of a request: the credentials and reset codes, on the storage's connection and\n * table settings (`KnexClient`, `SqlSchemaConfig`, in the root).\n */\nconst SelfHostedAuthRequestStorage: RequestStorage.Interface = {\n register(container) {\n SelfHostedAuthSqlFeature.register(container);\n }\n};\n\n/**\n * What the handler puts in the root container, once per process: the storage (the connection and\n * its settings, its `RequestStorage` and its schema setup runner), the login storage's\n * `RequestStorage`, and the self-hosted JWT identity provider. The IdP is in the root so the\n * RequestIdentityLoader (driven by HttpIdentityLoaderDecorator) can resolve it; it only needs the\n * token issuer, and the login use cases it registers resolve the credential storage from the\n * request's container.\n */\nexport function registerSqlApiRoot(\n container: Container,\n params: { storage: SqlStorage; signingSecret: string }\n): void {\n params.storage.register(container);\n container.registerInstance(RequestStorage, SelfHostedAuthRequestStorage);\n SelfHostedAuthApiFeature.register(container, { signingSecret: params.signingSecret });\n}\n\nexport function createSqlApiHandler(config: CreateSqlApiHandlerConfig) {\n const { storage } = config;\n // Read once, so a missing secret stops the server at boot instead of failing the first login.\n const signingSecret = readSigningSecret(process.env);\n\n return createBaseHandler({\n extensions: config.extensions,\n registerRootStorage: async container => {\n registerSqlApiRoot(container, { storage, signingSecret });\n /*\n * Before the server takes traffic: `root` runs before the server is handed back to\n * listen. The setup's container is built as a request's (`ApiRequestRegistration`), so\n * the login storage's `RequestStorage` adds the login tables. A failure (a missing\n * module, a database that's down) stops the server here.\n */\n await container.resolve(SqlSchemaSetupRunner).run({\n request: container.resolve(ApiRequestRegistration)\n });\n }\n });\n}\n"],"names":["SelfHostedAuthRequestStorage","container","SelfHostedAuthSqlFeature","registerSqlApiRoot","params","RequestStorage","SelfHostedAuthApiFeature","createSqlApiHandler","config","storage","signingSecret","readSigningSecret","process","createBaseHandler","SqlSchemaSetupRunner","ApiRequestRegistration"],"mappings":";;;;;;;AAmCA,MAAMA,+BAAyD;IAC3D,UAASC,SAAS;QACdC,yBAAyB,QAAQ,CAACD;IACtC;AACJ;AAUO,SAASE,mBACZF,SAAoB,EACpBG,MAAsD;IAEtDA,OAAO,OAAO,CAAC,QAAQ,CAACH;IACxBA,UAAU,gBAAgB,CAACI,gBAAgBL;IAC3CM,yBAAyB,QAAQ,CAACL,WAAW;QAAE,eAAeG,OAAO,aAAa;IAAC;AACvF;AAEO,SAASG,oBAAoBC,MAAiC;IACjE,MAAM,EAAEC,OAAO,EAAE,GAAGD;IAEpB,MAAME,gBAAgBC,kBAAkBC,QAAQ,GAAG;IAEnD,OAAOC,uBAAkB;QACrB,YAAYL,OAAO,UAAU;QAC7B,qBAAqB,OAAMP;YACvBE,mBAAmBF,WAAW;gBAAEQ;gBAASC;YAAc;YAOvD,MAAMT,UAAU,OAAO,CAACa,sBAAsB,GAAG,CAAC;gBAC9C,SAASb,UAAU,OAAO,CAACc;YAC/B;QACJ;IACJ;AACJ"}
1
+ {"version":3,"file":"createWebinyApiHandler.js","sources":["../src/createWebinyApiHandler.ts"],"sourcesContent":["/**\n * Webiny API handler for the standalone Node server transport on a SQL storage.\n *\n * Thin variant over the transport base (@webiny/api-composition-standalone): takes the storage\n * (`createSqliteStorage` from @webiny/api-storage-sqlite, or `createPgStorage` from\n * @webiny/api-storage-pg, each built from its connection) and adds the self-hosted identity provider\n * (JWT IdP + SQL credential storage) on the storage's connection.\n *\n * Extra storages (`storages`) are SQL storages built with an `id`: only the tenants whose\n * `settings.storage` names one run on it. Root and the tenant registry stay on the default storage.\n *\n * At start, before the server accepts a request, it runs every storage's schema setup\n * (`runSqlSchemaSetups`) on a container built the way a scope on that storage is, so the storage,\n * the login tables and the modules among the project's extensions set up theirs. Then each storage\n * is schema-managed: no request checks for or creates tables. The JWT signing secret is read from\n * `WEBINY_SELF_HOSTED_AUTH_SECRET` when the handler is created.\n */\nimport type { Container } from \"@webiny/di\";\nimport { TenantStorageId } from \"@webiny/api-core/features/tenancy/TenantStorages/index.js\";\nimport { runSqlSchemaSetups, SqlScopeStorage, type SqlStorage } from \"@webiny/api-core-sql\";\nimport { ApiRequestRegistration } from \"@webiny/api-composition-core\";\nimport {\n createWebinyApiHandler as createBaseHandler,\n type CreateWebinyApiHandlerConfig as BaseConfig\n} from \"@webiny/api-composition-standalone\";\nimport { SelfHostedAuthApiFeature } from \"@webiny/self-hosted-auth\";\nimport { SelfHostedAuthSqlFeature } from \"@webiny/self-hosted-auth-sql\";\nimport { readSigningSecret } from \"~/readSigningSecret.js\";\n\nexport type CreateSqlApiHandlerConfig = Pick<BaseConfig, \"extensions\"> & {\n /** The SQL storage of the API, built from its connection: the default storage, root's. */\n storage: SqlStorage;\n /**\n * Extra storages tenants can be placed on, each built with its own `id` (`createPgStorage`,\n * `createSqliteStorage`). A tenant runs on one when its `settings.storage` names it.\n */\n storages?: SqlStorage[];\n};\n\n/*\n * The login storage of a tenant scope: the credentials and reset codes, on the scope's SQL\n * connection and table settings (`KnexClient`, `SqlSchemaConfig`), on whichever SQL storage the\n * tenant runs.\n */\nconst SelfHostedAuthScopeStorage: SqlScopeStorage.Interface = {\n register(container) {\n SelfHostedAuthSqlFeature.register(container);\n }\n};\n\n/*\n * The default storage is the one without an id, each extra one has its own: a mix-up would put root\n * or a tenant on the wrong database, so it stops the server.\n */\nconst assertStorageIds = (storage: SqlStorage, storages: SqlStorage[]): void => {\n if (!TenantStorageId.from(storage.id).isDefault()) {\n throw new Error(\n `The API's storage is the default storage and takes no id, but it has the id \"${storage.id}\".`\n );\n }\n const seen = new Set<string>();\n for (const extra of storages) {\n if (TenantStorageId.from(extra.id).isDefault()) {\n throw new Error(\n `An extra storage needs an id of its own: \"${TenantStorageId.DEFAULT.toString()}\" is the default storage's.`\n );\n }\n if (seen.has(extra.id)) {\n throw new Error(`Two extra storages have the id \"${extra.id}\".`);\n }\n seen.add(extra.id);\n }\n};\n\n/**\n * What the handler puts in the root container, once per process: the default storage (the\n * connection and its settings, its `RequestStorage` and its schema setup runner), each extra storage\n * (its backend and its runner), the login storage's part of every SQL storage (`SqlScopeStorage`),\n * and the self-hosted JWT identity provider. The IdP is in the root so the RequestIdentityLoader\n * (driven by HttpIdentityLoaderDecorator) can resolve it; it only needs the token issuer, and the\n * login use cases it registers resolve the credential storage from the tenant's scope.\n */\nexport function registerSqlApiRoot(\n container: Container,\n params: { storage: SqlStorage; storages?: SqlStorage[]; signingSecret: string }\n): void {\n const storages = params.storages ?? [];\n assertStorageIds(params.storage, storages);\n params.storage.register(container);\n for (const extra of storages) {\n extra.register(container);\n }\n container.registerInstance(SqlScopeStorage, SelfHostedAuthScopeStorage);\n SelfHostedAuthApiFeature.register(container, { signingSecret: params.signingSecret });\n}\n\nexport function createSqlApiHandler(config: CreateSqlApiHandlerConfig) {\n const { storage, storages } = config;\n // Read once, so a missing secret stops the server at boot instead of failing the first login.\n const signingSecret = readSigningSecret(process.env);\n\n return createBaseHandler({\n extensions: config.extensions,\n registerRootStorage: async container => {\n registerSqlApiRoot(container, { storage, storages, signingSecret });\n /*\n * Before the server takes traffic: `root` runs before the server is handed back to\n * listen. Each storage's setup container is built as a scope on that storage is\n * (`ApiRequestRegistration`), so the login storage's part adds the login tables. A\n * failure (a missing module, a database that's down) stops the server here.\n */\n await runSqlSchemaSetups({\n root: container,\n request: container.resolve(ApiRequestRegistration)\n });\n }\n });\n}\n"],"names":["SelfHostedAuthScopeStorage","container","SelfHostedAuthSqlFeature","assertStorageIds","storage","storages","TenantStorageId","Error","seen","Set","extra","registerSqlApiRoot","params","SqlScopeStorage","SelfHostedAuthApiFeature","createSqlApiHandler","config","signingSecret","readSigningSecret","process","createBaseHandler","runSqlSchemaSetups","ApiRequestRegistration"],"mappings":";;;;;;;AA4CA,MAAMA,6BAAwD;IAC1D,UAASC,SAAS;QACdC,yBAAyB,QAAQ,CAACD;IACtC;AACJ;AAMA,MAAME,mBAAmB,CAACC,SAAqBC;IAC3C,IAAI,CAACC,gBAAgB,IAAI,CAACF,QAAQ,EAAE,EAAE,SAAS,IAC3C,MAAM,IAAIG,MACN,CAAC,6EAA6E,EAAEH,QAAQ,EAAE,CAAC,EAAE,CAAC;IAGtG,MAAMI,OAAO,IAAIC;IACjB,KAAK,MAAMC,SAASL,SAAU;QAC1B,IAAIC,gBAAgB,IAAI,CAACI,MAAM,EAAE,EAAE,SAAS,IACxC,MAAM,IAAIH,MACN,CAAC,0CAA0C,EAAED,gBAAgB,OAAO,CAAC,QAAQ,GAAG,2BAA2B,CAAC;QAGpH,IAAIE,KAAK,GAAG,CAACE,MAAM,EAAE,GACjB,MAAM,IAAIH,MAAM,CAAC,gCAAgC,EAAEG,MAAM,EAAE,CAAC,EAAE,CAAC;QAEnEF,KAAK,GAAG,CAACE,MAAM,EAAE;IACrB;AACJ;AAUO,SAASC,mBACZV,SAAoB,EACpBW,MAA+E;IAE/E,MAAMP,WAAWO,OAAO,QAAQ,IAAI,EAAE;IACtCT,iBAAiBS,OAAO,OAAO,EAAEP;IACjCO,OAAO,OAAO,CAAC,QAAQ,CAACX;IACxB,KAAK,MAAMS,SAASL,SAChBK,MAAM,QAAQ,CAACT;IAEnBA,UAAU,gBAAgB,CAACY,iBAAiBb;IAC5Cc,yBAAyB,QAAQ,CAACb,WAAW;QAAE,eAAeW,OAAO,aAAa;IAAC;AACvF;AAEO,SAASG,oBAAoBC,MAAiC;IACjE,MAAM,EAAEZ,OAAO,EAAEC,QAAQ,EAAE,GAAGW;IAE9B,MAAMC,gBAAgBC,kBAAkBC,QAAQ,GAAG;IAEnD,OAAOC,uBAAkB;QACrB,YAAYJ,OAAO,UAAU;QAC7B,qBAAqB,OAAMf;YACvBU,mBAAmBV,WAAW;gBAAEG;gBAASC;gBAAUY;YAAc;YAOjE,MAAMI,mBAAmB;gBACrB,MAAMpB;gBACN,SAASA,UAAU,OAAO,CAACqB;YAC/B;QACJ;IACJ;AACJ"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/api-composition-standalone-sql",
3
- "version": "6.6.0-alpha.13",
3
+ "version": "6.6.0-alpha.14",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -18,33 +18,34 @@
18
18
  ],
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@webiny/api-composition-core": "6.6.0-alpha.13",
22
- "@webiny/api-composition-standalone": "6.6.0-alpha.13",
23
- "@webiny/api-core": "6.6.0-alpha.13",
24
- "@webiny/api-core-sql": "6.6.0-alpha.13",
25
- "@webiny/api-mailer": "6.6.0-alpha.13",
21
+ "@webiny/api-composition-core": "6.6.0-alpha.14",
22
+ "@webiny/api-composition-standalone": "6.6.0-alpha.14",
23
+ "@webiny/api-core": "6.6.0-alpha.14",
24
+ "@webiny/api-core-sql": "6.6.0-alpha.14",
25
+ "@webiny/api-mailer": "6.6.0-alpha.14",
26
26
  "@webiny/di": "2.0.0",
27
- "@webiny/handler": "6.6.0-alpha.13",
28
- "@webiny/self-hosted-auth": "6.6.0-alpha.13",
29
- "@webiny/self-hosted-auth-sql": "6.6.0-alpha.13"
27
+ "@webiny/handler": "6.6.0-alpha.14",
28
+ "@webiny/self-hosted-auth": "6.6.0-alpha.14",
29
+ "@webiny/self-hosted-auth-sql": "6.6.0-alpha.14"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@electric-sql/pglite": "0.5.17",
33
33
  "@electric-sql/pglite-socket": "0.2.20",
34
34
  "@types/ws": "8.18.2",
35
- "@webiny/api-aco": "6.6.0-alpha.13",
36
- "@webiny/api-aco-pg": "6.6.0-alpha.13",
37
- "@webiny/api-audit-logs": "6.6.0-alpha.13",
38
- "@webiny/api-graphql": "6.6.0-alpha.13",
39
- "@webiny/api-headless-cms": "6.6.0-alpha.13",
40
- "@webiny/api-headless-cms-pg": "6.6.0-alpha.13",
41
- "@webiny/api-kernel-core": "6.6.0-alpha.13",
42
- "@webiny/api-object-storage": "6.6.0-alpha.13",
43
- "@webiny/api-object-storage-disk": "6.6.0-alpha.13",
44
- "@webiny/api-storage-pg": "6.6.0-alpha.13",
45
- "@webiny/api-storage-sqlite": "6.6.0-alpha.13",
46
- "@webiny/api-websockets": "6.6.0-alpha.13",
47
- "@webiny/build-tools": "6.6.0-alpha.13",
35
+ "@webiny/api-aco": "6.6.0-alpha.14",
36
+ "@webiny/api-aco-pg": "6.6.0-alpha.14",
37
+ "@webiny/api-audit-logs": "6.6.0-alpha.14",
38
+ "@webiny/api-graphql": "6.6.0-alpha.14",
39
+ "@webiny/api-headless-cms": "6.6.0-alpha.14",
40
+ "@webiny/api-headless-cms-pg": "6.6.0-alpha.14",
41
+ "@webiny/api-kernel-core": "6.6.0-alpha.14",
42
+ "@webiny/api-object-storage": "6.6.0-alpha.14",
43
+ "@webiny/api-object-storage-disk": "6.6.0-alpha.14",
44
+ "@webiny/api-storage-pg": "6.6.0-alpha.14",
45
+ "@webiny/api-storage-sqlite": "6.6.0-alpha.14",
46
+ "@webiny/api-websockets": "6.6.0-alpha.14",
47
+ "@webiny/build-tools": "6.6.0-alpha.14",
48
+ "@webiny/tenant-manager": "6.6.0-alpha.14",
48
49
  "knex": "3.3.0",
49
50
  "rimraf": "6.1.4",
50
51
  "typescript": "7.0.2",