@webiny/api-core-sql 0.0.0-unstable.0d717d18dd → 0.0.0-unstable.3fddf667ea
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/TableManager.d.ts +29 -9
- package/TableManager.js +31 -11
- package/TableManager.js.map +1 -1
- package/adminUsers/index.js +20 -17
- package/adminUsers/index.js.map +1 -1
- package/connection/SqlConnectionFeature.d.ts +28 -0
- package/connection/SqlConnectionFeature.js +26 -0
- package/connection/SqlConnectionFeature.js.map +1 -0
- package/connection/index.d.ts +2 -0
- package/connection/index.js +1 -0
- package/connectionRetry/index.d.ts +3 -0
- package/connectionRetry/index.js +2 -0
- package/connectionRetry/isConnectionError.d.ts +1 -0
- package/connectionRetry/isConnectionError.js +28 -0
- package/connectionRetry/isConnectionError.js.map +1 -0
- package/connectionRetry/withPgConnectionRetry.d.ts +49 -0
- package/connectionRetry/withPgConnectionRetry.js +175 -0
- package/connectionRetry/withPgConnectionRetry.js.map +1 -0
- package/coreStorage/CoreSqlSchemaSetup.d.ts +18 -0
- package/coreStorage/CoreSqlSchemaSetup.js +35 -0
- package/coreStorage/CoreSqlSchemaSetup.js.map +1 -0
- package/coreStorage/SqlApiCoreStorageOperationsFactory.d.ts +15 -0
- package/coreStorage/SqlApiCoreStorageOperationsFactory.js +32 -0
- package/coreStorage/SqlApiCoreStorageOperationsFactory.js.map +1 -0
- package/coreStorage/feature.d.ts +13 -0
- package/coreStorage/feature.js +13 -0
- package/coreStorage/feature.js.map +1 -0
- package/coreStorage/index.d.ts +1 -0
- package/coreStorage/index.js +1 -0
- package/createApiCoreSql.d.ts +13 -1
- package/createApiCoreSql.js +8 -8
- package/createApiCoreSql.js.map +1 -1
- package/index.d.ts +16 -3
- package/index.js +10 -2
- package/index.js.map +1 -1
- package/keyValueStore/index.js +160 -21
- package/keyValueStore/index.js.map +1 -1
- package/package.json +9 -9
- package/schemaConfig/abstractions.d.ts +22 -0
- package/schemaConfig/abstractions.js +5 -0
- package/schemaConfig/abstractions.js.map +1 -0
- package/schemaConfig/index.d.ts +1 -0
- package/schemaConfig/index.js +1 -0
- package/schemaMemory/ensureTable.d.ts +28 -0
- package/schemaMemory/ensureTable.js +43 -0
- package/schemaMemory/ensureTable.js.map +1 -0
- package/schemaMemory/getConnectionKey.d.ts +8 -0
- package/schemaMemory/getConnectionKey.js +15 -0
- package/schemaMemory/getConnectionKey.js.map +1 -0
- package/schemaMemory/getSchemaMemory.d.ts +37 -0
- package/schemaMemory/getSchemaMemory.js +53 -0
- package/schemaMemory/getSchemaMemory.js.map +1 -0
- package/schemaSetup/abstractions.d.ts +67 -0
- package/schemaSetup/abstractions.js +6 -0
- package/schemaSetup/abstractions.js.map +1 -0
- package/schemaSetup/index.d.ts +4 -0
- package/schemaSetup/index.js +2 -0
- package/schemaSetup/runSchemaSetupSteps.d.ts +34 -0
- package/schemaSetup/runSchemaSetupSteps.js +35 -0
- package/schemaSetup/runSchemaSetupSteps.js.map +1 -0
- package/security/index.js +47 -47
- package/security/index.js.map +1 -1
- package/serviceDiscovery/SqlServiceManifestLoader.d.ts +1 -1
- package/serviceDiscovery/SqlServiceManifestLoader.js +3 -8
- package/serviceDiscovery/SqlServiceManifestLoader.js.map +1 -1
- package/serviceDiscovery/SqlServiceManifestWriter.d.ts +16 -0
- package/serviceDiscovery/SqlServiceManifestWriter.js +27 -0
- package/serviceDiscovery/SqlServiceManifestWriter.js.map +1 -0
- package/serviceDiscovery/index.d.ts +1 -0
- package/serviceDiscovery/index.js +1 -0
- package/serviceDiscovery/serviceManifestsTable.d.ts +13 -0
- package/serviceDiscovery/serviceManifestsTable.js +8 -0
- package/serviceDiscovery/serviceManifestsTable.js.map +1 -0
- package/storage/types.d.ts +22 -0
- package/storage/types.js +0 -0
- package/tenancy/index.js +9 -9
- package/tenancy/index.js.map +1 -1
- package/ApiCoreSqlFeature.d.ts +0 -13
- package/ApiCoreSqlFeature.js +0 -17
- package/ApiCoreSqlFeature.js.map +0 -1
package/security/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"security/index.js","sources":["../../src/security/index.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type {\n SecurityStorageOperations,\n StorageApiKey,\n StorageRole,\n StorageTeam\n} from \"@webiny/api-core/types/security.js\";\nimport WebinyError from \"@webiny/error\";\nimport type { TableManager } from \"~/TableManager.js\";\nimport { sortItems } from \"~/sortItems.js\";\n\nconst ROLES_TABLE = \"webiny_core_roles\";\nconst TEAMS_TABLE = \"webiny_core_teams\";\nconst API_KEYS_TABLE = \"webiny_core_api_keys\";\n\ninterface IRoleRow {\n id: string;\n tenant: string;\n slug: string;\n data: string;\n}\n\ninterface ITeamRow {\n id: string;\n tenant: string;\n slug: string;\n data: string;\n}\n\ninterface IApiKeyRow {\n id: string;\n tenant: string;\n token: string;\n slug: string;\n data: string;\n}\n\ninterface CreateStorageOperationsParams {\n knex: Knex;\n tableManager: TableManager;\n}\n\nexport const createStorageOperations = (\n params: CreateStorageOperationsParams\n): SecurityStorageOperations => {\n const { knex, tableManager } = params;\n\n const ensureRolesTable = () => {\n return tableManager.ensure(ROLES_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n };\n\n const ensureTeamsTable = () => {\n return tableManager.ensure(TEAMS_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n };\n\n const ensureApiKeysTable = () => {\n return tableManager.ensure(API_KEYS_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"token\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"token\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n };\n\n const rolesQuery = () => knex<IRoleRow>(tableManager.resolve(ROLES_TABLE));\n const teamsQuery = () => knex<ITeamRow>(tableManager.resolve(TEAMS_TABLE));\n const apiKeysQuery = () => knex<IApiKeyRow>(tableManager.resolve(API_KEYS_TABLE));\n\n return {\n /* Roles */\n async getRole({ where: { tenant, id, slug } }) {\n await ensureRolesTable();\n\n try {\n if (id) {\n const row = await rolesQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"id\", id)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageRole) : null;\n }\n\n const row = await rolesQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug as string)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageRole) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load role.\",\n code: \"GET_ROLE_ERROR\",\n data: { id, slug }\n });\n }\n },\n\n async listRoles({ where: { tenant, id_in, slug_in }, sort }) {\n await ensureRolesTable();\n\n try {\n const qb = rolesQuery().where(\"tenant\", tenant);\n\n if (Array.isArray(id_in)) {\n qb.whereIn(\"id\", id_in);\n } else if (Array.isArray(slug_in)) {\n qb.whereIn(\"slug\", slug_in);\n }\n\n const rows = await qb;\n const items = rows.map(row => JSON.parse(row.data) as StorageRole);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list roles.\",\n code: \"LIST_ROLE_ERROR\"\n });\n }\n },\n\n async createRole({ role }) {\n await ensureRolesTable();\n\n try {\n await rolesQuery().insert({\n id: role.id,\n tenant: role.tenant,\n slug: role.slug,\n data: JSON.stringify(role)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create role.\",\n code: \"CREATE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n async updateRole({ role }) {\n await ensureRolesTable();\n\n try {\n await rolesQuery()\n .where(\"tenant\", role.tenant)\n .andWhere(\"id\", role.id)\n .update({\n slug: role.slug,\n data: JSON.stringify(role)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update role.\",\n code: \"UPDATE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n async deleteRole({ role }) {\n await ensureRolesTable();\n\n try {\n await rolesQuery().where(\"tenant\", role.tenant).andWhere(\"id\", role.id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete role.\",\n code: \"DELETE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n /* Teams */\n async getTeam({ where: { tenant, id, slug } }) {\n await ensureTeamsTable();\n\n try {\n if (id) {\n const row = await teamsQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"id\", id)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageTeam) : null;\n }\n\n const row = await teamsQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug as string)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageTeam) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load team.\",\n code: \"GET_TEAM_ERROR\",\n data: { id, slug }\n });\n }\n },\n\n async listTeams({ where: { tenant, id_in, slug_in }, sort }) {\n await ensureTeamsTable();\n\n try {\n const qb = teamsQuery().where(\"tenant\", tenant);\n\n if (Array.isArray(id_in)) {\n qb.whereIn(\"id\", id_in);\n } else if (Array.isArray(slug_in)) {\n qb.whereIn(\"slug\", slug_in);\n }\n\n const rows = await qb;\n const items = rows.map(row => JSON.parse(row.data) as StorageTeam);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list teams.\",\n code: \"LIST_TEAM_ERROR\"\n });\n }\n },\n\n async createTeam({ team }) {\n await ensureTeamsTable();\n\n try {\n await teamsQuery().insert({\n id: team.id,\n tenant: team.tenant,\n slug: team.slug,\n data: JSON.stringify(team)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create team.\",\n code: \"CREATE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n async updateTeam({ team }) {\n await ensureTeamsTable();\n\n try {\n await teamsQuery()\n .where(\"tenant\", team.tenant)\n .andWhere(\"id\", team.id)\n .update({\n slug: team.slug,\n data: JSON.stringify(team)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update team.\",\n code: \"UPDATE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n async deleteTeam({ team }) {\n await ensureTeamsTable();\n\n try {\n await teamsQuery().where(\"tenant\", team.tenant).andWhere(\"id\", team.id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete team.\",\n code: \"DELETE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n /* API Keys */\n async getApiKey({ id, tenant }) {\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery().where(\"tenant\", tenant).andWhere(\"id\", id).first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key.\",\n code: \"GET_API_KEY_ERROR\",\n data: { id }\n });\n }\n },\n\n async getApiKeyByToken({ tenant, token }) {\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"token\", token)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key by token.\",\n code: \"GET_BY_TOKEN_API_KEY_ERROR\",\n data: { token }\n });\n }\n },\n\n async getApiKeyBySlug({ tenant, slug }) {\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key by slug.\",\n code: \"GET_BY_SLUG_API_KEY_ERROR\",\n data: { slug }\n });\n }\n },\n\n async listApiKeys({ where: { tenant }, sort }) {\n await ensureApiKeysTable();\n\n try {\n const rows = await apiKeysQuery().where(\"tenant\", tenant);\n const items = rows.map(row => JSON.parse(row.data) as StorageApiKey);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list api keys.\",\n code: \"LIST_API_KEY_ERROR\"\n });\n }\n },\n\n async createApiKey({ apiKey }) {\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery().insert({\n id: apiKey.id,\n tenant: apiKey.tenant,\n token: apiKey.token,\n slug: apiKey.slug,\n data: JSON.stringify(apiKey)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create api key.\",\n code: \"CREATE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n },\n\n async updateApiKey({ apiKey }) {\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery()\n .where(\"tenant\", apiKey.tenant)\n .andWhere(\"id\", apiKey.id)\n .update({\n token: apiKey.token,\n slug: apiKey.slug,\n data: JSON.stringify(apiKey)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update api key.\",\n code: \"UPDATE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n },\n\n async deleteApiKey({ apiKey }) {\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery()\n .where(\"tenant\", apiKey.tenant)\n .andWhere(\"id\", apiKey.id)\n .delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete api key.\",\n code: \"DELETE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n }\n };\n};\n"],"names":["ROLES_TABLE","TEAMS_TABLE","API_KEYS_TABLE","createStorageOperations","params","knex","tableManager","ensureRolesTable","table","ensureTeamsTable","ensureApiKeysTable","rolesQuery","teamsQuery","apiKeysQuery","tenant","id","slug","row","JSON","err","WebinyError","id_in","slug_in","sort","qb","Array","rows","items","sortItems","role","team","token","apiKey"],"mappings":";;AAWA,MAAMA,cAAc;AACpB,MAAMC,cAAc;AACpB,MAAMC,iBAAiB;AA6BhB,MAAMC,0BAA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,MAAM;wBAAEa;oBAAO;gBACnB;YACJ;QACJ;QAEA,MAAM,cAAa,EAAEA,MAAM,EAAE;YACzB,MAAMtB;YAEN,IAAI;gBACA,MAAMG,eACD,KAAK,CAAC,UAAUmB,OAAO,MAAM,EAC7B,QAAQ,CAAC,MAAMA,OAAO,EAAE,EACxB,MAAM,CAAC;oBACJ,OAAOA,OAAO,KAAK;oBACnB,MAAMA,OAAO,IAAI;oBACjB,MAAMd,KAAK,SAAS,CAACc;gBACzB;YACR,EAAE,OAAOb,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAEa;oBAAO;gBACnB;YACJ;QACJ;QAEA,MAAM,cAAa,EAAEA,MAAM,EAAE;YACzB,MAAMtB;YAEN,IAAI;gBACA,MAAMG,eACD,KAAK,CAAC,UAAUmB,OAAO,MAAM,EAC7B,QAAQ,CAAC,MAAMA,OAAO,EAAE,EACxB,MAAM;YACf,EAAE,OAAOb,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAEa;oBAAO;gBACnB;YACJ;QACJ;IACJ;AACJ"}
|
|
1
|
+
{"version":3,"file":"security/index.js","sources":["../../src/security/index.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type {\n SecurityStorageOperations,\n StorageApiKey,\n StorageRole,\n StorageTeam\n} from \"@webiny/api-core/types/security.js\";\nimport WebinyError from \"@webiny/error\";\nimport type { TableManager } from \"~/TableManager.js\";\nimport { sortItems } from \"~/sortItems.js\";\n\nconst ROLES_TABLE = \"webiny_core_roles\";\nconst TEAMS_TABLE = \"webiny_core_teams\";\nconst API_KEYS_TABLE = \"webiny_core_api_keys\";\n\ninterface IRoleRow {\n id: string;\n tenant: string;\n slug: string;\n data: string;\n}\n\ninterface ITeamRow {\n id: string;\n tenant: string;\n slug: string;\n data: string;\n}\n\ninterface IApiKeyRow {\n id: string;\n tenant: string;\n token: string;\n slug: string;\n data: string;\n}\n\ninterface CreateStorageOperationsParams {\n knex: Knex;\n tableManager: TableManager;\n}\n\nexport const createStorageOperations = (\n params: CreateStorageOperationsParams\n): SecurityStorageOperations => {\n const { knex, tableManager } = params;\n\n const ensureRolesTable = tableManager.define(ROLES_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n\n const ensureTeamsTable = tableManager.define(TEAMS_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n\n const ensureApiKeysTable = tableManager.define(API_KEYS_TABLE, table => {\n table.text(\"id\").notNullable();\n table.text(\"tenant\").notNullable();\n table.text(\"token\").notNullable();\n table.text(\"slug\").notNullable();\n table.text(\"data\").notNullable();\n\n table.primary([\"tenant\", \"id\"]);\n table.unique([\"tenant\", \"token\"]);\n table.unique([\"tenant\", \"slug\"]);\n });\n\n const rolesQuery = () => knex<IRoleRow>(tableManager.resolve(ROLES_TABLE));\n const teamsQuery = () => knex<ITeamRow>(tableManager.resolve(TEAMS_TABLE));\n const apiKeysQuery = () => knex<IApiKeyRow>(tableManager.resolve(API_KEYS_TABLE));\n\n return {\n /* Roles */\n async getRole({ where: { tenant, id, slug } }) {\n await ensureRolesTable();\n\n try {\n if (id) {\n const row = await rolesQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"id\", id)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageRole) : null;\n }\n\n const row = await rolesQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug as string)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageRole) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load role.\",\n code: \"GET_ROLE_ERROR\",\n data: { id, slug }\n });\n }\n },\n\n async listRoles({ where: { tenant, id_in, slug_in }, sort }) {\n await ensureRolesTable();\n\n try {\n const qb = rolesQuery().where(\"tenant\", tenant);\n\n if (Array.isArray(id_in)) {\n qb.whereIn(\"id\", id_in);\n } else if (Array.isArray(slug_in)) {\n qb.whereIn(\"slug\", slug_in);\n }\n\n const rows = await qb;\n const items = rows.map(row => JSON.parse(row.data) as StorageRole);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list roles.\",\n code: \"LIST_ROLE_ERROR\"\n });\n }\n },\n\n async createRole({ role }) {\n await ensureRolesTable();\n\n try {\n await rolesQuery().insert({\n id: role.id,\n tenant: role.tenant,\n slug: role.slug,\n data: JSON.stringify(role)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create role.\",\n code: \"CREATE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n async updateRole({ role }) {\n await ensureRolesTable();\n\n try {\n await rolesQuery()\n .where(\"tenant\", role.tenant)\n .andWhere(\"id\", role.id)\n .update({\n slug: role.slug,\n data: JSON.stringify(role)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update role.\",\n code: \"UPDATE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n async deleteRole({ role }) {\n await ensureRolesTable();\n\n try {\n await rolesQuery().where(\"tenant\", role.tenant).andWhere(\"id\", role.id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete role.\",\n code: \"DELETE_ROLE_ERROR\",\n data: { role }\n });\n }\n },\n\n /* Teams */\n async getTeam({ where: { tenant, id, slug } }) {\n await ensureTeamsTable();\n\n try {\n if (id) {\n const row = await teamsQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"id\", id)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageTeam) : null;\n }\n\n const row = await teamsQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug as string)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageTeam) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load team.\",\n code: \"GET_TEAM_ERROR\",\n data: { id, slug }\n });\n }\n },\n\n async listTeams({ where: { tenant, id_in, slug_in }, sort }) {\n await ensureTeamsTable();\n\n try {\n const qb = teamsQuery().where(\"tenant\", tenant);\n\n if (Array.isArray(id_in)) {\n qb.whereIn(\"id\", id_in);\n } else if (Array.isArray(slug_in)) {\n qb.whereIn(\"slug\", slug_in);\n }\n\n const rows = await qb;\n const items = rows.map(row => JSON.parse(row.data) as StorageTeam);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list teams.\",\n code: \"LIST_TEAM_ERROR\"\n });\n }\n },\n\n async createTeam({ team }) {\n await ensureTeamsTable();\n\n try {\n await teamsQuery().insert({\n id: team.id,\n tenant: team.tenant,\n slug: team.slug,\n data: JSON.stringify(team)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create team.\",\n code: \"CREATE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n async updateTeam({ team }) {\n await ensureTeamsTable();\n\n try {\n await teamsQuery()\n .where(\"tenant\", team.tenant)\n .andWhere(\"id\", team.id)\n .update({\n slug: team.slug,\n data: JSON.stringify(team)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update team.\",\n code: \"UPDATE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n async deleteTeam({ team }) {\n await ensureTeamsTable();\n\n try {\n await teamsQuery().where(\"tenant\", team.tenant).andWhere(\"id\", team.id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete team.\",\n code: \"DELETE_TEAM_ERROR\",\n data: { team }\n });\n }\n },\n\n /* API Keys */\n async getApiKey({ id, tenant }) {\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery().where(\"tenant\", tenant).andWhere(\"id\", id).first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key.\",\n code: \"GET_API_KEY_ERROR\",\n data: { id }\n });\n }\n },\n\n async getApiKeyByToken({ tenant, token }) {\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"token\", token)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key by token.\",\n code: \"GET_BY_TOKEN_API_KEY_ERROR\",\n data: { token }\n });\n }\n },\n\n async getApiKeyBySlug({ tenant, slug }) {\n await ensureApiKeysTable();\n\n try {\n const row = await apiKeysQuery()\n .where(\"tenant\", tenant)\n .andWhere(\"slug\", slug)\n .first();\n\n return row ? (JSON.parse(row.data) as StorageApiKey) : null;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not load api key by slug.\",\n code: \"GET_BY_SLUG_API_KEY_ERROR\",\n data: { slug }\n });\n }\n },\n\n async listApiKeys({ where: { tenant }, sort }) {\n await ensureApiKeysTable();\n\n try {\n const rows = await apiKeysQuery().where(\"tenant\", tenant);\n const items = rows.map(row => JSON.parse(row.data) as StorageApiKey);\n\n return sortItems(items, sort);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list api keys.\",\n code: \"LIST_API_KEY_ERROR\"\n });\n }\n },\n\n async createApiKey({ apiKey }) {\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery().insert({\n id: apiKey.id,\n tenant: apiKey.tenant,\n token: apiKey.token,\n slug: apiKey.slug,\n data: JSON.stringify(apiKey)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create api key.\",\n code: \"CREATE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n },\n\n async updateApiKey({ apiKey }) {\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery()\n .where(\"tenant\", apiKey.tenant)\n .andWhere(\"id\", apiKey.id)\n .update({\n token: apiKey.token,\n slug: apiKey.slug,\n data: JSON.stringify(apiKey)\n });\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update api key.\",\n code: \"UPDATE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n },\n\n async deleteApiKey({ apiKey }) {\n await ensureApiKeysTable();\n\n try {\n await apiKeysQuery()\n .where(\"tenant\", apiKey.tenant)\n .andWhere(\"id\", apiKey.id)\n .delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete api key.\",\n code: \"DELETE_API_KEY_ERROR\",\n data: { apiKey }\n });\n }\n }\n };\n};\n"],"names":["ROLES_TABLE","TEAMS_TABLE","API_KEYS_TABLE","createStorageOperations","params","knex","tableManager","ensureRolesTable","table","ensureTeamsTable","ensureApiKeysTable","rolesQuery","teamsQuery","apiKeysQuery","tenant","id","slug","row","JSON","err","WebinyError","id_in","slug_in","sort","qb","Array","rows","items","sortItems","role","team","token","apiKey"],"mappings":";;AAWA,MAAMA,cAAc;AACpB,MAAMC,cAAc;AACpB,MAAMC,iBAAiB;AA6BhB,MAAMC,0BAA0B,CACnCC;IAEA,MAAM,EAAEC,IAAI,EAAEC,YAAY,EAAE,GAAGF;IAE/B,MAAMG,mBAAmBD,aAAa,MAAM,CAACN,aAAaQ,CAAAA;QACtDA,MAAM,IAAI,CAAC,MAAM,WAAW;QAC5BA,MAAM,IAAI,CAAC,UAAU,WAAW;QAChCA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAC9BA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAE9BA,MAAM,OAAO,CAAC;YAAC;YAAU;SAAK;QAC9BA,MAAM,MAAM,CAAC;YAAC;YAAU;SAAO;IACnC;IAEA,MAAMC,mBAAmBH,aAAa,MAAM,CAACL,aAAaO,CAAAA;QACtDA,MAAM,IAAI,CAAC,MAAM,WAAW;QAC5BA,MAAM,IAAI,CAAC,UAAU,WAAW;QAChCA,MAAM,IAAI,CAAC,QAAQ,WAAW;QAC9BA,MAAM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AO,EAAE,EACxB,MAAM;YACf,EAAE,OAAOb,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAEa;oBAAO;gBACnB;YACJ;QACJ;IACJ;AACJ"}
|
|
@@ -4,10 +4,10 @@ import type { TableManager } from "../TableManager.js";
|
|
|
4
4
|
export declare class SqlServiceManifestLoader implements IServiceManifestLoader {
|
|
5
5
|
private readonly knex;
|
|
6
6
|
private readonly tableManager;
|
|
7
|
+
private readonly ensureTable;
|
|
7
8
|
constructor(knex: Knex, tableManager: TableManager);
|
|
8
9
|
load(): Promise<{
|
|
9
10
|
name: string;
|
|
10
11
|
manifest: any;
|
|
11
12
|
}[] | undefined>;
|
|
12
|
-
private ensureTable;
|
|
13
13
|
}
|
|
@@ -1,24 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
import { SERVICE_MANIFESTS_TABLE, defineServiceManifestsTable } from "./serviceManifestsTable.js";
|
|
2
2
|
class SqlServiceManifestLoader {
|
|
3
3
|
constructor(knex, tableManager){
|
|
4
4
|
this.knex = knex;
|
|
5
5
|
this.tableManager = tableManager;
|
|
6
|
+
this.ensureTable = defineServiceManifestsTable(tableManager);
|
|
6
7
|
}
|
|
7
8
|
async load() {
|
|
8
9
|
await this.ensureTable();
|
|
9
|
-
const rows = await this.knex(this.tableManager.resolve(
|
|
10
|
+
const rows = await this.knex(this.tableManager.resolve(SERVICE_MANIFESTS_TABLE)).select("name", "manifest");
|
|
10
11
|
if (!rows.length) return;
|
|
11
12
|
return rows.map((row)=>({
|
|
12
13
|
name: row.name,
|
|
13
14
|
manifest: JSON.parse(row.manifest)
|
|
14
15
|
}));
|
|
15
16
|
}
|
|
16
|
-
async ensureTable() {
|
|
17
|
-
await this.tableManager.ensure(TABLE_NAME, (table)=>{
|
|
18
|
-
table.text("name").primary().notNullable();
|
|
19
|
-
table.text("manifest").notNullable();
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
17
|
}
|
|
23
18
|
export { SqlServiceManifestLoader };
|
|
24
19
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serviceDiscovery/SqlServiceManifestLoader.js","sources":["../../src/serviceDiscovery/SqlServiceManifestLoader.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type { IServiceManifestLoader } from \"@webiny/api-core/features/serviceDiscovery/index.js\";\nimport type { TableManager } from \"~/TableManager.js\";\
|
|
1
|
+
{"version":3,"file":"serviceDiscovery/SqlServiceManifestLoader.js","sources":["../../src/serviceDiscovery/SqlServiceManifestLoader.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type { IServiceManifestLoader } from \"@webiny/api-core/features/serviceDiscovery/index.js\";\nimport type { TableManager } from \"~/TableManager.js\";\nimport {\n defineServiceManifestsTable,\n SERVICE_MANIFESTS_TABLE,\n type IServiceManifestRow\n} from \"./serviceManifestsTable.js\";\n\nexport class SqlServiceManifestLoader implements IServiceManifestLoader {\n private readonly knex: Knex;\n private readonly tableManager: TableManager;\n private readonly ensureTable: () => Promise<void>;\n\n constructor(knex: Knex, tableManager: TableManager) {\n this.knex = knex;\n this.tableManager = tableManager;\n this.ensureTable = defineServiceManifestsTable(tableManager);\n }\n\n async load() {\n await this.ensureTable();\n\n const rows = await this.knex<IServiceManifestRow>(\n this.tableManager.resolve(SERVICE_MANIFESTS_TABLE)\n ).select(\"name\", \"manifest\");\n\n if (!rows.length) {\n return undefined;\n }\n\n return rows.map(row => ({\n name: row.name,\n manifest: JSON.parse(row.manifest)\n }));\n }\n}\n"],"names":["SqlServiceManifestLoader","knex","tableManager","defineServiceManifestsTable","rows","SERVICE_MANIFESTS_TABLE","row","JSON"],"mappings":";AASO,MAAMA;IAKT,YAAYC,IAAU,EAAEC,YAA0B,CAAE;QAChD,IAAI,CAAC,IAAI,GAAGD;QACZ,IAAI,CAAC,YAAY,GAAGC;QACpB,IAAI,CAAC,WAAW,GAAGC,4BAA4BD;IACnD;IAEA,MAAM,OAAO;QACT,MAAM,IAAI,CAAC,WAAW;QAEtB,MAAME,OAAO,MAAM,IAAI,CAAC,IAAI,CACxB,IAAI,CAAC,YAAY,CAAC,OAAO,CAACC,0BAC5B,MAAM,CAAC,QAAQ;QAEjB,IAAI,CAACD,KAAK,MAAM,EACZ;QAGJ,OAAOA,KAAK,GAAG,CAACE,CAAAA,MAAQ;gBACpB,MAAMA,IAAI,IAAI;gBACd,UAAUC,KAAK,KAAK,CAACD,IAAI,QAAQ;YACrC;IACJ;AACJ"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
import type { IServiceManifest } from "@webiny/api-core/features/serviceDiscovery/index.js";
|
|
3
|
+
import type { TableManager } from "../TableManager.js";
|
|
4
|
+
/**
|
|
5
|
+
* Writes service manifests where `SqlServiceManifestLoader` reads them. A deployment calls it with
|
|
6
|
+
* the manifests of the services it deployed: each one replaces the stored manifest of the same name,
|
|
7
|
+
* and services it doesn't pass keep theirs (the admin deploy writes `admin` alone, after the api
|
|
8
|
+
* deploy wrote `core`, `api` and `scheduler`).
|
|
9
|
+
*/
|
|
10
|
+
export declare class SqlServiceManifestWriter {
|
|
11
|
+
private readonly knex;
|
|
12
|
+
private readonly tableManager;
|
|
13
|
+
private readonly ensureTable;
|
|
14
|
+
constructor(knex: Knex, tableManager: TableManager);
|
|
15
|
+
write(manifests: IServiceManifest[]): Promise<void>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SERVICE_MANIFESTS_TABLE, defineServiceManifestsTable } from "./serviceManifestsTable.js";
|
|
2
|
+
class SqlServiceManifestWriter {
|
|
3
|
+
constructor(knex, tableManager){
|
|
4
|
+
this.knex = knex;
|
|
5
|
+
this.tableManager = tableManager;
|
|
6
|
+
this.ensureTable = defineServiceManifestsTable(tableManager);
|
|
7
|
+
}
|
|
8
|
+
async write(manifests) {
|
|
9
|
+
const rows = new Map();
|
|
10
|
+
for (const item of manifests){
|
|
11
|
+
if (!item || "string" != typeof item.name || !item.name) throw new Error("A service manifest needs a `name`.");
|
|
12
|
+
if (!item.manifest || "object" != typeof item.manifest) throw new Error(`The service manifest "${item.name}" needs a \`manifest\` object.`);
|
|
13
|
+
rows.set(item.name, {
|
|
14
|
+
name: item.name,
|
|
15
|
+
manifest: JSON.stringify(item.manifest)
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
if (!rows.size) return;
|
|
19
|
+
await this.ensureTable();
|
|
20
|
+
await this.knex(this.tableManager.resolve(SERVICE_MANIFESTS_TABLE)).insert([
|
|
21
|
+
...rows.values()
|
|
22
|
+
]).onConflict("name").merge();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export { SqlServiceManifestWriter };
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=SqlServiceManifestWriter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serviceDiscovery/SqlServiceManifestWriter.js","sources":["../../src/serviceDiscovery/SqlServiceManifestWriter.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type { IServiceManifest } from \"@webiny/api-core/features/serviceDiscovery/index.js\";\nimport type { TableManager } from \"~/TableManager.js\";\nimport {\n defineServiceManifestsTable,\n SERVICE_MANIFESTS_TABLE,\n type IServiceManifestRow\n} from \"./serviceManifestsTable.js\";\n\n/**\n * Writes service manifests where `SqlServiceManifestLoader` reads them. A deployment calls it with\n * the manifests of the services it deployed: each one replaces the stored manifest of the same name,\n * and services it doesn't pass keep theirs (the admin deploy writes `admin` alone, after the api\n * deploy wrote `core`, `api` and `scheduler`).\n */\nexport class SqlServiceManifestWriter {\n private readonly knex: Knex;\n private readonly tableManager: TableManager;\n private readonly ensureTable: () => Promise<void>;\n\n constructor(knex: Knex, tableManager: TableManager) {\n this.knex = knex;\n this.tableManager = tableManager;\n this.ensureTable = defineServiceManifestsTable(tableManager);\n }\n\n async write(manifests: IServiceManifest[]): Promise<void> {\n // The input comes from a deployment's invocation payload, so it's checked before it's stored.\n const rows = new Map<string, IServiceManifestRow>();\n for (const item of manifests) {\n if (!item || typeof item.name !== \"string\" || !item.name) {\n throw new Error(\"A service manifest needs a `name`.\");\n }\n if (!item.manifest || typeof item.manifest !== \"object\") {\n throw new Error(`The service manifest \"${item.name}\" needs a \\`manifest\\` object.`);\n }\n // One row per name: Postgres refuses an upsert that touches the same row twice.\n rows.set(item.name, { name: item.name, manifest: JSON.stringify(item.manifest) });\n }\n\n if (!rows.size) {\n return;\n }\n\n await this.ensureTable();\n\n await this.knex<IServiceManifestRow>(this.tableManager.resolve(SERVICE_MANIFESTS_TABLE))\n .insert([...rows.values()])\n .onConflict(\"name\")\n .merge();\n }\n}\n"],"names":["SqlServiceManifestWriter","knex","tableManager","defineServiceManifestsTable","manifests","rows","Map","item","Error","JSON","SERVICE_MANIFESTS_TABLE"],"mappings":";AAeO,MAAMA;IAKT,YAAYC,IAAU,EAAEC,YAA0B,CAAE;QAChD,IAAI,CAAC,IAAI,GAAGD;QACZ,IAAI,CAAC,YAAY,GAAGC;QACpB,IAAI,CAAC,WAAW,GAAGC,4BAA4BD;IACnD;IAEA,MAAM,MAAME,SAA6B,EAAiB;QAEtD,MAAMC,OAAO,IAAIC;QACjB,KAAK,MAAMC,QAAQH,UAAW;YAC1B,IAAI,CAACG,QAAQ,AAAqB,YAArB,OAAOA,KAAK,IAAI,IAAiB,CAACA,KAAK,IAAI,EACpD,MAAM,IAAIC,MAAM;YAEpB,IAAI,CAACD,KAAK,QAAQ,IAAI,AAAyB,YAAzB,OAAOA,KAAK,QAAQ,EACtC,MAAM,IAAIC,MAAM,CAAC,sBAAsB,EAAED,KAAK,IAAI,CAAC,8BAA8B,CAAC;YAGtFF,KAAK,GAAG,CAACE,KAAK,IAAI,EAAE;gBAAE,MAAMA,KAAK,IAAI;gBAAE,UAAUE,KAAK,SAAS,CAACF,KAAK,QAAQ;YAAE;QACnF;QAEA,IAAI,CAACF,KAAK,IAAI,EACV;QAGJ,MAAM,IAAI,CAAC,WAAW;QAEtB,MAAM,IAAI,CAAC,IAAI,CAAsB,IAAI,CAAC,YAAY,CAAC,OAAO,CAACK,0BAC1D,MAAM,CAAC;eAAIL,KAAK,MAAM;SAAG,EACzB,UAAU,CAAC,QACX,KAAK;IACd;AACJ"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { TableManager } from "../TableManager.js";
|
|
2
|
+
/** Service manifests, one row per service (`core`, `api`, `admin`, …), read by service discovery. */
|
|
3
|
+
export declare const SERVICE_MANIFESTS_TABLE = "webiny_service_manifests";
|
|
4
|
+
export interface IServiceManifestRow {
|
|
5
|
+
name: string;
|
|
6
|
+
/** The manifest, as JSON. */
|
|
7
|
+
manifest: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Declares the manifests table on `tableManager` and returns the function that ensures it on first
|
|
11
|
+
* use. The loader and the writer both declare it here, so they can't disagree on its shape.
|
|
12
|
+
*/
|
|
13
|
+
export declare const defineServiceManifestsTable: (tableManager: TableManager) => (() => Promise<void>);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const SERVICE_MANIFESTS_TABLE = "webiny_service_manifests";
|
|
2
|
+
const defineServiceManifestsTable = (tableManager)=>tableManager.define(SERVICE_MANIFESTS_TABLE, (table)=>{
|
|
3
|
+
table.text("name").primary().notNullable();
|
|
4
|
+
table.text("manifest").notNullable();
|
|
5
|
+
});
|
|
6
|
+
export { SERVICE_MANIFESTS_TABLE, defineServiceManifestsTable };
|
|
7
|
+
|
|
8
|
+
//# sourceMappingURL=serviceManifestsTable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serviceDiscovery/serviceManifestsTable.js","sources":["../../src/serviceDiscovery/serviceManifestsTable.ts"],"sourcesContent":["import type { TableManager } from \"~/TableManager.js\";\n\n/** Service manifests, one row per service (`core`, `api`, `admin`, …), read by service discovery. */\nexport const SERVICE_MANIFESTS_TABLE = \"webiny_service_manifests\";\n\nexport interface IServiceManifestRow {\n name: string;\n /** The manifest, as JSON. */\n manifest: string;\n}\n\n/**\n * Declares the manifests table on `tableManager` and returns the function that ensures it on first\n * use. The loader and the writer both declare it here, so they can't disagree on its shape.\n */\nexport const defineServiceManifestsTable = (tableManager: TableManager): (() => Promise<void>) => {\n return tableManager.define(SERVICE_MANIFESTS_TABLE, table => {\n table.text(\"name\").primary().notNullable();\n table.text(\"manifest\").notNullable();\n });\n};\n"],"names":["SERVICE_MANIFESTS_TABLE","defineServiceManifestsTable","tableManager","table"],"mappings":"AAGO,MAAMA,0BAA0B;AAYhC,MAAMC,8BAA8B,CAACC,eACjCA,aAAa,MAAM,CAACF,yBAAyBG,CAAAA;QAChDA,MAAM,IAAI,CAAC,QAAQ,OAAO,GAAG,WAAW;QACxCA,MAAM,IAAI,CAAC,YAAY,WAAW;IACtC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
import type { Container } from "@webiny/feature/api";
|
|
3
|
+
/**
|
|
4
|
+
* The SQL storage of the Webiny API, built from a connection (`createSqliteStorage`,
|
|
5
|
+
* `createPgStorage`) and handed to a transport (the standalone server, AWS Lambda). It holds no
|
|
6
|
+
* engine settings beyond its own; modules with settings (the Postgres CMS) come with the project's
|
|
7
|
+
* extensions.
|
|
8
|
+
*/
|
|
9
|
+
export interface SqlStorage {
|
|
10
|
+
/** The connection the storage runs on. */
|
|
11
|
+
readonly knex: Knex;
|
|
12
|
+
/** The prefix of every table the storage uses. */
|
|
13
|
+
readonly tableNamePrefix?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Registers the storage in the transport's root container, once per process: the connection and
|
|
16
|
+
* its settings (`SqlConnectionFeature`), a `RequestStorage` that registers the storages (core,
|
|
17
|
+
* audit logs, websockets) in each request's container, and its `SqlSchemaSetupRunner`. The
|
|
18
|
+
* transport runs the setup before the API takes traffic; once it ran, the storage is
|
|
19
|
+
* schema-managed: no part checks for or creates tables at runtime.
|
|
20
|
+
*/
|
|
21
|
+
register(container: Container): void;
|
|
22
|
+
}
|
package/storage/types.js
ADDED
|
File without changes
|
package/tenancy/index.js
CHANGED
|
@@ -14,15 +14,15 @@ const rowToTenant = (row)=>{
|
|
|
14
14
|
};
|
|
15
15
|
const createStorageOperations = (params)=>{
|
|
16
16
|
const { knex, tableManager } = params;
|
|
17
|
-
const ensureTable =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
const ensureTable = tableManager.define(TABLE_NAME, (table)=>{
|
|
18
|
+
table.text("id").primary().notNullable();
|
|
19
|
+
table.text("parent");
|
|
20
|
+
table.text("createdOn").notNullable();
|
|
21
|
+
table.text("data").notNullable();
|
|
22
|
+
table.index([
|
|
23
|
+
"parent"
|
|
24
|
+
]);
|
|
25
|
+
});
|
|
26
26
|
const query = ()=>knex(tableManager.resolve(TABLE_NAME));
|
|
27
27
|
return {
|
|
28
28
|
async getTenantsByIds (ids) {
|
package/tenancy/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tenancy/index.js","sources":["../../src/tenancy/index.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type {\n ListTenantsParams,\n TenancyStorageOperations,\n Tenant\n} from \"@webiny/api-core/types/tenancy.js\";\nimport WebinyError from \"@webiny/error\";\nimport type { TableManager } from \"~/TableManager.js\";\n\nconst TABLE_NAME = \"webiny_core_tenants\";\n\ninterface ITenantRow {\n id: string;\n parent: string | null;\n createdOn: string;\n data: string;\n}\n\nconst tenantToRow = (tenant: Tenant): ITenantRow => {\n return {\n id: tenant.id,\n parent: tenant.parent,\n createdOn: tenant.createdOn,\n data: JSON.stringify(tenant)\n };\n};\n\nconst rowToTenant = (row: ITenantRow): Tenant => {\n const tenant = JSON.parse(row.data) as Tenant;\n\n if (!tenant.tags) {\n tenant.tags = [];\n }\n\n if (!tenant.description) {\n tenant.description = \"\";\n }\n\n return tenant;\n};\n\ninterface CreateStorageOperationsParams {\n knex: Knex;\n tableManager: TableManager;\n}\n\nexport const createStorageOperations = (\n params: CreateStorageOperationsParams\n): TenancyStorageOperations => {\n const { knex, tableManager } = params;\n\n const ensureTable =
|
|
1
|
+
{"version":3,"file":"tenancy/index.js","sources":["../../src/tenancy/index.ts"],"sourcesContent":["import type { Knex } from \"knex\";\nimport type {\n ListTenantsParams,\n TenancyStorageOperations,\n Tenant\n} from \"@webiny/api-core/types/tenancy.js\";\nimport WebinyError from \"@webiny/error\";\nimport type { TableManager } from \"~/TableManager.js\";\n\nconst TABLE_NAME = \"webiny_core_tenants\";\n\ninterface ITenantRow {\n id: string;\n parent: string | null;\n createdOn: string;\n data: string;\n}\n\nconst tenantToRow = (tenant: Tenant): ITenantRow => {\n return {\n id: tenant.id,\n parent: tenant.parent,\n createdOn: tenant.createdOn,\n data: JSON.stringify(tenant)\n };\n};\n\nconst rowToTenant = (row: ITenantRow): Tenant => {\n const tenant = JSON.parse(row.data) as Tenant;\n\n if (!tenant.tags) {\n tenant.tags = [];\n }\n\n if (!tenant.description) {\n tenant.description = \"\";\n }\n\n return tenant;\n};\n\ninterface CreateStorageOperationsParams {\n knex: Knex;\n tableManager: TableManager;\n}\n\nexport const createStorageOperations = (\n params: CreateStorageOperationsParams\n): TenancyStorageOperations => {\n const { knex, tableManager } = params;\n\n const ensureTable = tableManager.define(TABLE_NAME, table => {\n table.text(\"id\").primary().notNullable();\n table.text(\"parent\");\n table.text(\"createdOn\").notNullable();\n table.text(\"data\").notNullable();\n\n table.index([\"parent\"]);\n });\n\n const query = () => {\n return knex<ITenantRow>(tableManager.resolve(TABLE_NAME));\n };\n\n return {\n async getTenantsByIds(ids: readonly string[]): Promise<Tenant[]> {\n await ensureTable();\n\n try {\n const rows = await query().whereIn(\"id\", ids);\n\n return rows.map(rowToTenant);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not get tenants by IDs.\",\n code: \"GET_TENANTS_BY_IDS_ERROR\",\n data: {\n ids\n }\n });\n }\n },\n\n async listTenants(params: ListTenantsParams = {}): Promise<Tenant[]> {\n await ensureTable();\n\n try {\n const qb = query();\n\n if (params.parent) {\n qb.where(\"parent\", params.parent);\n }\n\n qb.orderBy(\"createdOn\", \"asc\");\n\n const rows = await qb;\n\n return rows.map(rowToTenant);\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not list tenants.\",\n code: \"LIST_TENANTS_ERROR\",\n data: { params }\n });\n }\n },\n\n async createTenant(data: Tenant): Promise<Tenant> {\n await ensureTable();\n\n try {\n const row = tenantToRow(data);\n await query().insert(row);\n\n return data;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not create tenant record.\",\n code: \"CREATE_TENANT_ERROR\",\n data: { tenant: data }\n });\n }\n },\n\n async updateTenant(data: Tenant): Promise<Tenant> {\n await ensureTable();\n\n try {\n const row = tenantToRow(data);\n await query().where(\"id\", data.id).update(row);\n\n return data;\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not update tenant record.\",\n code: \"UPDATE_TENANT_ERROR\",\n data: { tenant: data }\n });\n }\n },\n\n async deleteTenant(id: string): Promise<void> {\n await ensureTable();\n\n try {\n await query().where(\"id\", id).delete();\n } catch (err) {\n throw WebinyError.from(err, {\n message: \"Could not delete tenant record.\",\n code: \"DELETE_TENANT_ERROR\",\n data: { id }\n });\n }\n }\n };\n};\n"],"names":["TABLE_NAME","tenantToRow","tenant","JSON","rowToTenant","row","createStorageOperations","params","knex","tableManager","ensureTable","table","query","ids","rows","err","WebinyError","qb","data","id"],"mappings":";AASA,MAAMA,aAAa;AASnB,MAAMC,cAAc,CAACC,SACV;QACH,IAAIA,OAAO,EAAE;QACb,QAAQA,OAAO,MAAM;QACrB,WAAWA,OAAO,SAAS;QAC3B,MAAMC,KAAK,SAAS,CAACD;IACzB;AAGJ,MAAME,cAAc,CAACC;IACjB,MAAMH,SAASC,KAAK,KAAK,CAACE,IAAI,IAAI;IAElC,IAAI,CAAC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BACA,MAAML,MAAMJ,YAAYiB;gBACxB,MAAMN,QAAQ,MAAM,CAACP;gBAErB,OAAOa;YACX,EAAE,OAAOH,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAE,QAAQG;oBAAK;gBACzB;YACJ;QACJ;QAEA,MAAM,cAAaA,IAAY;YAC3B,MAAMR;YAEN,IAAI;gBACA,MAAML,MAAMJ,YAAYiB;gBACxB,MAAMN,QAAQ,KAAK,CAAC,MAAMM,KAAK,EAAE,EAAE,MAAM,CAACb;gBAE1C,OAAOa;YACX,EAAE,OAAOH,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAE,QAAQG;oBAAK;gBACzB;YACJ;QACJ;QAEA,MAAM,cAAaC,EAAU;YACzB,MAAMT;YAEN,IAAI;gBACA,MAAME,QAAQ,KAAK,CAAC,MAAMO,IAAI,MAAM;YACxC,EAAE,OAAOJ,KAAK;gBACV,MAAMC,MAAAA,IAAgB,CAACD,KAAK;oBACxB,SAAS;oBACT,MAAM;oBACN,MAAM;wBAAEI;oBAAG;gBACf;YACJ;QACJ;IACJ;AACJ"}
|
package/ApiCoreSqlFeature.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Knex } from "knex";
|
|
2
|
-
export interface ApiCoreSqlConfig {
|
|
3
|
-
knex: Knex;
|
|
4
|
-
tableNamePrefix?: string;
|
|
5
|
-
}
|
|
6
|
-
/**
|
|
7
|
-
* Registers the SQL implementation of ApiCoreStorageOperationsFactory. ApiCoreFeature.register
|
|
8
|
-
* resolves + builds it. Mirrors ApiCoreDdbFeature.
|
|
9
|
-
*/
|
|
10
|
-
export declare const ApiCoreSqlFeature: {
|
|
11
|
-
name: string;
|
|
12
|
-
register(container: import("@webiny/di").Container, context: ApiCoreSqlConfig): void;
|
|
13
|
-
};
|
package/ApiCoreSqlFeature.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { createFeature } from "@webiny/feature/api";
|
|
2
|
-
import { ApiCoreStorageOperationsFactory } from "@webiny/api-core";
|
|
3
|
-
import { createApiCoreSql } from "./createApiCoreSql.js";
|
|
4
|
-
const ApiCoreSqlFeature = createFeature({
|
|
5
|
-
name: "ApiCoreSql",
|
|
6
|
-
register (container, { knex, tableNamePrefix }) {
|
|
7
|
-
container.registerInstance(ApiCoreStorageOperationsFactory, {
|
|
8
|
-
create: ()=>createApiCoreSql({
|
|
9
|
-
knex,
|
|
10
|
-
tableNamePrefix
|
|
11
|
-
})
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
export { ApiCoreSqlFeature };
|
|
16
|
-
|
|
17
|
-
//# sourceMappingURL=ApiCoreSqlFeature.js.map
|
package/ApiCoreSqlFeature.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ApiCoreSqlFeature.js","sources":["../src/ApiCoreSqlFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport type { Knex } from \"knex\";\nimport { ApiCoreStorageOperationsFactory } from \"@webiny/api-core\";\nimport { createApiCoreSql } from \"./createApiCoreSql.js\";\n\nexport interface ApiCoreSqlConfig {\n knex: Knex;\n tableNamePrefix?: string;\n}\n\n/**\n * Registers the SQL implementation of ApiCoreStorageOperationsFactory. ApiCoreFeature.register\n * resolves + builds it. Mirrors ApiCoreDdbFeature.\n */\nexport const ApiCoreSqlFeature = createFeature<ApiCoreSqlConfig>({\n name: \"ApiCoreSql\",\n register(container, { knex, tableNamePrefix }) {\n container.registerInstance(ApiCoreStorageOperationsFactory, {\n create: () => createApiCoreSql({ knex, tableNamePrefix })\n });\n }\n});\n"],"names":["ApiCoreSqlFeature","createFeature","container","knex","tableNamePrefix","ApiCoreStorageOperationsFactory","createApiCoreSql"],"mappings":";;;AAcO,MAAMA,oBAAoBC,cAAgC;IAC7D,MAAM;IACN,UAASC,SAAS,EAAE,EAAEC,IAAI,EAAEC,eAAe,EAAE;QACzCF,UAAU,gBAAgB,CAACG,iCAAiC;YACxD,QAAQ,IAAMC,iBAAiB;oBAAEH;oBAAMC;gBAAgB;QAC3D;IACJ;AACJ"}
|