@webiny/api-core-sql 0.0.0-unstable.0d717d18dd
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/ApiCoreSqlFeature.d.ts +13 -0
- package/ApiCoreSqlFeature.js +17 -0
- package/ApiCoreSqlFeature.js.map +1 -0
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/TableManager.d.ts +16 -0
- package/TableManager.js +26 -0
- package/TableManager.js.map +1 -0
- package/adminUsers/index.d.ts +9 -0
- package/adminUsers/index.js +117 -0
- package/adminUsers/index.js.map +1 -0
- package/createApiCoreSql.d.ts +8 -0
- package/createApiCoreSql.js +32 -0
- package/createApiCoreSql.js.map +1 -0
- package/feature/KnexClient/KnexClient.d.ts +9 -0
- package/feature/KnexClient/KnexClient.js +8 -0
- package/feature/KnexClient/KnexClient.js.map +1 -0
- package/feature/KnexClient/abstractions.d.ts +8 -0
- package/feature/KnexClient/abstractions.js +5 -0
- package/feature/KnexClient/abstractions.js.map +1 -0
- package/feature/KnexClient/index.d.ts +6 -0
- package/feature/KnexClient/index.js +15 -0
- package/feature/KnexClient/index.js.map +1 -0
- package/getSqlTablePrefix.d.ts +1 -0
- package/getSqlTablePrefix.js +4 -0
- package/getSqlTablePrefix.js.map +1 -0
- package/index.d.ts +11 -0
- package/index.js +12 -0
- package/index.js.map +1 -0
- package/keyValueStore/index.d.ts +9 -0
- package/keyValueStore/index.js +84 -0
- package/keyValueStore/index.js.map +1 -0
- package/package.json +43 -0
- package/security/index.d.ts +9 -0
- package/security/index.js +345 -0
- package/security/index.js.map +1 -0
- package/serviceDiscovery/SqlServiceManifestLoader.d.ts +13 -0
- package/serviceDiscovery/SqlServiceManifestLoader.js +25 -0
- package/serviceDiscovery/SqlServiceManifestLoader.js.map +1 -0
- package/serviceDiscovery/index.d.ts +1 -0
- package/serviceDiscovery/index.js +1 -0
- package/sortItems.d.ts +1 -0
- package/sortItems.js +31 -0
- package/sortItems.js.map +1 -0
- package/tenancy/index.d.ts +9 -0
- package/tenancy/index.js +111 -0
- package/tenancy/index.js.map +1 -0
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import error from "@webiny/error";
|
|
2
|
+
import { sortItems } from "../sortItems.js";
|
|
3
|
+
const ROLES_TABLE = "webiny_core_roles";
|
|
4
|
+
const TEAMS_TABLE = "webiny_core_teams";
|
|
5
|
+
const API_KEYS_TABLE = "webiny_core_api_keys";
|
|
6
|
+
const createStorageOperations = (params)=>{
|
|
7
|
+
const { knex, tableManager } = params;
|
|
8
|
+
const ensureRolesTable = ()=>tableManager.ensure(ROLES_TABLE, (table)=>{
|
|
9
|
+
table.text("id").notNullable();
|
|
10
|
+
table.text("tenant").notNullable();
|
|
11
|
+
table.text("slug").notNullable();
|
|
12
|
+
table.text("data").notNullable();
|
|
13
|
+
table.primary([
|
|
14
|
+
"tenant",
|
|
15
|
+
"id"
|
|
16
|
+
]);
|
|
17
|
+
table.unique([
|
|
18
|
+
"tenant",
|
|
19
|
+
"slug"
|
|
20
|
+
]);
|
|
21
|
+
});
|
|
22
|
+
const ensureTeamsTable = ()=>tableManager.ensure(TEAMS_TABLE, (table)=>{
|
|
23
|
+
table.text("id").notNullable();
|
|
24
|
+
table.text("tenant").notNullable();
|
|
25
|
+
table.text("slug").notNullable();
|
|
26
|
+
table.text("data").notNullable();
|
|
27
|
+
table.primary([
|
|
28
|
+
"tenant",
|
|
29
|
+
"id"
|
|
30
|
+
]);
|
|
31
|
+
table.unique([
|
|
32
|
+
"tenant",
|
|
33
|
+
"slug"
|
|
34
|
+
]);
|
|
35
|
+
});
|
|
36
|
+
const ensureApiKeysTable = ()=>tableManager.ensure(API_KEYS_TABLE, (table)=>{
|
|
37
|
+
table.text("id").notNullable();
|
|
38
|
+
table.text("tenant").notNullable();
|
|
39
|
+
table.text("token").notNullable();
|
|
40
|
+
table.text("slug").notNullable();
|
|
41
|
+
table.text("data").notNullable();
|
|
42
|
+
table.primary([
|
|
43
|
+
"tenant",
|
|
44
|
+
"id"
|
|
45
|
+
]);
|
|
46
|
+
table.unique([
|
|
47
|
+
"tenant",
|
|
48
|
+
"token"
|
|
49
|
+
]);
|
|
50
|
+
table.unique([
|
|
51
|
+
"tenant",
|
|
52
|
+
"slug"
|
|
53
|
+
]);
|
|
54
|
+
});
|
|
55
|
+
const rolesQuery = ()=>knex(tableManager.resolve(ROLES_TABLE));
|
|
56
|
+
const teamsQuery = ()=>knex(tableManager.resolve(TEAMS_TABLE));
|
|
57
|
+
const apiKeysQuery = ()=>knex(tableManager.resolve(API_KEYS_TABLE));
|
|
58
|
+
return {
|
|
59
|
+
async getRole ({ where: { tenant, id, slug } }) {
|
|
60
|
+
await ensureRolesTable();
|
|
61
|
+
try {
|
|
62
|
+
if (id) {
|
|
63
|
+
const row = await rolesQuery().where("tenant", tenant).andWhere("id", id).first();
|
|
64
|
+
return row ? JSON.parse(row.data) : null;
|
|
65
|
+
}
|
|
66
|
+
const row = await rolesQuery().where("tenant", tenant).andWhere("slug", slug).first();
|
|
67
|
+
return row ? JSON.parse(row.data) : null;
|
|
68
|
+
} catch (err) {
|
|
69
|
+
throw error.from(err, {
|
|
70
|
+
message: "Could not load role.",
|
|
71
|
+
code: "GET_ROLE_ERROR",
|
|
72
|
+
data: {
|
|
73
|
+
id,
|
|
74
|
+
slug
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
async listRoles ({ where: { tenant, id_in, slug_in }, sort }) {
|
|
80
|
+
await ensureRolesTable();
|
|
81
|
+
try {
|
|
82
|
+
const qb = rolesQuery().where("tenant", tenant);
|
|
83
|
+
if (Array.isArray(id_in)) qb.whereIn("id", id_in);
|
|
84
|
+
else if (Array.isArray(slug_in)) qb.whereIn("slug", slug_in);
|
|
85
|
+
const rows = await qb;
|
|
86
|
+
const items = rows.map((row)=>JSON.parse(row.data));
|
|
87
|
+
return sortItems(items, sort);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
throw error.from(err, {
|
|
90
|
+
message: "Could not list roles.",
|
|
91
|
+
code: "LIST_ROLE_ERROR"
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
async createRole ({ role }) {
|
|
96
|
+
await ensureRolesTable();
|
|
97
|
+
try {
|
|
98
|
+
await rolesQuery().insert({
|
|
99
|
+
id: role.id,
|
|
100
|
+
tenant: role.tenant,
|
|
101
|
+
slug: role.slug,
|
|
102
|
+
data: JSON.stringify(role)
|
|
103
|
+
});
|
|
104
|
+
} catch (err) {
|
|
105
|
+
throw error.from(err, {
|
|
106
|
+
message: "Could not create role.",
|
|
107
|
+
code: "CREATE_ROLE_ERROR",
|
|
108
|
+
data: {
|
|
109
|
+
role
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
async updateRole ({ role }) {
|
|
115
|
+
await ensureRolesTable();
|
|
116
|
+
try {
|
|
117
|
+
await rolesQuery().where("tenant", role.tenant).andWhere("id", role.id).update({
|
|
118
|
+
slug: role.slug,
|
|
119
|
+
data: JSON.stringify(role)
|
|
120
|
+
});
|
|
121
|
+
} catch (err) {
|
|
122
|
+
throw error.from(err, {
|
|
123
|
+
message: "Could not update role.",
|
|
124
|
+
code: "UPDATE_ROLE_ERROR",
|
|
125
|
+
data: {
|
|
126
|
+
role
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
async deleteRole ({ role }) {
|
|
132
|
+
await ensureRolesTable();
|
|
133
|
+
try {
|
|
134
|
+
await rolesQuery().where("tenant", role.tenant).andWhere("id", role.id).delete();
|
|
135
|
+
} catch (err) {
|
|
136
|
+
throw error.from(err, {
|
|
137
|
+
message: "Could not delete role.",
|
|
138
|
+
code: "DELETE_ROLE_ERROR",
|
|
139
|
+
data: {
|
|
140
|
+
role
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
async getTeam ({ where: { tenant, id, slug } }) {
|
|
146
|
+
await ensureTeamsTable();
|
|
147
|
+
try {
|
|
148
|
+
if (id) {
|
|
149
|
+
const row = await teamsQuery().where("tenant", tenant).andWhere("id", id).first();
|
|
150
|
+
return row ? JSON.parse(row.data) : null;
|
|
151
|
+
}
|
|
152
|
+
const row = await teamsQuery().where("tenant", tenant).andWhere("slug", slug).first();
|
|
153
|
+
return row ? JSON.parse(row.data) : null;
|
|
154
|
+
} catch (err) {
|
|
155
|
+
throw error.from(err, {
|
|
156
|
+
message: "Could not load team.",
|
|
157
|
+
code: "GET_TEAM_ERROR",
|
|
158
|
+
data: {
|
|
159
|
+
id,
|
|
160
|
+
slug
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
async listTeams ({ where: { tenant, id_in, slug_in }, sort }) {
|
|
166
|
+
await ensureTeamsTable();
|
|
167
|
+
try {
|
|
168
|
+
const qb = teamsQuery().where("tenant", tenant);
|
|
169
|
+
if (Array.isArray(id_in)) qb.whereIn("id", id_in);
|
|
170
|
+
else if (Array.isArray(slug_in)) qb.whereIn("slug", slug_in);
|
|
171
|
+
const rows = await qb;
|
|
172
|
+
const items = rows.map((row)=>JSON.parse(row.data));
|
|
173
|
+
return sortItems(items, sort);
|
|
174
|
+
} catch (err) {
|
|
175
|
+
throw error.from(err, {
|
|
176
|
+
message: "Could not list teams.",
|
|
177
|
+
code: "LIST_TEAM_ERROR"
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
async createTeam ({ team }) {
|
|
182
|
+
await ensureTeamsTable();
|
|
183
|
+
try {
|
|
184
|
+
await teamsQuery().insert({
|
|
185
|
+
id: team.id,
|
|
186
|
+
tenant: team.tenant,
|
|
187
|
+
slug: team.slug,
|
|
188
|
+
data: JSON.stringify(team)
|
|
189
|
+
});
|
|
190
|
+
} catch (err) {
|
|
191
|
+
throw error.from(err, {
|
|
192
|
+
message: "Could not create team.",
|
|
193
|
+
code: "CREATE_TEAM_ERROR",
|
|
194
|
+
data: {
|
|
195
|
+
team
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
async updateTeam ({ team }) {
|
|
201
|
+
await ensureTeamsTable();
|
|
202
|
+
try {
|
|
203
|
+
await teamsQuery().where("tenant", team.tenant).andWhere("id", team.id).update({
|
|
204
|
+
slug: team.slug,
|
|
205
|
+
data: JSON.stringify(team)
|
|
206
|
+
});
|
|
207
|
+
} catch (err) {
|
|
208
|
+
throw error.from(err, {
|
|
209
|
+
message: "Could not update team.",
|
|
210
|
+
code: "UPDATE_TEAM_ERROR",
|
|
211
|
+
data: {
|
|
212
|
+
team
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
async deleteTeam ({ team }) {
|
|
218
|
+
await ensureTeamsTable();
|
|
219
|
+
try {
|
|
220
|
+
await teamsQuery().where("tenant", team.tenant).andWhere("id", team.id).delete();
|
|
221
|
+
} catch (err) {
|
|
222
|
+
throw error.from(err, {
|
|
223
|
+
message: "Could not delete team.",
|
|
224
|
+
code: "DELETE_TEAM_ERROR",
|
|
225
|
+
data: {
|
|
226
|
+
team
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
async getApiKey ({ id, tenant }) {
|
|
232
|
+
await ensureApiKeysTable();
|
|
233
|
+
try {
|
|
234
|
+
const row = await apiKeysQuery().where("tenant", tenant).andWhere("id", id).first();
|
|
235
|
+
return row ? JSON.parse(row.data) : null;
|
|
236
|
+
} catch (err) {
|
|
237
|
+
throw error.from(err, {
|
|
238
|
+
message: "Could not load api key.",
|
|
239
|
+
code: "GET_API_KEY_ERROR",
|
|
240
|
+
data: {
|
|
241
|
+
id
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
async getApiKeyByToken ({ tenant, token }) {
|
|
247
|
+
await ensureApiKeysTable();
|
|
248
|
+
try {
|
|
249
|
+
const row = await apiKeysQuery().where("tenant", tenant).andWhere("token", token).first();
|
|
250
|
+
return row ? JSON.parse(row.data) : null;
|
|
251
|
+
} catch (err) {
|
|
252
|
+
throw error.from(err, {
|
|
253
|
+
message: "Could not load api key by token.",
|
|
254
|
+
code: "GET_BY_TOKEN_API_KEY_ERROR",
|
|
255
|
+
data: {
|
|
256
|
+
token
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
async getApiKeyBySlug ({ tenant, slug }) {
|
|
262
|
+
await ensureApiKeysTable();
|
|
263
|
+
try {
|
|
264
|
+
const row = await apiKeysQuery().where("tenant", tenant).andWhere("slug", slug).first();
|
|
265
|
+
return row ? JSON.parse(row.data) : null;
|
|
266
|
+
} catch (err) {
|
|
267
|
+
throw error.from(err, {
|
|
268
|
+
message: "Could not load api key by slug.",
|
|
269
|
+
code: "GET_BY_SLUG_API_KEY_ERROR",
|
|
270
|
+
data: {
|
|
271
|
+
slug
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
async listApiKeys ({ where: { tenant }, sort }) {
|
|
277
|
+
await ensureApiKeysTable();
|
|
278
|
+
try {
|
|
279
|
+
const rows = await apiKeysQuery().where("tenant", tenant);
|
|
280
|
+
const items = rows.map((row)=>JSON.parse(row.data));
|
|
281
|
+
return sortItems(items, sort);
|
|
282
|
+
} catch (err) {
|
|
283
|
+
throw error.from(err, {
|
|
284
|
+
message: "Could not list api keys.",
|
|
285
|
+
code: "LIST_API_KEY_ERROR"
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
},
|
|
289
|
+
async createApiKey ({ apiKey }) {
|
|
290
|
+
await ensureApiKeysTable();
|
|
291
|
+
try {
|
|
292
|
+
await apiKeysQuery().insert({
|
|
293
|
+
id: apiKey.id,
|
|
294
|
+
tenant: apiKey.tenant,
|
|
295
|
+
token: apiKey.token,
|
|
296
|
+
slug: apiKey.slug,
|
|
297
|
+
data: JSON.stringify(apiKey)
|
|
298
|
+
});
|
|
299
|
+
} catch (err) {
|
|
300
|
+
throw error.from(err, {
|
|
301
|
+
message: "Could not create api key.",
|
|
302
|
+
code: "CREATE_API_KEY_ERROR",
|
|
303
|
+
data: {
|
|
304
|
+
apiKey
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
},
|
|
309
|
+
async updateApiKey ({ apiKey }) {
|
|
310
|
+
await ensureApiKeysTable();
|
|
311
|
+
try {
|
|
312
|
+
await apiKeysQuery().where("tenant", apiKey.tenant).andWhere("id", apiKey.id).update({
|
|
313
|
+
token: apiKey.token,
|
|
314
|
+
slug: apiKey.slug,
|
|
315
|
+
data: JSON.stringify(apiKey)
|
|
316
|
+
});
|
|
317
|
+
} catch (err) {
|
|
318
|
+
throw error.from(err, {
|
|
319
|
+
message: "Could not update api key.",
|
|
320
|
+
code: "UPDATE_API_KEY_ERROR",
|
|
321
|
+
data: {
|
|
322
|
+
apiKey
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
async deleteApiKey ({ apiKey }) {
|
|
328
|
+
await ensureApiKeysTable();
|
|
329
|
+
try {
|
|
330
|
+
await apiKeysQuery().where("tenant", apiKey.tenant).andWhere("id", apiKey.id).delete();
|
|
331
|
+
} catch (err) {
|
|
332
|
+
throw error.from(err, {
|
|
333
|
+
message: "Could not delete api key.",
|
|
334
|
+
code: "DELETE_API_KEY_ERROR",
|
|
335
|
+
data: {
|
|
336
|
+
apiKey
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
};
|
|
343
|
+
export { createStorageOperations };
|
|
344
|
+
|
|
345
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,0BAA0B,CACnCC;IAEA,MAAM,EAAEC,IAAI,EAAEC,YAAY,EAAE,GAAGF;IAE/B,MAAMG,mBAAmB,I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AAMtB;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"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
import type { IServiceManifestLoader } from "@webiny/api-core/features/serviceDiscovery/index.js";
|
|
3
|
+
import type { TableManager } from "../TableManager.js";
|
|
4
|
+
export declare class SqlServiceManifestLoader implements IServiceManifestLoader {
|
|
5
|
+
private readonly knex;
|
|
6
|
+
private readonly tableManager;
|
|
7
|
+
constructor(knex: Knex, tableManager: TableManager);
|
|
8
|
+
load(): Promise<{
|
|
9
|
+
name: string;
|
|
10
|
+
manifest: any;
|
|
11
|
+
}[] | undefined>;
|
|
12
|
+
private ensureTable;
|
|
13
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const TABLE_NAME = "webiny_service_manifests";
|
|
2
|
+
class SqlServiceManifestLoader {
|
|
3
|
+
constructor(knex, tableManager){
|
|
4
|
+
this.knex = knex;
|
|
5
|
+
this.tableManager = tableManager;
|
|
6
|
+
}
|
|
7
|
+
async load() {
|
|
8
|
+
await this.ensureTable();
|
|
9
|
+
const rows = await this.knex(this.tableManager.resolve(TABLE_NAME)).select("name", "manifest");
|
|
10
|
+
if (!rows.length) return;
|
|
11
|
+
return rows.map((row)=>({
|
|
12
|
+
name: row.name,
|
|
13
|
+
manifest: JSON.parse(row.manifest)
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
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
|
+
}
|
|
23
|
+
export { SqlServiceManifestLoader };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=SqlServiceManifestLoader.js.map
|
|
@@ -0,0 +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\";\n\nconst TABLE_NAME = \"webiny_service_manifests\";\n\ninterface IServiceManifestRow {\n name: string;\n manifest: string;\n}\n\nexport class SqlServiceManifestLoader implements IServiceManifestLoader {\n private readonly knex: Knex;\n private readonly tableManager: TableManager;\n\n constructor(knex: Knex, tableManager: TableManager) {\n this.knex = knex;\n this.tableManager = tableManager;\n }\n\n async load() {\n await this.ensureTable();\n\n const rows = await this.knex<IServiceManifestRow>(\n this.tableManager.resolve(TABLE_NAME)\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 private async ensureTable() {\n await this.tableManager.ensure(TABLE_NAME, table => {\n table.text(\"name\").primary().notNullable();\n table.text(\"manifest\").notNullable();\n });\n }\n}\n"],"names":["TABLE_NAME","SqlServiceManifestLoader","knex","tableManager","rows","row","JSON","table"],"mappings":"AAIA,MAAMA,aAAa;AAOZ,MAAMC;IAIT,YAAYC,IAAU,EAAEC,YAA0B,CAAE;QAChD,IAAI,CAAC,IAAI,GAAGD;QACZ,IAAI,CAAC,YAAY,GAAGC;IACxB;IAEA,MAAM,OAAO;QACT,MAAM,IAAI,CAAC,WAAW;QAEtB,MAAMC,OAAO,MAAM,IAAI,CAAC,IAAI,CACxB,IAAI,CAAC,YAAY,CAAC,OAAO,CAACJ,aAC5B,MAAM,CAAC,QAAQ;QAEjB,IAAI,CAACI,KAAK,MAAM,EACZ;QAGJ,OAAOA,KAAK,GAAG,CAACC,CAAAA,MAAQ;gBACpB,MAAMA,IAAI,IAAI;gBACd,UAAUC,KAAK,KAAK,CAACD,IAAI,QAAQ;YACrC;IACJ;IAEA,MAAc,cAAc;QACxB,MAAM,IAAI,CAAC,YAAY,CAAC,MAAM,CAACL,YAAYO,CAAAA;YACvCA,MAAM,IAAI,CAAC,QAAQ,OAAO,GAAG,WAAW;YACxCA,MAAM,IAAI,CAAC,YAAY,WAAW;QACtC;IACJ;AACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SqlServiceManifestLoader } from "./SqlServiceManifestLoader.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SqlServiceManifestLoader } from "./SqlServiceManifestLoader.js";
|
package/sortItems.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const sortItems: <T>(items: T[], sort?: string[]) => T[];
|
package/sortItems.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const sortItems = (items, sort)=>{
|
|
2
|
+
if (!sort?.length || items.length <= 1) return items;
|
|
3
|
+
const parsed = sort.filter(Boolean).map((s)=>{
|
|
4
|
+
const parts = s.split("_");
|
|
5
|
+
const direction = parts.pop()?.toUpperCase() === "ASC" ? 1 : -1;
|
|
6
|
+
const field = parts.join("_");
|
|
7
|
+
return {
|
|
8
|
+
field,
|
|
9
|
+
direction
|
|
10
|
+
};
|
|
11
|
+
}).filter(({ field })=>field.length > 0);
|
|
12
|
+
if (0 === parsed.length) return items;
|
|
13
|
+
return [
|
|
14
|
+
...items
|
|
15
|
+
].sort((a, b)=>{
|
|
16
|
+
for (const { field, direction } of parsed){
|
|
17
|
+
const va = a[field];
|
|
18
|
+
const vb = b[field];
|
|
19
|
+
if (null != va || null != vb) {
|
|
20
|
+
if (null == va) return direction;
|
|
21
|
+
if (null == vb) return -direction;
|
|
22
|
+
if (va < vb) return -1 * direction;
|
|
23
|
+
if (va > vb) return +direction;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return 0;
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
export { sortItems };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=sortItems.js.map
|
package/sortItems.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sortItems.js","sources":["../src/sortItems.ts"],"sourcesContent":["export const sortItems = <T>(items: T[], sort?: string[]): T[] => {\n if (!sort?.length || items.length <= 1) {\n return items;\n }\n\n const parsed = sort\n .filter(Boolean)\n .map(s => {\n const parts = s.split(\"_\");\n const direction = parts.pop()?.toUpperCase() === \"ASC\" ? 1 : -1;\n const field = parts.join(\"_\");\n return { field, direction };\n })\n .filter(({ field }) => field.length > 0);\n\n if (parsed.length === 0) {\n return items;\n }\n\n return [...items].sort((a, b) => {\n for (const { field, direction } of parsed) {\n const va = (a as Record<string, unknown>)[field];\n const vb = (b as Record<string, unknown>)[field];\n\n if (va == null && vb == null) {\n continue;\n }\n if (va == null) {\n return direction;\n }\n if (vb == null) {\n return -direction;\n }\n if (va < vb) {\n return -1 * direction;\n }\n if (va > vb) {\n return 1 * direction;\n }\n }\n return 0;\n });\n};\n"],"names":["sortItems","items","sort","parsed","Boolean","s","parts","direction","field","a","b","va","vb"],"mappings":"AAAO,MAAMA,YAAY,CAAIC,OAAYC;IACrC,IAAI,CAACA,MAAM,UAAUD,MAAM,MAAM,IAAI,GACjC,OAAOA;IAGX,MAAME,SAASD,KACV,MAAM,CAACE,SACP,GAAG,CAACC,CAAAA;QACD,MAAMC,QAAQD,EAAE,KAAK,CAAC;QACtB,MAAME,YAAYD,MAAM,GAAG,IAAI,kBAAkB,QAAQ,IAAI;QAC7D,MAAME,QAAQF,MAAM,IAAI,CAAC;QACzB,OAAO;YAAEE;YAAOD;QAAU;IAC9B,GACC,MAAM,CAAC,CAAC,EAAEC,KAAK,EAAE,GAAKA,MAAM,MAAM,GAAG;IAE1C,IAAIL,AAAkB,MAAlBA,OAAO,MAAM,EACb,OAAOF;IAGX,OAAO;WAAIA;KAAM,CAAC,IAAI,CAAC,CAACQ,GAAGC;QACvB,KAAK,MAAM,EAAEF,KAAK,EAAED,SAAS,EAAE,IAAIJ,OAAQ;YACvC,MAAMQ,KAAMF,CAA6B,CAACD,MAAM;YAChD,MAAMI,KAAMF,CAA6B,CAACF,MAAM;YAEhD,IAAIG,AAAM,QAANA,MAAcC,AAAM,QAANA;gBAGlB,IAAID,AAAM,QAANA,IACA,OAAOJ;gBAEX,IAAIK,AAAM,QAANA,IACA,OAAO,CAACL;gBAEZ,IAAII,KAAKC,IACL,OAAO,KAAKL;gBAEhB,IAAII,KAAKC,IACL,OAAO,CAAIL;;QAEnB;QACA,OAAO;IACX;AACJ"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Knex } from "knex";
|
|
2
|
+
import type { TenancyStorageOperations } from "@webiny/api-core/types/tenancy.js";
|
|
3
|
+
import type { TableManager } from "../TableManager.js";
|
|
4
|
+
interface CreateStorageOperationsParams {
|
|
5
|
+
knex: Knex;
|
|
6
|
+
tableManager: TableManager;
|
|
7
|
+
}
|
|
8
|
+
export declare const createStorageOperations: (params: CreateStorageOperationsParams) => TenancyStorageOperations;
|
|
9
|
+
export {};
|
package/tenancy/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import error from "@webiny/error";
|
|
2
|
+
const TABLE_NAME = "webiny_core_tenants";
|
|
3
|
+
const tenantToRow = (tenant)=>({
|
|
4
|
+
id: tenant.id,
|
|
5
|
+
parent: tenant.parent,
|
|
6
|
+
createdOn: tenant.createdOn,
|
|
7
|
+
data: JSON.stringify(tenant)
|
|
8
|
+
});
|
|
9
|
+
const rowToTenant = (row)=>{
|
|
10
|
+
const tenant = JSON.parse(row.data);
|
|
11
|
+
if (!tenant.tags) tenant.tags = [];
|
|
12
|
+
if (!tenant.description) tenant.description = "";
|
|
13
|
+
return tenant;
|
|
14
|
+
};
|
|
15
|
+
const createStorageOperations = (params)=>{
|
|
16
|
+
const { knex, tableManager } = params;
|
|
17
|
+
const ensureTable = ()=>tableManager.ensure(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
|
+
const query = ()=>knex(tableManager.resolve(TABLE_NAME));
|
|
27
|
+
return {
|
|
28
|
+
async getTenantsByIds (ids) {
|
|
29
|
+
await ensureTable();
|
|
30
|
+
try {
|
|
31
|
+
const rows = await query().whereIn("id", ids);
|
|
32
|
+
return rows.map(rowToTenant);
|
|
33
|
+
} catch (err) {
|
|
34
|
+
throw error.from(err, {
|
|
35
|
+
message: "Could not get tenants by IDs.",
|
|
36
|
+
code: "GET_TENANTS_BY_IDS_ERROR",
|
|
37
|
+
data: {
|
|
38
|
+
ids
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
async listTenants (params = {}) {
|
|
44
|
+
await ensureTable();
|
|
45
|
+
try {
|
|
46
|
+
const qb = query();
|
|
47
|
+
if (params.parent) qb.where("parent", params.parent);
|
|
48
|
+
qb.orderBy("createdOn", "asc");
|
|
49
|
+
const rows = await qb;
|
|
50
|
+
return rows.map(rowToTenant);
|
|
51
|
+
} catch (err) {
|
|
52
|
+
throw error.from(err, {
|
|
53
|
+
message: "Could not list tenants.",
|
|
54
|
+
code: "LIST_TENANTS_ERROR",
|
|
55
|
+
data: {
|
|
56
|
+
params
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
async createTenant (data) {
|
|
62
|
+
await ensureTable();
|
|
63
|
+
try {
|
|
64
|
+
const row = tenantToRow(data);
|
|
65
|
+
await query().insert(row);
|
|
66
|
+
return data;
|
|
67
|
+
} catch (err) {
|
|
68
|
+
throw error.from(err, {
|
|
69
|
+
message: "Could not create tenant record.",
|
|
70
|
+
code: "CREATE_TENANT_ERROR",
|
|
71
|
+
data: {
|
|
72
|
+
tenant: data
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
async updateTenant (data) {
|
|
78
|
+
await ensureTable();
|
|
79
|
+
try {
|
|
80
|
+
const row = tenantToRow(data);
|
|
81
|
+
await query().where("id", data.id).update(row);
|
|
82
|
+
return data;
|
|
83
|
+
} catch (err) {
|
|
84
|
+
throw error.from(err, {
|
|
85
|
+
message: "Could not update tenant record.",
|
|
86
|
+
code: "UPDATE_TENANT_ERROR",
|
|
87
|
+
data: {
|
|
88
|
+
tenant: data
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
async deleteTenant (id) {
|
|
94
|
+
await ensureTable();
|
|
95
|
+
try {
|
|
96
|
+
await query().where("id", id).delete();
|
|
97
|
+
} catch (err) {
|
|
98
|
+
throw error.from(err, {
|
|
99
|
+
message: "Could not delete tenant record.",
|
|
100
|
+
code: "DELETE_TENANT_ERROR",
|
|
101
|
+
data: {
|
|
102
|
+
id
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
export { createStorageOperations };
|
|
110
|
+
|
|
111
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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 = () => {\n return tableManager.ensure(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\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,CAACH,OAAO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