@twin.org/entity-storage-service 0.0.2-next.9 → 0.0.3-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/es/entityStorageRoutes.js +227 -0
  2. package/dist/es/entityStorageRoutes.js.map +1 -0
  3. package/dist/es/entityStorageService.js +122 -0
  4. package/dist/es/entityStorageService.js.map +1 -0
  5. package/dist/es/index.js +9 -0
  6. package/dist/es/index.js.map +1 -0
  7. package/dist/es/models/IEntityStorageRoutesExamples.js +2 -0
  8. package/dist/es/models/IEntityStorageRoutesExamples.js.map +1 -0
  9. package/dist/es/models/IEntityStorageServiceConfig.js +4 -0
  10. package/dist/es/models/IEntityStorageServiceConfig.js.map +1 -0
  11. package/dist/es/models/IEntityStorageServiceConstructorOptions.js +2 -0
  12. package/dist/es/models/IEntityStorageServiceConstructorOptions.js.map +1 -0
  13. package/dist/es/restEntryPoints.js +15 -0
  14. package/dist/es/restEntryPoints.js.map +1 -0
  15. package/dist/types/entityStorageRoutes.d.ts +1 -1
  16. package/dist/types/entityStorageService.d.ts +13 -12
  17. package/dist/types/index.d.ts +6 -6
  18. package/dist/types/models/IEntityStorageServiceConfig.d.ts +5 -0
  19. package/dist/types/models/IEntityStorageServiceConstructorOptions.d.ts +2 -2
  20. package/docs/changelog.md +45 -0
  21. package/docs/open-api/spec.json +14 -279
  22. package/docs/reference/classes/EntityStorageService.md +24 -34
  23. package/docs/reference/index.md +1 -1
  24. package/docs/reference/interfaces/IEntityStorageServiceConfig.md +3 -0
  25. package/docs/reference/interfaces/IEntityStorageServiceConstructorOptions.md +1 -1
  26. package/locales/en.json +1 -1
  27. package/package.json +11 -9
  28. package/dist/cjs/index.cjs +0 -421
  29. package/dist/esm/index.mjs +0 -412
  30. package/dist/types/models/IEntityStorageConfig.d.ts +0 -10
  31. package/docs/reference/interfaces/IEntityStorageConfig.md +0 -17
@@ -1,421 +0,0 @@
1
- 'use strict';
2
-
3
- var apiModels = require('@twin.org/api-models');
4
- var core = require('@twin.org/core');
5
- var web = require('@twin.org/web');
6
- var entity = require('@twin.org/entity');
7
- var entityStorageModels = require('@twin.org/entity-storage-models');
8
-
9
- // Copyright 2024 IOTA Stiftung.
10
- // SPDX-License-Identifier: Apache-2.0.
11
- /**
12
- * The source used when communicating about these routes.
13
- */
14
- const ROUTES_SOURCE = "entityStorageRoutes";
15
- /**
16
- * The tag to associate with the routes.
17
- */
18
- const tagsEntityStorage = [
19
- {
20
- name: "EntityStorage",
21
- description: "Endpoints which are modelled to access an entity storage contract."
22
- }
23
- ];
24
- /**
25
- * The REST routes for entity storage.
26
- * @param baseRouteName Prefix to prepend to the paths.
27
- * @param componentName The name of the component to use in the routes stored in the ComponentFactory.
28
- * @param options Additional options for the routes.
29
- * @param options.typeName Optional type name to use in the routes, defaults to Entity Storage.
30
- * @param options.tagName Optional name to use in OpenAPI spec for tag.
31
- * @param options.examples Optional examples to use in the routes.
32
- * @returns The generated routes.
33
- */
34
- function generateRestRoutesEntityStorage(baseRouteName, componentName, options) {
35
- const typeName = options?.typeName ?? "Entity Storage";
36
- const lowerName = typeName.toLowerCase();
37
- const camelTypeName = core.StringHelper.camelCase(typeName);
38
- const setRoute = {
39
- operationId: `${camelTypeName}Set`,
40
- summary: `Set an entry in ${lowerName}.`,
41
- tag: options?.tagName ?? tagsEntityStorage[0].name,
42
- method: "POST",
43
- path: `${baseRouteName}/`,
44
- handler: async (httpRequestContext, request) => entityStorageSet(httpRequestContext, componentName, request),
45
- requestType: {
46
- type: "IEntityStorageSetRequest",
47
- examples: options?.examples?.set?.requestExamples ?? [
48
- {
49
- id: `${camelTypeName}SetRequestExample`,
50
- request: {
51
- body: {
52
- id: "12345",
53
- name: "My Item"
54
- }
55
- }
56
- }
57
- ]
58
- },
59
- responseType: [
60
- {
61
- type: "INoContentResponse"
62
- }
63
- ]
64
- };
65
- const getRoute = {
66
- operationId: `${camelTypeName}Get`,
67
- summary: `Get an entry from ${lowerName}.`,
68
- tag: options?.tagName ?? tagsEntityStorage[0].name,
69
- method: "GET",
70
- path: `${baseRouteName}/:id`,
71
- handler: async (httpRequestContext, request) => entityStorageGet(httpRequestContext, componentName, request),
72
- requestType: {
73
- type: "IEntityStorageGetRequest",
74
- examples: options?.examples?.get?.requestExamples ?? [
75
- {
76
- id: `${camelTypeName}GetRequestExample`,
77
- request: {
78
- pathParams: {
79
- id: "12345"
80
- }
81
- }
82
- }
83
- ]
84
- },
85
- responseType: [
86
- {
87
- type: "IEntityStorageGetResponse",
88
- examples: options?.examples?.get?.responseExamples ?? [
89
- {
90
- id: `${camelTypeName}GetResponseExample`,
91
- response: {
92
- body: {
93
- id: "12345",
94
- name: "My Item"
95
- }
96
- }
97
- }
98
- ]
99
- },
100
- {
101
- type: "INotFoundResponse"
102
- }
103
- ]
104
- };
105
- const removeRoute = {
106
- operationId: `${camelTypeName}Remove`,
107
- summary: `Remove an entry from ${lowerName}.`,
108
- tag: options?.tagName ?? tagsEntityStorage[0].name,
109
- method: "DELETE",
110
- path: `${baseRouteName}/:id`,
111
- handler: async (httpRequestContext, request) => entityStorageRemove(httpRequestContext, componentName, request),
112
- requestType: {
113
- type: "IEntityStorageRemoveRequest",
114
- examples: options?.examples?.remove?.requestExamples ?? [
115
- {
116
- id: `${camelTypeName}RemoveRequestExample`,
117
- request: {
118
- pathParams: {
119
- id: "12345"
120
- }
121
- }
122
- }
123
- ]
124
- },
125
- responseType: [
126
- {
127
- type: "INoContentResponse"
128
- },
129
- {
130
- type: "INotFoundResponse"
131
- }
132
- ]
133
- };
134
- const listRoute = {
135
- operationId: `${camelTypeName}List`,
136
- summary: `Query entries from ${lowerName}.`,
137
- tag: options?.tagName ?? tagsEntityStorage[0].name,
138
- method: "GET",
139
- path: `${baseRouteName}/`,
140
- handler: async (httpRequestContext, request) => entityStorageList(httpRequestContext, componentName, request),
141
- requestType: {
142
- type: "IEntityStorageListRequest",
143
- examples: options?.examples?.list?.requestExamples ?? [
144
- {
145
- id: `${camelTypeName}ListRequestExample`,
146
- request: {}
147
- }
148
- ]
149
- },
150
- responseType: [
151
- {
152
- type: "IEntityStorageListResponse",
153
- examples: options?.examples?.list?.responseExamples ?? [
154
- {
155
- id: `${camelTypeName}ListResponseExample`,
156
- response: {
157
- body: {
158
- entities: [{ id: "12345", name: "My Item" }]
159
- }
160
- }
161
- }
162
- ]
163
- }
164
- ]
165
- };
166
- return [setRoute, getRoute, removeRoute, listRoute];
167
- }
168
- /**
169
- * Set the entry in entity storage.
170
- * @param httpRequestContext The request context for the API.
171
- * @param componentName The name of the component to use in the routes.
172
- * @param request The request.
173
- * @returns The response object with additional http response properties.
174
- */
175
- async function entityStorageSet(httpRequestContext, componentName, request) {
176
- core.Guards.object(ROUTES_SOURCE, "request", request);
177
- const component = core.ComponentFactory.get(componentName);
178
- await component.set(request.body, httpRequestContext.userIdentity);
179
- return {
180
- statusCode: web.HttpStatusCode.noContent
181
- };
182
- }
183
- /**
184
- * Get the entry from entity storage.
185
- * @param httpRequestContext The request context for the API.
186
- * @param componentName The name of the component to use in the routes.
187
- * @param request The request.
188
- * @returns The response object with additional http response properties.
189
- */
190
- async function entityStorageGet(httpRequestContext, componentName, request) {
191
- core.Guards.object(ROUTES_SOURCE, "request", request);
192
- core.Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
193
- core.Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
194
- const component = core.ComponentFactory.get(componentName);
195
- const item = await component.get(request.pathParams.id, request.query?.secondaryIndex, httpRequestContext.userIdentity);
196
- return {
197
- body: item
198
- };
199
- }
200
- /**
201
- * Remove the entry from entity storage.
202
- * @param httpRequestContext The request context for the API.
203
- * @param componentName The name of the component to use in the routes.
204
- * @param request The request.
205
- * @returns The response object with additional http response properties.
206
- */
207
- async function entityStorageRemove(httpRequestContext, componentName, request) {
208
- core.Guards.object(ROUTES_SOURCE, "request", request);
209
- core.Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
210
- core.Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
211
- const component = core.ComponentFactory.get(componentName);
212
- await component.remove(request.pathParams.id, httpRequestContext.userIdentity);
213
- return {
214
- statusCode: web.HttpStatusCode.noContent
215
- };
216
- }
217
- /**
218
- * Query the entries from entity storage.
219
- * @param httpRequestContext The request context for the API.
220
- * @param componentName The name of the component to use in the routes.
221
- * @param request The request.
222
- * @returns The response object with additional http response properties.
223
- */
224
- async function entityStorageList(httpRequestContext, componentName, request) {
225
- core.Guards.object(ROUTES_SOURCE, "request", request);
226
- const component = core.ComponentFactory.get(componentName);
227
- const result = await component.query(apiModels.HttpParameterHelper.objectFromString(request.query?.conditions), request.query?.orderBy, request.query?.orderByDirection, apiModels.HttpParameterHelper.objectFromString(request.query?.properties), request.query?.cursor, core.Coerce.number(request.query?.pageSize), httpRequestContext.userIdentity);
228
- return {
229
- body: result
230
- };
231
- }
232
-
233
- // Copyright 2024 IOTA Stiftung.
234
- // SPDX-License-Identifier: Apache-2.0.
235
- /**
236
- * Class for performing entity service operations.
237
- */
238
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
239
- class EntityStorageService {
240
- /**
241
- * Runtime name for the class.
242
- */
243
- CLASS_NAME = "EntityStorageService";
244
- /**
245
- * The entity storage for items.
246
- * @internal
247
- */
248
- _entityStorage;
249
- /**
250
- * Include the user identity when performing storage operations, this allow separation of data per user, default to false.
251
- * @internal
252
- */
253
- _partitionPerUser;
254
- /**
255
- * Create a new instance of EntityStorageService.
256
- * @param options The dependencies for the entity storage service.
257
- */
258
- constructor(options) {
259
- core.Guards.string(this.CLASS_NAME, "options.entityStorageType", options.entityStorageType);
260
- this._entityStorage = entityStorageModels.EntityStorageConnectorFactory.get(options.entityStorageType);
261
- this._partitionPerUser = options.config?.partitionPerUser ?? false;
262
- }
263
- /**
264
- * Set an entity.
265
- * @param entity The entity to set.
266
- * @param userIdentity The user identity to use with storage operations.
267
- * @returns The id of the entity.
268
- */
269
- async set(entity, userIdentity) {
270
- core.Guards.object(this.CLASS_NAME, "entity", entity);
271
- const conditions = [];
272
- if (this._partitionPerUser) {
273
- core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
274
- conditions.push({
275
- property: "userIdentity",
276
- value: userIdentity
277
- });
278
- core.ObjectHelper.propertySet(entity, "userIdentity", userIdentity);
279
- }
280
- return this._entityStorage.set(entity, conditions);
281
- }
282
- /**
283
- * Get an entity.
284
- * @param id The id of the entity to get, or the index value if secondaryIndex is set.
285
- * @param secondaryIndex Get the item using a secondary index.
286
- * @param userIdentity The user identity to use with storage operations.
287
- * @returns The object if it can be found or undefined.
288
- */
289
- async get(id, secondaryIndex, userIdentity) {
290
- core.Guards.stringValue(this.CLASS_NAME, "id", id);
291
- return this.internalGet(id, secondaryIndex, userIdentity);
292
- }
293
- /**
294
- * Remove the entity.
295
- * @param id The id of the entity to remove.
296
- * @param userIdentity The user identity to use with storage operations.
297
- * @returns Nothing.
298
- */
299
- async remove(id, userIdentity) {
300
- core.Guards.stringValue(this.CLASS_NAME, "id", id);
301
- const conditions = [];
302
- if (this._partitionPerUser) {
303
- core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
304
- conditions.push({
305
- property: "userIdentity",
306
- value: userIdentity
307
- });
308
- }
309
- await this._entityStorage.remove(id, conditions);
310
- }
311
- /**
312
- * Query all the entities which match the conditions.
313
- * @param conditions The conditions to match for the entities.
314
- * @param orderBy The order for the results.
315
- * @param orderByDirection The direction for the order, defaults to ascending.
316
- * @param properties The optional properties to return, defaults to all.
317
- * @param cursor The cursor to request the next page of entities.
318
- * @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
319
- * @param userIdentity The user identity to use with storage operations.
320
- * @returns All the entities for the storage matching the conditions,
321
- * and a cursor which can be used to request more entities.
322
- */
323
- async query(conditions, orderBy, orderByDirection, properties, cursor, pageSize, userIdentity) {
324
- const finalConditions = {
325
- conditions: [],
326
- logicalOperator: entity.LogicalOperator.And
327
- };
328
- if (this._partitionPerUser) {
329
- core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
330
- finalConditions.conditions.push({
331
- property: "userIdentity",
332
- comparison: entity.ComparisonOperator.Equals,
333
- value: userIdentity
334
- });
335
- }
336
- if (!core.Is.empty(conditions)) {
337
- finalConditions.conditions.push(conditions);
338
- }
339
- const result = await this._entityStorage.query(finalConditions.conditions.length > 0 ? finalConditions : undefined, core.Is.stringValue(orderBy)
340
- ? [{ property: orderBy, sortDirection: orderByDirection ?? entity.SortDirection.Ascending }]
341
- : undefined, properties, cursor, pageSize);
342
- for (const entity of result.entities) {
343
- core.ObjectHelper.propertyDelete(entity, "userIdentity");
344
- }
345
- return result;
346
- }
347
- /**
348
- * Get an entity.
349
- * @param id The id of the entity to get, or the index value if secondaryIndex is set.
350
- * @param secondaryIndex Get the item using a secondary index.
351
- * @param userIdentity The user identity to use with storage operations.
352
- * @returns The object if it can be found or throws.
353
- * @internal
354
- */
355
- async internalGet(id, secondaryIndex, userIdentity) {
356
- const conditions = [];
357
- if (this._partitionPerUser) {
358
- core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
359
- conditions.push({
360
- property: "userIdentity",
361
- comparison: entity.ComparisonOperator.Equals,
362
- value: userIdentity
363
- });
364
- }
365
- if (core.Is.stringValue(secondaryIndex)) {
366
- conditions.push({
367
- property: secondaryIndex,
368
- comparison: entity.ComparisonOperator.Equals,
369
- value: id
370
- });
371
- }
372
- let entity$1;
373
- if (conditions.length === 0) {
374
- entity$1 = await this._entityStorage.get(id, secondaryIndex);
375
- }
376
- else {
377
- if (!core.Is.stringValue(secondaryIndex)) {
378
- const schema = this._entityStorage.getSchema();
379
- const primaryKey = entity.EntitySchemaHelper.getPrimaryKey(schema);
380
- conditions.unshift({
381
- property: primaryKey.property,
382
- comparison: entity.ComparisonOperator.Equals,
383
- value: id
384
- });
385
- }
386
- const results = await this._entityStorage.query({
387
- conditions,
388
- logicalOperator: entity.LogicalOperator.And
389
- }, undefined, undefined, undefined, 1);
390
- entity$1 = results.entities[0];
391
- }
392
- if (core.Is.empty(entity$1)) {
393
- throw new core.NotFoundError(this.CLASS_NAME, "entityNotFound", id);
394
- }
395
- core.ObjectHelper.propertyDelete(entity$1, "userIdentity");
396
- return entity$1;
397
- }
398
- }
399
-
400
- /**
401
- * These are dummy entry points for the entity storage service.
402
- * In reality your application would create its own entry points based on the
403
- * entity storage schema objects it wants to store, using a custom defaultBaseRoute.
404
- */
405
- const restEntryPoints = [
406
- {
407
- name: "entity-storage",
408
- defaultBaseRoute: "entity-storage",
409
- tags: tagsEntityStorage,
410
- generateRoutes: generateRestRoutesEntityStorage
411
- }
412
- ];
413
-
414
- exports.EntityStorageService = EntityStorageService;
415
- exports.entityStorageGet = entityStorageGet;
416
- exports.entityStorageList = entityStorageList;
417
- exports.entityStorageRemove = entityStorageRemove;
418
- exports.entityStorageSet = entityStorageSet;
419
- exports.generateRestRoutesEntityStorage = generateRestRoutesEntityStorage;
420
- exports.restEntryPoints = restEntryPoints;
421
- exports.tagsEntityStorage = tagsEntityStorage;