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

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # TWIN Entity Storage Service
1
+ # Entity Storage Service
2
2
 
3
- Entity Storage contract implementation and REST endpoint definitions.
3
+ This package defines the service-facing contracts and REST endpoint definitions used to expose storage behaviour consistently. It is designed to work with the wider storage ecosystem so applications can keep behaviour consistent across connectors and environments.
4
4
 
5
5
  ## Installation
6
6
 
@@ -57,6 +57,33 @@ export function generateRestRoutesEntityStorage(baseRouteName, componentName, op
57
57
  }
58
58
  ]
59
59
  };
60
+ const setBatchRoute = {
61
+ operationId: `${camelTypeName}SetBatch`,
62
+ summary: `Set multiple entries in ${lowerName}.`,
63
+ tag: options?.tagName ?? tagsEntityStorage[0].name,
64
+ method: "POST",
65
+ path: `${baseRouteName}/batch`,
66
+ handler: async (httpRequestContext, request) => entityStorageSetBatch(httpRequestContext, componentName, request),
67
+ requestType: {
68
+ type: "IEntityStorageSetBatchRequest",
69
+ examples: [
70
+ {
71
+ id: `${camelTypeName}SetBatchRequestExample`,
72
+ request: {
73
+ body: [
74
+ { id: "12345", name: "My Item" },
75
+ { id: "67890", name: "My Other Item" }
76
+ ]
77
+ }
78
+ }
79
+ ]
80
+ },
81
+ responseType: [
82
+ {
83
+ type: "INoContentResponse"
84
+ }
85
+ ]
86
+ };
60
87
  const getRoute = {
61
88
  operationId: `${camelTypeName}Get`,
62
89
  summary: `Get an entry from ${lowerName}.`,
@@ -158,7 +185,94 @@ export function generateRestRoutesEntityStorage(baseRouteName, componentName, op
158
185
  }
159
186
  ]
160
187
  };
161
- return [setRoute, getRoute, removeRoute, listRoute];
188
+ const countRoute = {
189
+ operationId: `${camelTypeName}Count`,
190
+ summary: `Count entries in ${lowerName}.`,
191
+ tag: options?.tagName ?? tagsEntityStorage[0].name,
192
+ method: "GET",
193
+ path: `${baseRouteName}/count`,
194
+ handler: async (httpRequestContext, request) => entityStorageCount(httpRequestContext, componentName, request),
195
+ requestType: {
196
+ type: "IEntityStorageCountRequest",
197
+ examples: options?.examples?.count?.requestExamples ?? [
198
+ {
199
+ id: `${camelTypeName}CountRequestExample`,
200
+ request: {}
201
+ }
202
+ ]
203
+ },
204
+ responseType: [
205
+ {
206
+ type: "IEntityStorageCountResponse",
207
+ examples: options?.examples?.count?.responseExamples ?? [
208
+ {
209
+ id: `${camelTypeName}CountResponseExample`,
210
+ response: {
211
+ body: {
212
+ count: 1
213
+ }
214
+ }
215
+ }
216
+ ]
217
+ }
218
+ ]
219
+ };
220
+ const emptyRoute = {
221
+ operationId: `${camelTypeName}Empty`,
222
+ summary: `Remove all entries from ${lowerName}.`,
223
+ tag: options?.tagName ?? tagsEntityStorage[0].name,
224
+ method: "DELETE",
225
+ path: `${baseRouteName}/`,
226
+ handler: async (httpRequestContext, request) => entityStorageEmpty(httpRequestContext, componentName, request),
227
+ requestType: {
228
+ type: "IEntityStorageEmptyRequest",
229
+ examples: [
230
+ {
231
+ id: `${camelTypeName}EmptyRequestExample`,
232
+ request: {}
233
+ }
234
+ ]
235
+ },
236
+ responseType: [
237
+ {
238
+ type: "INoContentResponse"
239
+ }
240
+ ]
241
+ };
242
+ const removeBatchRoute = {
243
+ operationId: `${camelTypeName}RemoveBatch`,
244
+ summary: `Remove multiple entries from ${lowerName} by id.`,
245
+ tag: options?.tagName ?? tagsEntityStorage[0].name,
246
+ method: "DELETE",
247
+ path: `${baseRouteName}/batch`,
248
+ handler: async (httpRequestContext, request) => entityStorageRemoveBatch(httpRequestContext, componentName, request),
249
+ requestType: {
250
+ type: "IEntityStorageRemoveBatchRequest",
251
+ examples: [
252
+ {
253
+ id: `${camelTypeName}RemoveBatchRequestExample`,
254
+ request: {
255
+ body: ["12345", "67890"]
256
+ }
257
+ }
258
+ ]
259
+ },
260
+ responseType: [
261
+ {
262
+ type: "INoContentResponse"
263
+ }
264
+ ]
265
+ };
266
+ return [
267
+ setRoute,
268
+ setBatchRoute,
269
+ getRoute,
270
+ removeRoute,
271
+ removeBatchRoute,
272
+ emptyRoute,
273
+ listRoute,
274
+ countRoute
275
+ ];
162
276
  }
163
277
  /**
164
278
  * Set the entry in entity storage.
@@ -175,6 +289,34 @@ export async function entityStorageSet(httpRequestContext, componentName, reques
175
289
  statusCode: HttpStatusCode.noContent
176
290
  };
177
291
  }
292
+ /**
293
+ * Set multiple entries in entity storage.
294
+ * @param httpRequestContext The request context for the API.
295
+ * @param componentName The name of the component to use in the routes.
296
+ * @param request The request.
297
+ * @returns The response object with additional http response properties.
298
+ */
299
+ export async function entityStorageSetBatch(httpRequestContext, componentName, request) {
300
+ Guards.object(ROUTES_SOURCE, "request", request);
301
+ Guards.arrayValue(ROUTES_SOURCE, "request.body", request.body);
302
+ const component = ComponentFactory.get(componentName);
303
+ await component.setBatch(request.body);
304
+ return {
305
+ statusCode: HttpStatusCode.noContent
306
+ };
307
+ }
308
+ /**
309
+ * Remove all entries from entity storage.
310
+ * @param httpRequestContext The request context for the API.
311
+ * @param componentName The name of the component to use in the routes.
312
+ * @param request The request.
313
+ * @returns The response object with additional http response properties.
314
+ */
315
+ export async function entityStorageEmpty(httpRequestContext, componentName, request) {
316
+ const component = ComponentFactory.get(componentName);
317
+ await component.empty();
318
+ return { statusCode: HttpStatusCode.noContent };
319
+ }
178
320
  /**
179
321
  * Get the entry from entity storage.
180
322
  * @param httpRequestContext The request context for the API.
@@ -224,4 +366,34 @@ export async function entityStorageList(httpRequestContext, componentName, reque
224
366
  body: result
225
367
  };
226
368
  }
369
+ /**
370
+ * Count the entries in entity storage.
371
+ * @param httpRequestContext The request context for the API.
372
+ * @param componentName The name of the component to use in the routes.
373
+ * @param request The request.
374
+ * @returns The response object with additional http response properties.
375
+ */
376
+ export async function entityStorageCount(httpRequestContext, componentName, request) {
377
+ const component = ComponentFactory.get(componentName);
378
+ const count = await component.count();
379
+ return {
380
+ body: { count }
381
+ };
382
+ }
383
+ /**
384
+ * Remove multiple entries from entity storage by id.
385
+ * @param httpRequestContext The request context for the API.
386
+ * @param componentName The name of the component to use in the routes.
387
+ * @param request The request.
388
+ * @returns The response object with additional http response properties.
389
+ */
390
+ export async function entityStorageRemoveBatch(httpRequestContext, componentName, request) {
391
+ Guards.object(ROUTES_SOURCE, "request", request);
392
+ Guards.arrayValue(ROUTES_SOURCE, "request.body", request.body);
393
+ const component = ComponentFactory.get(componentName);
394
+ await component.removeBatch(request.body);
395
+ return {
396
+ statusCode: HttpStatusCode.noContent
397
+ };
398
+ }
227
399
  //# sourceMappingURL=entityStorageRoutes.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"entityStorageRoutes.js","sourceRoot":"","sources":["../../src/entityStorageRoutes.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EACN,mBAAmB,EAMnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAWhF,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAG/C;;GAEG;AACH,MAAM,aAAa,GAAG,qBAAqB,CAAC;AAE5C;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAW;IACxC;QACC,IAAI,EAAE,eAAe;QACrB,WAAW,EAAE,oEAAoE;KACjF;CACD,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,UAAU,+BAA+B,CAC9C,aAAqB,EACrB,aAAqB,EACrB,OAIC;IAED,MAAM,QAAQ,GAAG,OAAO,EAAE,QAAQ,IAAI,gBAAgB,CAAC;IACvD,MAAM,SAAS,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;IACzC,MAAM,aAAa,GAAG,YAAY,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;IAEvD,MAAM,QAAQ,GAA6D;QAC1E,WAAW,EAAE,GAAG,aAAa,KAAK;QAClC,OAAO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B,aAAa,CAAC,CAAC;IAC/E,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,KAAK,CACnC,mBAAmB,CAAC,gBAAgB,CAAC,OAAO,CAAC,KAAK,EAAE,UAAU,CAAC,EAC/D,OAAO,CAAC,KAAK,EAAE,OAAwB,EACvC,OAAO,CAAC,KAAK,EAAE,gBAAgB,EAC/B,mBAAmB,CAAC,gBAAgB,CAAC,OAAO,CAAC,KAAK,EAAE,UAAU,CAAC,EAC/D,OAAO,CAAC,KAAK,EAAE,MAAM,EACrB,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CACnC,CAAC;IACF,OAAO;QACN,IAAI,EAAE,MAAM;KACZ,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tHttpParameterHelper,\n\ttype INotFoundResponse,\n\ttype IHttpRequestContext,\n\ttype INoContentResponse,\n\ttype IRestRoute,\n\ttype ITag\n} from \"@twin.org/api-models\";\nimport { Coerce, ComponentFactory, Guards, StringHelper } from \"@twin.org/core\";\nimport type {\n\tIEntityStorageComponent,\n\tIEntityStorageGetRequest,\n\tIEntityStorageGetResponse,\n\tIEntityStorageListRequest,\n\tIEntityStorageListResponse,\n\tIEntityStorageRemoveRequest,\n\tIEntityStorageSetRequest\n} from \"@twin.org/entity-storage-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { HttpStatusCode } from \"@twin.org/web\";\nimport type { IEntityStorageRoutesExamples } from \"./models/IEntityStorageRoutesExamples.js\";\n\n/**\n * The source used when communicating about these routes.\n */\nconst ROUTES_SOURCE = \"entityStorageRoutes\";\n\n/**\n * The tag to associate with the routes.\n */\nexport const tagsEntityStorage: ITag[] = [\n\t{\n\t\tname: \"EntityStorage\",\n\t\tdescription: \"Endpoints which are modelled to access an entity storage contract.\"\n\t}\n];\n\n/**\n * The REST routes for entity storage.\n * @param baseRouteName Prefix to prepend to the paths.\n * @param componentName The name of the component to use in the routes stored in the ComponentFactory.\n * @param options Additional options for the routes.\n * @param options.typeName Optional type name to use in the routes, defaults to Entity Storage.\n * @param options.tagName Optional name to use in OpenAPI spec for tag.\n * @param options.examples Optional examples to use in the routes.\n * @returns The generated routes.\n */\nexport function generateRestRoutesEntityStorage(\n\tbaseRouteName: string,\n\tcomponentName: string,\n\toptions?: {\n\t\ttypeName?: string;\n\t\ttagName?: string;\n\t\texamples?: IEntityStorageRoutesExamples;\n\t}\n): IRestRoute[] {\n\tconst typeName = options?.typeName ?? \"Entity Storage\";\n\tconst lowerName = typeName.toLowerCase();\n\tconst camelTypeName = StringHelper.camelCase(typeName);\n\n\tconst setRoute: IRestRoute<IEntityStorageSetRequest, INoContentResponse> = {\n\t\toperationId: `${camelTypeName}Set`,\n\t\tsummary: `Set an entry in ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"POST\",\n\t\tpath: `${baseRouteName}/`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageSet(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageSetRequest>(),\n\t\t\texamples: options?.examples?.set?.requestExamples ?? [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}SetRequestExample`,\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tid: \"12345\",\n\t\t\t\t\t\t\tname: \"My Item\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst getRoute: IRestRoute<IEntityStorageGetRequest, IEntityStorageGetResponse> = {\n\t\toperationId: `${camelTypeName}Get`,\n\t\tsummary: `Get an entry from ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageGet(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageGetRequest>(),\n\t\t\texamples: options?.examples?.get?.requestExamples ?? [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}GetRequestExample`,\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"12345\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<IEntityStorageGetResponse>(),\n\t\t\t\texamples: options?.examples?.get?.responseExamples ?? [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: `${camelTypeName}GetResponseExample`,\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tid: \"12345\",\n\t\t\t\t\t\t\t\tname: \"My Item\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t},\n\t\t\t{\n\t\t\t\ttype: nameof<INotFoundResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst removeRoute: IRestRoute<IEntityStorageRemoveRequest, INoContentResponse> = {\n\t\toperationId: `${camelTypeName}Remove`,\n\t\tsummary: `Remove an entry from ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"DELETE\",\n\t\tpath: `${baseRouteName}/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageRemove(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageRemoveRequest>(),\n\t\t\texamples: options?.examples?.remove?.requestExamples ?? [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}RemoveRequestExample`,\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"12345\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t},\n\t\t\t{\n\t\t\t\ttype: nameof<INotFoundResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst listRoute: IRestRoute<IEntityStorageListRequest, IEntityStorageListResponse> = {\n\t\toperationId: `${camelTypeName}List`,\n\t\tsummary: `Query entries from ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageList(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageListRequest>(),\n\t\t\texamples: options?.examples?.list?.requestExamples ?? [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}ListRequestExample`,\n\t\t\t\t\trequest: {}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<IEntityStorageListResponse>(),\n\t\t\t\texamples: options?.examples?.list?.responseExamples ?? [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: `${camelTypeName}ListResponseExample`,\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tentities: [{ id: \"12345\", name: \"My Item\" }]\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\treturn [setRoute, getRoute, removeRoute, listRoute];\n}\n\n/**\n * Set the entry in entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageSet(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageSetRequest\n): Promise<INoContentResponse> {\n\tGuards.object<IEntityStorageSetRequest>(ROUTES_SOURCE, nameof(request), request);\n\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tawait component.set(request.body);\n\treturn {\n\t\tstatusCode: HttpStatusCode.noContent\n\t};\n}\n\n/**\n * Get the entry from entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageGet(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageGetRequest\n): Promise<IEntityStorageGetResponse> {\n\tGuards.object<IEntityStorageGetRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<IEntityStorageGetRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tconst item = await component.get(\n\t\trequest.pathParams.id,\n\t\trequest.query?.secondaryIndex as keyof unknown\n\t);\n\treturn {\n\t\tbody: item\n\t};\n}\n\n/**\n * Remove the entry from entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageRemove(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageRemoveRequest\n): Promise<INoContentResponse> {\n\tGuards.object<IEntityStorageRemoveRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<IEntityStorageRemoveRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tawait component.remove(request.pathParams.id);\n\treturn {\n\t\tstatusCode: HttpStatusCode.noContent\n\t};\n}\n\n/**\n * Query the entries from entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageList(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageListRequest\n): Promise<IEntityStorageListResponse> {\n\tGuards.object<IEntityStorageListRequest>(ROUTES_SOURCE, nameof(request), request);\n\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tconst result = await component.query(\n\t\tHttpParameterHelper.objectFromString(request.query?.conditions),\n\t\trequest.query?.orderBy as keyof unknown,\n\t\trequest.query?.orderByDirection,\n\t\tHttpParameterHelper.objectFromString(request.query?.properties),\n\t\trequest.query?.cursor,\n\t\tCoerce.number(request.query?.limit)\n\t);\n\treturn {\n\t\tbody: result\n\t};\n}\n"]}
1
+ {"version":3,"file":"entityStorageRoutes.js","sourceRoot":"","sources":["../../src/entityStorageRoutes.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EACN,mBAAmB,EAMnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAgBhF,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAG/C;;GAEG;AACH,MAAM,aAAa,GAAG,qBAAqB,CAAC;AAE5C;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAW;IACxC;QACC,IAAI,EAAE,eAAe;QACrB,WAAW,EAAE,oEAAoE;KACjF;CACD,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,UAAU,+BAA+B,CAC9C,aAAqB,EACrB,aAAqB,EACrB,OAIC;IAED,MAAM,QAAQ,GAAG,OAAO,EAAE,QAAQ,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;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tHttpParameterHelper,\n\ttype INotFoundResponse,\n\ttype IHttpRequestContext,\n\ttype INoContentResponse,\n\ttype IRestRoute,\n\ttype ITag\n} from \"@twin.org/api-models\";\nimport { Coerce, ComponentFactory, Guards, StringHelper } from \"@twin.org/core\";\nimport type {\n\tIEntityStorageComponent,\n\tIEntityStorageCountRequest,\n\tIEntityStorageCountResponse,\n\tIEntityStorageEmptyRequest,\n\tIEntityStorageGetRequest,\n\tIEntityStorageGetResponse,\n\tIEntityStorageListRequest,\n\tIEntityStorageListResponse,\n\tIEntityStorageRemoveBatchRequest,\n\tIEntityStorageRemoveRequest,\n\tIEntityStorageSetBatchRequest,\n\tIEntityStorageSetRequest\n} from \"@twin.org/entity-storage-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { HttpStatusCode } from \"@twin.org/web\";\nimport type { IEntityStorageRoutesExamples } from \"./models/IEntityStorageRoutesExamples.js\";\n\n/**\n * The source used when communicating about these routes.\n */\nconst ROUTES_SOURCE = \"entityStorageRoutes\";\n\n/**\n * The tag to associate with the routes.\n */\nexport const tagsEntityStorage: ITag[] = [\n\t{\n\t\tname: \"EntityStorage\",\n\t\tdescription: \"Endpoints which are modelled to access an entity storage contract.\"\n\t}\n];\n\n/**\n * The REST routes for entity storage.\n * @param baseRouteName Prefix to prepend to the paths.\n * @param componentName The name of the component to use in the routes stored in the ComponentFactory.\n * @param options Additional options for the routes.\n * @param options.typeName Optional type name to use in the routes, defaults to Entity Storage.\n * @param options.tagName Optional name to use in OpenAPI spec for tag.\n * @param options.examples Optional examples to use in the routes.\n * @returns The generated routes.\n */\nexport function generateRestRoutesEntityStorage(\n\tbaseRouteName: string,\n\tcomponentName: string,\n\toptions?: {\n\t\ttypeName?: string;\n\t\ttagName?: string;\n\t\texamples?: IEntityStorageRoutesExamples;\n\t}\n): IRestRoute[] {\n\tconst typeName = options?.typeName ?? \"Entity Storage\";\n\tconst lowerName = typeName.toLowerCase();\n\tconst camelTypeName = StringHelper.camelCase(typeName);\n\n\tconst setRoute: IRestRoute<IEntityStorageSetRequest, INoContentResponse> = {\n\t\toperationId: `${camelTypeName}Set`,\n\t\tsummary: `Set an entry in ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"POST\",\n\t\tpath: `${baseRouteName}/`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageSet(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageSetRequest>(),\n\t\t\texamples: options?.examples?.set?.requestExamples ?? [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}SetRequestExample`,\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\tid: \"12345\",\n\t\t\t\t\t\t\tname: \"My Item\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst setBatchRoute: IRestRoute<IEntityStorageSetBatchRequest, INoContentResponse> = {\n\t\toperationId: `${camelTypeName}SetBatch`,\n\t\tsummary: `Set multiple entries in ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"POST\",\n\t\tpath: `${baseRouteName}/batch`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageSetBatch(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageSetBatchRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}SetBatchRequestExample`,\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tbody: [\n\t\t\t\t\t\t\t{ id: \"12345\", name: \"My Item\" },\n\t\t\t\t\t\t\t{ id: \"67890\", name: \"My Other Item\" }\n\t\t\t\t\t\t]\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst getRoute: IRestRoute<IEntityStorageGetRequest, IEntityStorageGetResponse> = {\n\t\toperationId: `${camelTypeName}Get`,\n\t\tsummary: `Get an entry from ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageGet(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageGetRequest>(),\n\t\t\texamples: options?.examples?.get?.requestExamples ?? [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}GetRequestExample`,\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"12345\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<IEntityStorageGetResponse>(),\n\t\t\t\texamples: options?.examples?.get?.responseExamples ?? [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: `${camelTypeName}GetResponseExample`,\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tid: \"12345\",\n\t\t\t\t\t\t\t\tname: \"My Item\"\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t},\n\t\t\t{\n\t\t\t\ttype: nameof<INotFoundResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst removeRoute: IRestRoute<IEntityStorageRemoveRequest, INoContentResponse> = {\n\t\toperationId: `${camelTypeName}Remove`,\n\t\tsummary: `Remove an entry from ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"DELETE\",\n\t\tpath: `${baseRouteName}/:id`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageRemove(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageRemoveRequest>(),\n\t\t\texamples: options?.examples?.remove?.requestExamples ?? [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}RemoveRequestExample`,\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tpathParams: {\n\t\t\t\t\t\t\tid: \"12345\"\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t},\n\t\t\t{\n\t\t\t\ttype: nameof<INotFoundResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst listRoute: IRestRoute<IEntityStorageListRequest, IEntityStorageListResponse> = {\n\t\toperationId: `${camelTypeName}List`,\n\t\tsummary: `Query entries from ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageList(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageListRequest>(),\n\t\t\texamples: options?.examples?.list?.requestExamples ?? [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}ListRequestExample`,\n\t\t\t\t\trequest: {}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<IEntityStorageListResponse>(),\n\t\t\t\texamples: options?.examples?.list?.responseExamples ?? [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: `${camelTypeName}ListResponseExample`,\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tentities: [{ id: \"12345\", name: \"My Item\" }]\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst countRoute: IRestRoute<IEntityStorageCountRequest, IEntityStorageCountResponse> = {\n\t\toperationId: `${camelTypeName}Count`,\n\t\tsummary: `Count entries in ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/count`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageCount(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageCountRequest>(),\n\t\t\texamples: options?.examples?.count?.requestExamples ?? [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}CountRequestExample`,\n\t\t\t\t\trequest: {}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<IEntityStorageCountResponse>(),\n\t\t\t\texamples: options?.examples?.count?.responseExamples ?? [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: `${camelTypeName}CountResponseExample`,\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tcount: 1\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t]\n\t};\n\n\tconst emptyRoute: IRestRoute<IEntityStorageEmptyRequest, INoContentResponse> = {\n\t\toperationId: `${camelTypeName}Empty`,\n\t\tsummary: `Remove all entries from ${lowerName}.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"DELETE\",\n\t\tpath: `${baseRouteName}/`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageEmpty(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageEmptyRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}EmptyRequestExample`,\n\t\t\t\t\trequest: {}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\tconst removeBatchRoute: IRestRoute<IEntityStorageRemoveBatchRequest, INoContentResponse> = {\n\t\toperationId: `${camelTypeName}RemoveBatch`,\n\t\tsummary: `Remove multiple entries from ${lowerName} by id.`,\n\t\ttag: options?.tagName ?? tagsEntityStorage[0].name,\n\t\tmethod: \"DELETE\",\n\t\tpath: `${baseRouteName}/batch`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tentityStorageRemoveBatch(httpRequestContext, componentName, request),\n\t\trequestType: {\n\t\t\ttype: nameof<IEntityStorageRemoveBatchRequest>(),\n\t\t\texamples: [\n\t\t\t\t{\n\t\t\t\t\tid: `${camelTypeName}RemoveBatchRequestExample`,\n\t\t\t\t\trequest: {\n\t\t\t\t\t\tbody: [\"12345\", \"67890\"]\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t]\n\t\t},\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: nameof<INoContentResponse>()\n\t\t\t}\n\t\t]\n\t};\n\n\treturn [\n\t\tsetRoute,\n\t\tsetBatchRoute,\n\t\tgetRoute,\n\t\tremoveRoute,\n\t\tremoveBatchRoute,\n\t\temptyRoute,\n\t\tlistRoute,\n\t\tcountRoute\n\t];\n}\n\n/**\n * Set the entry in entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageSet(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageSetRequest\n): Promise<INoContentResponse> {\n\tGuards.object<IEntityStorageSetRequest>(ROUTES_SOURCE, nameof(request), request);\n\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tawait component.set(request.body);\n\treturn {\n\t\tstatusCode: HttpStatusCode.noContent\n\t};\n}\n\n/**\n * Set multiple entries in entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageSetBatch(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageSetBatchRequest\n): Promise<INoContentResponse> {\n\tGuards.object<IEntityStorageSetBatchRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.arrayValue(ROUTES_SOURCE, nameof(request.body), request.body);\n\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tawait component.setBatch(request.body);\n\treturn {\n\t\tstatusCode: HttpStatusCode.noContent\n\t};\n}\n\n/**\n * Remove all entries from entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageEmpty(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageEmptyRequest\n): Promise<INoContentResponse> {\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tawait component.empty();\n\treturn { statusCode: HttpStatusCode.noContent };\n}\n\n/**\n * Get the entry from entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageGet(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageGetRequest\n): Promise<IEntityStorageGetResponse> {\n\tGuards.object<IEntityStorageGetRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<IEntityStorageGetRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tconst item = await component.get(\n\t\trequest.pathParams.id,\n\t\trequest.query?.secondaryIndex as keyof unknown\n\t);\n\treturn {\n\t\tbody: item\n\t};\n}\n\n/**\n * Remove the entry from entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageRemove(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageRemoveRequest\n): Promise<INoContentResponse> {\n\tGuards.object<IEntityStorageRemoveRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.object<IEntityStorageRemoveRequest[\"pathParams\"]>(\n\t\tROUTES_SOURCE,\n\t\tnameof(request.pathParams),\n\t\trequest.pathParams\n\t);\n\tGuards.stringValue(ROUTES_SOURCE, nameof(request.pathParams.id), request.pathParams.id);\n\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tawait component.remove(request.pathParams.id);\n\treturn {\n\t\tstatusCode: HttpStatusCode.noContent\n\t};\n}\n\n/**\n * Query the entries from entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageList(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageListRequest\n): Promise<IEntityStorageListResponse> {\n\tGuards.object<IEntityStorageListRequest>(ROUTES_SOURCE, nameof(request), request);\n\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tconst result = await component.query(\n\t\tHttpParameterHelper.objectFromString(request.query?.conditions),\n\t\trequest.query?.orderBy as keyof unknown,\n\t\trequest.query?.orderByDirection,\n\t\tHttpParameterHelper.objectFromString(request.query?.properties),\n\t\trequest.query?.cursor,\n\t\tCoerce.number(request.query?.limit)\n\t);\n\treturn {\n\t\tbody: result\n\t};\n}\n\n/**\n * Count the entries in entity storage.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageCount(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageCountRequest\n): Promise<IEntityStorageCountResponse> {\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tconst count = await component.count();\n\treturn {\n\t\tbody: { count }\n\t};\n}\n\n/**\n * Remove multiple entries from entity storage by id.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function entityStorageRemoveBatch(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: IEntityStorageRemoveBatchRequest\n): Promise<INoContentResponse> {\n\tGuards.object<IEntityStorageRemoveBatchRequest>(ROUTES_SOURCE, nameof(request), request);\n\tGuards.arrayValue(ROUTES_SOURCE, nameof(request.body), request.body);\n\n\tconst component = ComponentFactory.get<IEntityStorageComponent>(componentName);\n\tawait component.removeBatch(request.body);\n\treturn {\n\t\tstatusCode: HttpStatusCode.noContent\n\t};\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  // Copyright 2024 IOTA Stiftung.
2
2
  // SPDX-License-Identifier: Apache-2.0.
3
- import { Guards, Is, NotFoundError } from "@twin.org/core";
4
- import { ComparisonOperator, EntitySchemaHelper, LogicalOperator, SortDirection } from "@twin.org/entity";
3
+ import { Guards, Is } from "@twin.org/core";
4
+ import { SortDirection } from "@twin.org/entity";
5
5
  import { EntityStorageConnectorFactory } from "@twin.org/entity-storage-models";
6
6
  /**
7
7
  * Class for performing entity service operations.
@@ -41,6 +41,15 @@ export class EntityStorageService {
41
41
  Guards.object(EntityStorageService.CLASS_NAME, "entity", entity);
42
42
  return this._entityStorage.set(entity, undefined);
43
43
  }
44
+ /**
45
+ * Set multiple entities in a batch.
46
+ * @param entities The entities to set.
47
+ * @returns Nothing.
48
+ */
49
+ async setBatch(entities) {
50
+ Guards.arrayValue(EntityStorageService.CLASS_NAME, "entities", entities);
51
+ return this._entityStorage.setBatch(entities);
52
+ }
44
53
  /**
45
54
  * Get an entity.
46
55
  * @param id The id of the entity to get, or the index value if secondaryIndex is set.
@@ -49,7 +58,7 @@ export class EntityStorageService {
49
58
  */
50
59
  async get(id, secondaryIndex) {
51
60
  Guards.stringValue(EntityStorageService.CLASS_NAME, "id", id);
52
- return this.internalGet(id, secondaryIndex);
61
+ return this._entityStorage.get(id, secondaryIndex);
53
62
  }
54
63
  /**
55
64
  * Remove the entity.
@@ -60,6 +69,29 @@ export class EntityStorageService {
60
69
  Guards.stringValue(EntityStorageService.CLASS_NAME, "id", id);
61
70
  await this._entityStorage.remove(id);
62
71
  }
72
+ /**
73
+ * Remove multiple entities by id.
74
+ * @param ids The ids of the entities to remove.
75
+ * @returns Nothing.
76
+ */
77
+ async removeBatch(ids) {
78
+ Guards.arrayValue(EntityStorageService.CLASS_NAME, "ids", ids);
79
+ return this._entityStorage.removeBatch(ids);
80
+ }
81
+ /**
82
+ * Remove all entities from the storage.
83
+ * @returns Nothing.
84
+ */
85
+ async empty() {
86
+ return this._entityStorage.empty();
87
+ }
88
+ /**
89
+ * Count all the entities which match the conditions.
90
+ * @returns The total count of entities in the storage.
91
+ */
92
+ async count() {
93
+ return this._entityStorage.count();
94
+ }
63
95
  /**
64
96
  * Query all the entities which match the conditions.
65
97
  * @param conditions The conditions to match for the entities.
@@ -77,46 +109,5 @@ export class EntityStorageService {
77
109
  : undefined, properties, cursor, limit);
78
110
  return result;
79
111
  }
80
- /**
81
- * Get an entity.
82
- * @param id The id of the entity to get, or the index value if secondaryIndex is set.
83
- * @param secondaryIndex Get the item using a secondary index.
84
- * @returns The object if it can be found or throws.
85
- * @internal
86
- */
87
- async internalGet(id, secondaryIndex) {
88
- const conditions = [];
89
- if (Is.stringValue(secondaryIndex)) {
90
- conditions.push({
91
- property: secondaryIndex,
92
- comparison: ComparisonOperator.Equals,
93
- value: id
94
- });
95
- }
96
- let entity;
97
- if (conditions.length === 0) {
98
- entity = await this._entityStorage.get(id, secondaryIndex);
99
- }
100
- else {
101
- if (!Is.stringValue(secondaryIndex)) {
102
- const schema = this._entityStorage.getSchema();
103
- const primaryKey = EntitySchemaHelper.getPrimaryKey(schema);
104
- conditions.unshift({
105
- property: primaryKey.property,
106
- comparison: ComparisonOperator.Equals,
107
- value: id
108
- });
109
- }
110
- const results = await this._entityStorage.query({
111
- conditions,
112
- logicalOperator: LogicalOperator.And
113
- }, undefined, undefined, undefined, 1);
114
- entity = results.entities[0];
115
- }
116
- if (Is.empty(entity)) {
117
- throw new NotFoundError(EntityStorageService.CLASS_NAME, "entityNotFound", id);
118
- }
119
- return entity;
120
- }
121
112
  }
122
113
  //# sourceMappingURL=entityStorageService.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"entityStorageService.js","sourceRoot":"","sources":["../../src/entityStorageService.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EACN,kBAAkB,EAElB,kBAAkB,EAClB,eAAe,EACf,aAAa,EACb,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACN,6BAA6B,EAG7B,MAAM,iCAAiC,CAAC;AAIzC;;GAEG;AACH,8DAA8D;AAC9D,MAAM,OAAO,oBAAoB;IAChC;;OAEG;IACI,MAAM,CAAU,UAAU,0BAA0C;IAE3E;;;OAGG;IACc,cAAc,CAA6B;IAE5D;;;OAGG;IACH,YAAY,OAAgD;QAC3D,MAAM,CAAC,MAAM,CACZ,oBAAoB,CAAC,UAAU,+BAE/B,OAAO,CAAC,iBAAiB,CACzB,CAAC;QACF,IAAI,CAAC,cAAc,GAAG,6BAA6B,CAAC,GAAG,CACtD,OAAO,CAAC,iBAAiB,CACzB,CAAC;IACH,CAAC;IAED;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AM,CAAC;IACf,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Guards, Is, NotFoundError } from \"@twin.org/core\";\nimport {\n\tComparisonOperator,\n\ttype EntityCondition,\n\tEntitySchemaHelper,\n\tLogicalOperator,\n\tSortDirection\n} from \"@twin.org/entity\";\nimport {\n\tEntityStorageConnectorFactory,\n\ttype IEntityStorageComponent,\n\ttype IEntityStorageConnector\n} from \"@twin.org/entity-storage-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IEntityStorageServiceConstructorOptions } from \"./models/IEntityStorageServiceConstructorOptions.js\";\n\n/**\n * Class for performing entity service operations.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport class EntityStorageService<T = any> implements IEntityStorageComponent<T> {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<EntityStorageService>();\n\n\t/**\n\t * The entity storage for items.\n\t * @internal\n\t */\n\tprivate readonly _entityStorage: IEntityStorageConnector<T>;\n\n\t/**\n\t * Create a new instance of EntityStorageService.\n\t * @param options The dependencies for the entity storage service.\n\t */\n\tconstructor(options: IEntityStorageServiceConstructorOptions) {\n\t\tGuards.string(\n\t\t\tEntityStorageService.CLASS_NAME,\n\t\t\tnameof(options.entityStorageType),\n\t\t\toptions.entityStorageType\n\t\t);\n\t\tthis._entityStorage = EntityStorageConnectorFactory.get<IEntityStorageConnector<T>>(\n\t\t\toptions.entityStorageType\n\t\t);\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn EntityStorageService.CLASS_NAME;\n\t}\n\n\t/**\n\t * Set an entity.\n\t * @param entity The entity to set.\n\t * @returns The id of the entity.\n\t */\n\tpublic async set(entity: T): Promise<void> {\n\t\tGuards.object(EntityStorageService.CLASS_NAME, nameof(entity), entity);\n\n\t\treturn this._entityStorage.set(entity, undefined);\n\t}\n\n\t/**\n\t * Get an entity.\n\t * @param id The id of the entity to get, or the index value if secondaryIndex is set.\n\t * @param secondaryIndex Get the item using a secondary index.\n\t * @returns The object if it can be found or undefined.\n\t */\n\tpublic async get(id: string, secondaryIndex?: keyof T): Promise<T | undefined> {\n\t\tGuards.stringValue(EntityStorageService.CLASS_NAME, nameof(id), id);\n\n\t\treturn this.internalGet(id, secondaryIndex);\n\t}\n\n\t/**\n\t * Remove the entity.\n\t * @param id The id of the entity to remove.\n\t * @returns Nothing.\n\t */\n\tpublic async remove(id: string): Promise<void> {\n\t\tGuards.stringValue(EntityStorageService.CLASS_NAME, nameof(id), id);\n\n\t\tawait this._entityStorage.remove(id);\n\t}\n\n\t/**\n\t * Query all the entities which match the conditions.\n\t * @param conditions The conditions to match for the entities.\n\t * @param orderBy The order for the results.\n\t * @param orderByDirection The direction for the order, defaults to ascending.\n\t * @param properties The optional properties to return, defaults to all.\n\t * @param cursor The cursor to request the next chunk of entities.\n\t * @param limit The suggested number of entities to return in each chunk, in some scenarios can return a different amount.\n\t * @returns All the entities for the storage matching the conditions,\n\t * and a cursor which can be used to request more entities.\n\t */\n\tpublic async query(\n\t\tconditions?: EntityCondition<T>,\n\t\torderBy?: keyof T,\n\t\torderByDirection?: SortDirection,\n\t\tproperties?: (keyof T)[],\n\t\tcursor?: string,\n\t\tlimit?: number\n\t): Promise<{\n\t\t/**\n\t\t * The entities, which can be partial if a limited keys list was provided.\n\t\t */\n\t\tentities: Partial<T>[];\n\t\t/**\n\t\t * An optional cursor, when defined can be used to call find to get more entities.\n\t\t */\n\t\tcursor?: string;\n\t}> {\n\t\tconst result = await this._entityStorage.query(\n\t\t\tconditions,\n\t\t\tIs.stringValue(orderBy)\n\t\t\t\t? [{ property: orderBy, sortDirection: orderByDirection ?? SortDirection.Ascending }]\n\t\t\t\t: undefined,\n\t\t\tproperties,\n\t\t\tcursor,\n\t\t\tlimit\n\t\t);\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Get an entity.\n\t * @param id The id of the entity to get, or the index value if secondaryIndex is set.\n\t * @param secondaryIndex Get the item using a secondary index.\n\t * @returns The object if it can be found or throws.\n\t * @internal\n\t */\n\tprivate async internalGet(id: string, secondaryIndex?: keyof T): Promise<T> {\n\t\tconst conditions: EntityCondition<T>[] = [];\n\n\t\tif (Is.stringValue(secondaryIndex)) {\n\t\t\tconditions.push({\n\t\t\t\tproperty: secondaryIndex,\n\t\t\t\tcomparison: ComparisonOperator.Equals,\n\t\t\t\tvalue: id\n\t\t\t});\n\t\t}\n\n\t\tlet entity: T | undefined;\n\t\tif (conditions.length === 0) {\n\t\t\tentity = await this._entityStorage.get(id, secondaryIndex);\n\t\t} else {\n\t\t\tif (!Is.stringValue(secondaryIndex)) {\n\t\t\t\tconst schema = this._entityStorage.getSchema();\n\t\t\t\tconst primaryKey = EntitySchemaHelper.getPrimaryKey(schema);\n\n\t\t\t\tconditions.unshift({\n\t\t\t\t\tproperty: primaryKey.property,\n\t\t\t\t\tcomparison: ComparisonOperator.Equals,\n\t\t\t\t\tvalue: id\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst results = await this._entityStorage.query(\n\t\t\t\t{\n\t\t\t\t\tconditions,\n\t\t\t\t\tlogicalOperator: LogicalOperator.And\n\t\t\t\t},\n\t\t\t\tundefined,\n\t\t\t\tundefined,\n\t\t\t\tundefined,\n\t\t\t\t1\n\t\t\t);\n\n\t\t\tentity = results.entities[0] as T;\n\t\t}\n\n\t\tif (Is.empty(entity)) {\n\t\t\tthrow new NotFoundError(EntityStorageService.CLASS_NAME, \"entityNotFound\", id);\n\t\t}\n\n\t\treturn entity;\n\t}\n}\n"]}
1
+ {"version":3,"file":"entityStorageService.js","sourceRoot":"","sources":["../../src/entityStorageService.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAwB,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACvE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UAAU,QAAc,EAAE,CAAC,CAAC;QAEpE,MAAM,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,WAAW,CAAC,GAAa;QACrC,MAAM,CAAC,UAAU,CAAC,oBAAoB,CAAC,UAAU,SAAe,GAAG,CAAC,CAAC;QAErE,OAAO,IAAI,CAAC,cAAc,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IAC7C,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,KAAK;QACjB,OAAO,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC;IACpC,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,KAAK;QACjB,OAAO,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC;IACpC,CAAC;IAED;;;;;;;;;;OAUG;IACI,KAAK,CAAC,KAAK,CACjB,UAA+B,EAC/B,OAAiB,EACjB,gBAAgC,EAChC,UAAwB,EACxB,MAAe,EACf,KAAc;QAWd,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,KAAK,CAC7C,UAAU,EACV,EAAE,CAAC,WAAW,CAAC,OAAO,CAAC;YACtB,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,aAAa,EAAE,gBAAgB,IAAI,aAAa,CAAC,SAAS,EAAE,CAAC;YACrF,CAAC,CAAC,SAAS,EACZ,UAAU,EACV,MAAM,EACN,KAAK,CACL,CAAC;QAEF,OAAO,MAAM,CAAC;IACf,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Guards, Is } from \"@twin.org/core\";\nimport { type EntityCondition, SortDirection } from \"@twin.org/entity\";\nimport {\n\tEntityStorageConnectorFactory,\n\ttype IEntityStorageComponent,\n\ttype IEntityStorageConnector\n} from \"@twin.org/entity-storage-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IEntityStorageServiceConstructorOptions } from \"./models/IEntityStorageServiceConstructorOptions.js\";\n\n/**\n * Class for performing entity service operations.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport class EntityStorageService<T = any> implements IEntityStorageComponent<T> {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<EntityStorageService>();\n\n\t/**\n\t * The entity storage for items.\n\t * @internal\n\t */\n\tprivate readonly _entityStorage: IEntityStorageConnector<T>;\n\n\t/**\n\t * Create a new instance of EntityStorageService.\n\t * @param options The dependencies for the entity storage service.\n\t */\n\tconstructor(options: IEntityStorageServiceConstructorOptions) {\n\t\tGuards.string(\n\t\t\tEntityStorageService.CLASS_NAME,\n\t\t\tnameof(options.entityStorageType),\n\t\t\toptions.entityStorageType\n\t\t);\n\t\tthis._entityStorage = EntityStorageConnectorFactory.get<IEntityStorageConnector<T>>(\n\t\t\toptions.entityStorageType\n\t\t);\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn EntityStorageService.CLASS_NAME;\n\t}\n\n\t/**\n\t * Set an entity.\n\t * @param entity The entity to set.\n\t * @returns The id of the entity.\n\t */\n\tpublic async set(entity: T): Promise<void> {\n\t\tGuards.object(EntityStorageService.CLASS_NAME, nameof(entity), entity);\n\n\t\treturn this._entityStorage.set(entity, undefined);\n\t}\n\n\t/**\n\t * Set multiple entities in a batch.\n\t * @param entities The entities to set.\n\t * @returns Nothing.\n\t */\n\tpublic async setBatch(entities: T[]): Promise<void> {\n\t\tGuards.arrayValue(EntityStorageService.CLASS_NAME, nameof(entities), entities);\n\n\t\treturn this._entityStorage.setBatch(entities);\n\t}\n\n\t/**\n\t * Get an entity.\n\t * @param id The id of the entity to get, or the index value if secondaryIndex is set.\n\t * @param secondaryIndex Get the item using a secondary index.\n\t * @returns The object if it can be found or undefined.\n\t */\n\tpublic async get(id: string, secondaryIndex?: keyof T): Promise<T | undefined> {\n\t\tGuards.stringValue(EntityStorageService.CLASS_NAME, nameof(id), id);\n\n\t\treturn this._entityStorage.get(id, secondaryIndex);\n\t}\n\n\t/**\n\t * Remove the entity.\n\t * @param id The id of the entity to remove.\n\t * @returns Nothing.\n\t */\n\tpublic async remove(id: string): Promise<void> {\n\t\tGuards.stringValue(EntityStorageService.CLASS_NAME, nameof(id), id);\n\n\t\tawait this._entityStorage.remove(id);\n\t}\n\n\t/**\n\t * Remove multiple entities by id.\n\t * @param ids The ids of the entities to remove.\n\t * @returns Nothing.\n\t */\n\tpublic async removeBatch(ids: string[]): Promise<void> {\n\t\tGuards.arrayValue(EntityStorageService.CLASS_NAME, nameof(ids), ids);\n\n\t\treturn this._entityStorage.removeBatch(ids);\n\t}\n\n\t/**\n\t * Remove all entities from the storage.\n\t * @returns Nothing.\n\t */\n\tpublic async empty(): Promise<void> {\n\t\treturn this._entityStorage.empty();\n\t}\n\n\t/**\n\t * Count all the entities which match the conditions.\n\t * @returns The total count of entities in the storage.\n\t */\n\tpublic async count(): Promise<number> {\n\t\treturn this._entityStorage.count();\n\t}\n\n\t/**\n\t * Query all the entities which match the conditions.\n\t * @param conditions The conditions to match for the entities.\n\t * @param orderBy The order for the results.\n\t * @param orderByDirection The direction for the order, defaults to ascending.\n\t * @param properties The optional properties to return, defaults to all.\n\t * @param cursor The cursor to request the next chunk of entities.\n\t * @param limit The suggested number of entities to return in each chunk, in some scenarios can return a different amount.\n\t * @returns All the entities for the storage matching the conditions,\n\t * and a cursor which can be used to request more entities.\n\t */\n\tpublic async query(\n\t\tconditions?: EntityCondition<T>,\n\t\torderBy?: keyof T,\n\t\torderByDirection?: SortDirection,\n\t\tproperties?: (keyof T)[],\n\t\tcursor?: string,\n\t\tlimit?: number\n\t): Promise<{\n\t\t/**\n\t\t * The entities, which can be partial if a limited keys list was provided.\n\t\t */\n\t\tentities: Partial<T>[];\n\t\t/**\n\t\t * An optional cursor, when defined can be used to call find to get more entities.\n\t\t */\n\t\tcursor?: string;\n\t}> {\n\t\tconst result = await this._entityStorage.query(\n\t\t\tconditions,\n\t\t\tIs.stringValue(orderBy)\n\t\t\t\t? [{ property: orderBy, sortDirection: orderByDirection ?? SortDirection.Ascending }]\n\t\t\t\t: undefined,\n\t\t\tproperties,\n\t\t\tcursor,\n\t\t\tlimit\n\t\t);\n\n\t\treturn result;\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"IEntityStorageRoutesExamples.js","sourceRoot":"","sources":["../../../src/models/IEntityStorageRoutesExamples.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IRestRouteRequestExample, IRestRouteResponseExample } from \"@twin.org/api-models\";\nimport type {\n\tIEntityStorageGetRequest,\n\tIEntityStorageGetResponse,\n\tIEntityStorageListRequest,\n\tIEntityStorageListResponse,\n\tIEntityStorageRemoveRequest,\n\tIEntityStorageSetRequest\n} from \"@twin.org/entity-storage-models\";\n\n/**\n * Examples for the entity storage routes.\n */\nexport interface IEntityStorageRoutesExamples {\n\t/**\n\t * Examples for the set route.\n\t */\n\tset?: {\n\t\trequestExamples: IRestRouteRequestExample<IEntityStorageSetRequest>[];\n\t};\n\n\t/**\n\t * Examples for the get route.\n\t */\n\tget?: {\n\t\trequestExamples: IRestRouteRequestExample<IEntityStorageGetRequest>[];\n\t\tresponseExamples: IRestRouteResponseExample<IEntityStorageGetResponse>[];\n\t};\n\n\t/**\n\t * Examples for the remove route.\n\t */\n\tremove?: {\n\t\trequestExamples: IRestRouteRequestExample<IEntityStorageRemoveRequest>[];\n\t};\n\n\t/**\n\t * Examples for the list route.\n\t */\n\tlist?: {\n\t\trequestExamples: IRestRouteRequestExample<IEntityStorageListRequest>[];\n\t\tresponseExamples: IRestRouteResponseExample<IEntityStorageListResponse>[];\n\t};\n}\n"]}
1
+ {"version":3,"file":"IEntityStorageRoutesExamples.js","sourceRoot":"","sources":["../../../src/models/IEntityStorageRoutesExamples.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IRestRouteRequestExample, IRestRouteResponseExample } from \"@twin.org/api-models\";\nimport type {\n\tIEntityStorageCountRequest,\n\tIEntityStorageCountResponse,\n\tIEntityStorageGetRequest,\n\tIEntityStorageGetResponse,\n\tIEntityStorageListRequest,\n\tIEntityStorageListResponse,\n\tIEntityStorageRemoveRequest,\n\tIEntityStorageSetRequest\n} from \"@twin.org/entity-storage-models\";\n\n/**\n * Examples for the entity storage routes.\n */\nexport interface IEntityStorageRoutesExamples {\n\t/**\n\t * Examples for the set route.\n\t */\n\tset?: {\n\t\trequestExamples: IRestRouteRequestExample<IEntityStorageSetRequest>[];\n\t};\n\n\t/**\n\t * Examples for the get route.\n\t */\n\tget?: {\n\t\trequestExamples: IRestRouteRequestExample<IEntityStorageGetRequest>[];\n\t\tresponseExamples: IRestRouteResponseExample<IEntityStorageGetResponse>[];\n\t};\n\n\t/**\n\t * Examples for the remove route.\n\t */\n\tremove?: {\n\t\trequestExamples: IRestRouteRequestExample<IEntityStorageRemoveRequest>[];\n\t};\n\n\t/**\n\t * Examples for the list route.\n\t */\n\tlist?: {\n\t\trequestExamples: IRestRouteRequestExample<IEntityStorageListRequest>[];\n\t\tresponseExamples: IRestRouteResponseExample<IEntityStorageListResponse>[];\n\t};\n\n\t/**\n\t * Examples for the count route.\n\t */\n\tcount?: {\n\t\trequestExamples: IRestRouteRequestExample<IEntityStorageCountRequest>[];\n\t\tresponseExamples: IRestRouteResponseExample<IEntityStorageCountResponse>[];\n\t};\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import { type IHttpRequestContext, type INoContentResponse, type IRestRoute, type ITag } from "@twin.org/api-models";
2
- import type { IEntityStorageGetRequest, IEntityStorageGetResponse, IEntityStorageListRequest, IEntityStorageListResponse, IEntityStorageRemoveRequest, IEntityStorageSetRequest } from "@twin.org/entity-storage-models";
2
+ import type { IEntityStorageCountRequest, IEntityStorageCountResponse, IEntityStorageEmptyRequest, IEntityStorageGetRequest, IEntityStorageGetResponse, IEntityStorageListRequest, IEntityStorageListResponse, IEntityStorageRemoveBatchRequest, IEntityStorageRemoveRequest, IEntityStorageSetBatchRequest, IEntityStorageSetRequest } from "@twin.org/entity-storage-models";
3
3
  import type { IEntityStorageRoutesExamples } from "./models/IEntityStorageRoutesExamples.js";
4
4
  /**
5
5
  * The tag to associate with the routes.
@@ -28,6 +28,22 @@ export declare function generateRestRoutesEntityStorage(baseRouteName: string, c
28
28
  * @returns The response object with additional http response properties.
29
29
  */
30
30
  export declare function entityStorageSet(httpRequestContext: IHttpRequestContext, componentName: string, request: IEntityStorageSetRequest): Promise<INoContentResponse>;
31
+ /**
32
+ * Set multiple entries in entity storage.
33
+ * @param httpRequestContext The request context for the API.
34
+ * @param componentName The name of the component to use in the routes.
35
+ * @param request The request.
36
+ * @returns The response object with additional http response properties.
37
+ */
38
+ export declare function entityStorageSetBatch(httpRequestContext: IHttpRequestContext, componentName: string, request: IEntityStorageSetBatchRequest): Promise<INoContentResponse>;
39
+ /**
40
+ * Remove all entries from entity storage.
41
+ * @param httpRequestContext The request context for the API.
42
+ * @param componentName The name of the component to use in the routes.
43
+ * @param request The request.
44
+ * @returns The response object with additional http response properties.
45
+ */
46
+ export declare function entityStorageEmpty(httpRequestContext: IHttpRequestContext, componentName: string, request: IEntityStorageEmptyRequest): Promise<INoContentResponse>;
31
47
  /**
32
48
  * Get the entry from entity storage.
33
49
  * @param httpRequestContext The request context for the API.
@@ -52,3 +68,19 @@ export declare function entityStorageRemove(httpRequestContext: IHttpRequestCont
52
68
  * @returns The response object with additional http response properties.
53
69
  */
54
70
  export declare function entityStorageList(httpRequestContext: IHttpRequestContext, componentName: string, request: IEntityStorageListRequest): Promise<IEntityStorageListResponse>;
71
+ /**
72
+ * Count the entries in entity storage.
73
+ * @param httpRequestContext The request context for the API.
74
+ * @param componentName The name of the component to use in the routes.
75
+ * @param request The request.
76
+ * @returns The response object with additional http response properties.
77
+ */
78
+ export declare function entityStorageCount(httpRequestContext: IHttpRequestContext, componentName: string, request: IEntityStorageCountRequest): Promise<IEntityStorageCountResponse>;
79
+ /**
80
+ * Remove multiple entries from entity storage by id.
81
+ * @param httpRequestContext The request context for the API.
82
+ * @param componentName The name of the component to use in the routes.
83
+ * @param request The request.
84
+ * @returns The response object with additional http response properties.
85
+ */
86
+ export declare function entityStorageRemoveBatch(httpRequestContext: IHttpRequestContext, componentName: string, request: IEntityStorageRemoveBatchRequest): Promise<INoContentResponse>;
@@ -25,6 +25,12 @@ export declare class EntityStorageService<T = any> implements IEntityStorageComp
25
25
  * @returns The id of the entity.
26
26
  */
27
27
  set(entity: T): Promise<void>;
28
+ /**
29
+ * Set multiple entities in a batch.
30
+ * @param entities The entities to set.
31
+ * @returns Nothing.
32
+ */
33
+ setBatch(entities: T[]): Promise<void>;
28
34
  /**
29
35
  * Get an entity.
30
36
  * @param id The id of the entity to get, or the index value if secondaryIndex is set.
@@ -38,6 +44,22 @@ export declare class EntityStorageService<T = any> implements IEntityStorageComp
38
44
  * @returns Nothing.
39
45
  */
40
46
  remove(id: string): Promise<void>;
47
+ /**
48
+ * Remove multiple entities by id.
49
+ * @param ids The ids of the entities to remove.
50
+ * @returns Nothing.
51
+ */
52
+ removeBatch(ids: string[]): Promise<void>;
53
+ /**
54
+ * Remove all entities from the storage.
55
+ * @returns Nothing.
56
+ */
57
+ empty(): Promise<void>;
58
+ /**
59
+ * Count all the entities which match the conditions.
60
+ * @returns The total count of entities in the storage.
61
+ */
62
+ count(): Promise<number>;
41
63
  /**
42
64
  * Query all the entities which match the conditions.
43
65
  * @param conditions The conditions to match for the entities.
@@ -1,5 +1,5 @@
1
1
  import type { IRestRouteRequestExample, IRestRouteResponseExample } from "@twin.org/api-models";
2
- import type { IEntityStorageGetRequest, IEntityStorageGetResponse, IEntityStorageListRequest, IEntityStorageListResponse, IEntityStorageRemoveRequest, IEntityStorageSetRequest } from "@twin.org/entity-storage-models";
2
+ import type { IEntityStorageCountRequest, IEntityStorageCountResponse, IEntityStorageGetRequest, IEntityStorageGetResponse, IEntityStorageListRequest, IEntityStorageListResponse, IEntityStorageRemoveRequest, IEntityStorageSetRequest } from "@twin.org/entity-storage-models";
3
3
  /**
4
4
  * Examples for the entity storage routes.
5
5
  */
@@ -30,4 +30,11 @@ export interface IEntityStorageRoutesExamples {
30
30
  requestExamples: IRestRouteRequestExample<IEntityStorageListRequest>[];
31
31
  responseExamples: IRestRouteResponseExample<IEntityStorageListResponse>[];
32
32
  };
33
+ /**
34
+ * Examples for the count route.
35
+ */
36
+ count?: {
37
+ requestExamples: IRestRouteRequestExample<IEntityStorageCountRequest>[];
38
+ responseExamples: IRestRouteResponseExample<IEntityStorageCountResponse>[];
39
+ };
33
40
  }