@twin.org/entity-storage-service 0.0.1-next.8 → 0.0.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.
- package/dist/cjs/index.cjs +91 -92
- package/dist/esm/index.mjs +92 -93
- package/dist/types/entityStorageService.d.ts +6 -13
- package/dist/types/index.d.ts +1 -0
- package/dist/types/models/IEntityStorageServiceConstructorOptions.d.ts +14 -0
- package/docs/changelog.md +105 -1
- package/docs/open-api/spec.json +640 -608
- package/docs/reference/classes/EntityStorageService.md +72 -48
- package/docs/reference/functions/entityStorageGet.md +9 -3
- package/docs/reference/functions/entityStorageList.md +9 -3
- package/docs/reference/functions/entityStorageRemove.md +9 -3
- package/docs/reference/functions/entityStorageSet.md +9 -3
- package/docs/reference/functions/generateRestRoutesEntityStorage.md +18 -8
- package/docs/reference/index.md +1 -0
- package/docs/reference/interfaces/IEntityStorageServiceConstructorOptions.md +19 -0
- package/package.json +10 -10
package/dist/cjs/index.cjs
CHANGED
@@ -224,7 +224,7 @@ async function entityStorageRemove(httpRequestContext, componentName, request) {
|
|
224
224
|
async function entityStorageList(httpRequestContext, componentName, request) {
|
225
225
|
core.Guards.object(ROUTES_SOURCE, "request", request);
|
226
226
|
const component = core.ComponentFactory.get(componentName);
|
227
|
-
const result = await component.query(apiModels.HttpParameterHelper.objectFromString(request.query?.conditions),
|
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, httpRequestContext.nodeIdentity);
|
228
228
|
return {
|
229
229
|
body: result
|
230
230
|
};
|
@@ -259,8 +259,6 @@ class EntityStorageService {
|
|
259
259
|
/**
|
260
260
|
* Create a new instance of EntityStorageService.
|
261
261
|
* @param options The dependencies for the entity storage service.
|
262
|
-
* @param options.config The configuration for the service.
|
263
|
-
* @param options.entityStorageType The entity storage type.
|
264
262
|
*/
|
265
263
|
constructor(options) {
|
266
264
|
core.Guards.string(this.CLASS_NAME, "options.entityStorageType", options.entityStorageType);
|
@@ -277,15 +275,24 @@ class EntityStorageService {
|
|
277
275
|
*/
|
278
276
|
async set(entity, userIdentity, nodeIdentity) {
|
279
277
|
core.Guards.object(this.CLASS_NAME, "entity", entity);
|
278
|
+
const conditions = [];
|
280
279
|
if (this._includeUserIdentity) {
|
281
280
|
core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
|
281
|
+
conditions.push({
|
282
|
+
property: "userIdentity",
|
283
|
+
value: userIdentity
|
284
|
+
});
|
282
285
|
core.ObjectHelper.propertySet(entity, "userIdentity", userIdentity);
|
283
286
|
}
|
284
287
|
if (this._includeNodeIdentity) {
|
285
288
|
core.Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
289
|
+
conditions.push({
|
290
|
+
property: "nodeIdentity",
|
291
|
+
value: nodeIdentity
|
292
|
+
});
|
286
293
|
core.ObjectHelper.propertySet(entity, "nodeIdentity", nodeIdentity);
|
287
294
|
}
|
288
|
-
return this._entityStorage.set(entity);
|
295
|
+
return this._entityStorage.set(entity, conditions);
|
289
296
|
}
|
290
297
|
/**
|
291
298
|
* Get an entity.
|
@@ -296,61 +303,91 @@ class EntityStorageService {
|
|
296
303
|
* @returns The object if it can be found or undefined.
|
297
304
|
*/
|
298
305
|
async get(id, secondaryIndex, userIdentity, nodeIdentity) {
|
306
|
+
core.Guards.stringValue(this.CLASS_NAME, "id", id);
|
307
|
+
return this.internalGet(id, secondaryIndex, userIdentity, nodeIdentity);
|
308
|
+
}
|
309
|
+
/**
|
310
|
+
* Remove the entity.
|
311
|
+
* @param id The id of the entity to remove.
|
312
|
+
* @param userIdentity The user identity to use with storage operations.
|
313
|
+
* @param nodeIdentity The node identity to use with storage operations.
|
314
|
+
* @returns Nothing.
|
315
|
+
*/
|
316
|
+
async remove(id, userIdentity, nodeIdentity) {
|
299
317
|
core.Guards.stringValue(this.CLASS_NAME, "id", id);
|
300
318
|
const conditions = [];
|
301
319
|
if (this._includeUserIdentity) {
|
302
320
|
core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
|
303
321
|
conditions.push({
|
304
322
|
property: "userIdentity",
|
305
|
-
comparison: entity.ComparisonOperator.Equals,
|
306
323
|
value: userIdentity
|
307
324
|
});
|
308
325
|
}
|
309
326
|
if (this._includeNodeIdentity) {
|
310
327
|
core.Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
311
328
|
conditions.push({
|
329
|
+
property: "nodeIdentity",
|
330
|
+
value: nodeIdentity
|
331
|
+
});
|
332
|
+
}
|
333
|
+
await this._entityStorage.remove(id, conditions);
|
334
|
+
}
|
335
|
+
/**
|
336
|
+
* Query all the entities which match the conditions.
|
337
|
+
* @param conditions The conditions to match for the entities.
|
338
|
+
* @param orderBy The order for the results.
|
339
|
+
* @param orderByDirection The direction for the order, defaults to ascending.
|
340
|
+
* @param properties The optional properties to return, defaults to all.
|
341
|
+
* @param cursor The cursor to request the next page of entities.
|
342
|
+
* @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
|
343
|
+
* @param userIdentity The user identity to use with storage operations.
|
344
|
+
* @param nodeIdentity The node identity to use with storage operations.
|
345
|
+
* @returns All the entities for the storage matching the conditions,
|
346
|
+
* and a cursor which can be used to request more entities.
|
347
|
+
*/
|
348
|
+
async query(conditions, orderBy, orderByDirection, properties, cursor, pageSize, userIdentity, nodeIdentity) {
|
349
|
+
const finalConditions = {
|
350
|
+
conditions: [],
|
351
|
+
logicalOperator: entity.LogicalOperator.And
|
352
|
+
};
|
353
|
+
if (this._includeNodeIdentity) {
|
354
|
+
core.Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
355
|
+
finalConditions.conditions.push({
|
312
356
|
property: "nodeIdentity",
|
313
357
|
comparison: entity.ComparisonOperator.Equals,
|
314
358
|
value: nodeIdentity
|
315
359
|
});
|
316
360
|
}
|
317
|
-
if (
|
318
|
-
|
319
|
-
|
361
|
+
if (this._includeUserIdentity) {
|
362
|
+
core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
|
363
|
+
finalConditions.conditions.push({
|
364
|
+
property: "userIdentity",
|
320
365
|
comparison: entity.ComparisonOperator.Equals,
|
321
|
-
value:
|
366
|
+
value: userIdentity
|
322
367
|
});
|
323
368
|
}
|
324
|
-
if (conditions
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
}
|
369
|
+
if (!core.Is.empty(conditions)) {
|
370
|
+
finalConditions.conditions.push(conditions);
|
371
|
+
}
|
372
|
+
const result = await this._entityStorage.query(finalConditions.conditions.length > 0 ? finalConditions : undefined, core.Is.stringValue(orderBy)
|
373
|
+
? [{ property: orderBy, sortDirection: orderByDirection ?? entity.SortDirection.Ascending }]
|
374
|
+
: undefined, properties, cursor, pageSize);
|
375
|
+
for (const entity of result.entities) {
|
329
376
|
core.ObjectHelper.propertyDelete(entity, "nodeIdentity");
|
330
377
|
core.ObjectHelper.propertyDelete(entity, "userIdentity");
|
331
|
-
return entity;
|
332
378
|
}
|
333
|
-
|
334
|
-
conditions,
|
335
|
-
logicalOperator: entity.LogicalOperator.And
|
336
|
-
}, undefined, undefined, undefined, 1);
|
337
|
-
const entity$1 = results.entities[0];
|
338
|
-
if (core.Is.empty(entity$1)) {
|
339
|
-
throw new core.NotFoundError(this.CLASS_NAME, "entityNotFound", id);
|
340
|
-
}
|
341
|
-
core.ObjectHelper.propertyDelete(entity$1, "nodeIdentity");
|
342
|
-
core.ObjectHelper.propertyDelete(entity$1, "userIdentity");
|
343
|
-
return entity$1;
|
379
|
+
return result;
|
344
380
|
}
|
345
381
|
/**
|
346
|
-
*
|
347
|
-
* @param id The id of the entity to
|
382
|
+
* Get an entity.
|
383
|
+
* @param id The id of the entity to get, or the index value if secondaryIndex is set.
|
384
|
+
* @param secondaryIndex Get the item using a secondary index.
|
348
385
|
* @param userIdentity The user identity to use with storage operations.
|
349
386
|
* @param nodeIdentity The node identity to use with storage operations.
|
350
|
-
* @returns
|
387
|
+
* @returns The object if it can be found or throws.
|
388
|
+
* @internal
|
351
389
|
*/
|
352
|
-
async
|
353
|
-
core.Guards.stringValue(this.CLASS_NAME, "id", id);
|
390
|
+
async internalGet(id, secondaryIndex, userIdentity, nodeIdentity) {
|
354
391
|
const conditions = [];
|
355
392
|
if (this._includeUserIdentity) {
|
356
393
|
core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
|
@@ -368,77 +405,39 @@ class EntityStorageService {
|
|
368
405
|
value: nodeIdentity
|
369
406
|
});
|
370
407
|
}
|
371
|
-
if (
|
372
|
-
const entity = await this._entityStorage.get(id);
|
373
|
-
if (core.Is.empty(entity)) {
|
374
|
-
throw new core.NotFoundError(this.CLASS_NAME, "entityNotFound", id);
|
375
|
-
}
|
376
|
-
await this._entityStorage.remove(id);
|
377
|
-
}
|
378
|
-
else {
|
379
|
-
const schema = this._entityStorage.getSchema();
|
380
|
-
const primaryKey = entity.EntitySchemaHelper.getPrimaryKey(schema);
|
408
|
+
if (core.Is.stringValue(secondaryIndex)) {
|
381
409
|
conditions.push({
|
382
|
-
property:
|
410
|
+
property: secondaryIndex,
|
383
411
|
comparison: entity.ComparisonOperator.Equals,
|
384
412
|
value: id
|
385
413
|
});
|
414
|
+
}
|
415
|
+
let entity$1;
|
416
|
+
if (conditions.length === 0) {
|
417
|
+
entity$1 = await this._entityStorage.get(id, secondaryIndex);
|
418
|
+
}
|
419
|
+
else {
|
420
|
+
if (!core.Is.stringValue(secondaryIndex)) {
|
421
|
+
const schema = this._entityStorage.getSchema();
|
422
|
+
const primaryKey = entity.EntitySchemaHelper.getPrimaryKey(schema);
|
423
|
+
conditions.unshift({
|
424
|
+
property: primaryKey.property,
|
425
|
+
comparison: entity.ComparisonOperator.Equals,
|
426
|
+
value: id
|
427
|
+
});
|
428
|
+
}
|
386
429
|
const results = await this._entityStorage.query({
|
387
430
|
conditions,
|
388
431
|
logicalOperator: entity.LogicalOperator.And
|
389
432
|
}, undefined, undefined, undefined, 1);
|
390
|
-
|
391
|
-
const firstEntity = results.entities[0];
|
392
|
-
await this._entityStorage.remove(firstEntity[primaryKey.property]);
|
393
|
-
}
|
394
|
-
else {
|
395
|
-
throw new core.NotFoundError(this.CLASS_NAME, "entityNotFound", id);
|
396
|
-
}
|
433
|
+
entity$1 = results.entities[0];
|
397
434
|
}
|
398
|
-
|
399
|
-
|
400
|
-
* Query all the entities which match the conditions.
|
401
|
-
* @param conditions The conditions to match for the entities.
|
402
|
-
* @param sortProperties The optional sort order.
|
403
|
-
* @param properties The optional properties to return, defaults to all.
|
404
|
-
* @param cursor The cursor to request the next page of entities.
|
405
|
-
* @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
|
406
|
-
* @param userIdentity The user identity to use with storage operations.
|
407
|
-
* @param nodeIdentity The node identity to use with storage operations.
|
408
|
-
* @returns All the entities for the storage matching the conditions,
|
409
|
-
* and a cursor which can be used to request more entities.
|
410
|
-
*/
|
411
|
-
async query(conditions, sortProperties, properties, cursor, pageSize, userIdentity, nodeIdentity) {
|
412
|
-
const conditionsList = [];
|
413
|
-
if (this._includeUserIdentity) {
|
414
|
-
core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
|
415
|
-
conditionsList.push({
|
416
|
-
property: "userIdentity",
|
417
|
-
comparison: entity.ComparisonOperator.Equals,
|
418
|
-
value: userIdentity
|
419
|
-
});
|
420
|
-
}
|
421
|
-
if (this._includeNodeIdentity) {
|
422
|
-
core.Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
423
|
-
conditionsList.push({
|
424
|
-
property: "nodeIdentity",
|
425
|
-
comparison: entity.ComparisonOperator.Equals,
|
426
|
-
value: nodeIdentity
|
427
|
-
});
|
428
|
-
}
|
429
|
-
const finalConditions = {
|
430
|
-
conditions: conditionsList,
|
431
|
-
logicalOperator: entity.LogicalOperator.And
|
432
|
-
};
|
433
|
-
if (!core.Is.empty(conditions)) {
|
434
|
-
finalConditions.conditions.push(conditions);
|
435
|
-
}
|
436
|
-
const result = await this._entityStorage.query(finalConditions.conditions.length > 0 ? finalConditions : undefined, sortProperties, properties, cursor, pageSize);
|
437
|
-
for (const entity of result.entities) {
|
438
|
-
core.ObjectHelper.propertyDelete(entity, "nodeIdentity");
|
439
|
-
core.ObjectHelper.propertyDelete(entity, "userIdentity");
|
435
|
+
if (core.Is.empty(entity$1)) {
|
436
|
+
throw new core.NotFoundError(this.CLASS_NAME, "entityNotFound", id);
|
440
437
|
}
|
441
|
-
|
438
|
+
core.ObjectHelper.propertyDelete(entity$1, "nodeIdentity");
|
439
|
+
core.ObjectHelper.propertyDelete(entity$1, "userIdentity");
|
440
|
+
return entity$1;
|
442
441
|
}
|
443
442
|
}
|
444
443
|
|
package/dist/esm/index.mjs
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { HttpParameterHelper } from '@twin.org/api-models';
|
2
2
|
import { StringHelper, Guards, ComponentFactory, Coerce, ObjectHelper, Is, NotFoundError } from '@twin.org/core';
|
3
3
|
import { HttpStatusCode } from '@twin.org/web';
|
4
|
-
import { ComparisonOperator,
|
4
|
+
import { LogicalOperator, ComparisonOperator, SortDirection, EntitySchemaHelper } from '@twin.org/entity';
|
5
5
|
import { EntityStorageConnectorFactory } from '@twin.org/entity-storage-models';
|
6
6
|
|
7
7
|
// Copyright 2024 IOTA Stiftung.
|
@@ -222,7 +222,7 @@ async function entityStorageRemove(httpRequestContext, componentName, request) {
|
|
222
222
|
async function entityStorageList(httpRequestContext, componentName, request) {
|
223
223
|
Guards.object(ROUTES_SOURCE, "request", request);
|
224
224
|
const component = ComponentFactory.get(componentName);
|
225
|
-
const result = await component.query(HttpParameterHelper.objectFromString(request.query?.conditions),
|
225
|
+
const result = await component.query(HttpParameterHelper.objectFromString(request.query?.conditions), request.query?.orderBy, request.query?.orderByDirection, HttpParameterHelper.objectFromString(request.query?.properties), request.query?.cursor, Coerce.number(request.query?.pageSize), httpRequestContext.userIdentity, httpRequestContext.nodeIdentity);
|
226
226
|
return {
|
227
227
|
body: result
|
228
228
|
};
|
@@ -257,8 +257,6 @@ class EntityStorageService {
|
|
257
257
|
/**
|
258
258
|
* Create a new instance of EntityStorageService.
|
259
259
|
* @param options The dependencies for the entity storage service.
|
260
|
-
* @param options.config The configuration for the service.
|
261
|
-
* @param options.entityStorageType The entity storage type.
|
262
260
|
*/
|
263
261
|
constructor(options) {
|
264
262
|
Guards.string(this.CLASS_NAME, "options.entityStorageType", options.entityStorageType);
|
@@ -275,15 +273,24 @@ class EntityStorageService {
|
|
275
273
|
*/
|
276
274
|
async set(entity, userIdentity, nodeIdentity) {
|
277
275
|
Guards.object(this.CLASS_NAME, "entity", entity);
|
276
|
+
const conditions = [];
|
278
277
|
if (this._includeUserIdentity) {
|
279
278
|
Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
|
279
|
+
conditions.push({
|
280
|
+
property: "userIdentity",
|
281
|
+
value: userIdentity
|
282
|
+
});
|
280
283
|
ObjectHelper.propertySet(entity, "userIdentity", userIdentity);
|
281
284
|
}
|
282
285
|
if (this._includeNodeIdentity) {
|
283
286
|
Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
287
|
+
conditions.push({
|
288
|
+
property: "nodeIdentity",
|
289
|
+
value: nodeIdentity
|
290
|
+
});
|
284
291
|
ObjectHelper.propertySet(entity, "nodeIdentity", nodeIdentity);
|
285
292
|
}
|
286
|
-
return this._entityStorage.set(entity);
|
293
|
+
return this._entityStorage.set(entity, conditions);
|
287
294
|
}
|
288
295
|
/**
|
289
296
|
* Get an entity.
|
@@ -294,61 +301,91 @@ class EntityStorageService {
|
|
294
301
|
* @returns The object if it can be found or undefined.
|
295
302
|
*/
|
296
303
|
async get(id, secondaryIndex, userIdentity, nodeIdentity) {
|
304
|
+
Guards.stringValue(this.CLASS_NAME, "id", id);
|
305
|
+
return this.internalGet(id, secondaryIndex, userIdentity, nodeIdentity);
|
306
|
+
}
|
307
|
+
/**
|
308
|
+
* Remove the entity.
|
309
|
+
* @param id The id of the entity to remove.
|
310
|
+
* @param userIdentity The user identity to use with storage operations.
|
311
|
+
* @param nodeIdentity The node identity to use with storage operations.
|
312
|
+
* @returns Nothing.
|
313
|
+
*/
|
314
|
+
async remove(id, userIdentity, nodeIdentity) {
|
297
315
|
Guards.stringValue(this.CLASS_NAME, "id", id);
|
298
316
|
const conditions = [];
|
299
317
|
if (this._includeUserIdentity) {
|
300
318
|
Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
|
301
319
|
conditions.push({
|
302
320
|
property: "userIdentity",
|
303
|
-
comparison: ComparisonOperator.Equals,
|
304
321
|
value: userIdentity
|
305
322
|
});
|
306
323
|
}
|
307
324
|
if (this._includeNodeIdentity) {
|
308
325
|
Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
309
326
|
conditions.push({
|
327
|
+
property: "nodeIdentity",
|
328
|
+
value: nodeIdentity
|
329
|
+
});
|
330
|
+
}
|
331
|
+
await this._entityStorage.remove(id, conditions);
|
332
|
+
}
|
333
|
+
/**
|
334
|
+
* Query all the entities which match the conditions.
|
335
|
+
* @param conditions The conditions to match for the entities.
|
336
|
+
* @param orderBy The order for the results.
|
337
|
+
* @param orderByDirection The direction for the order, defaults to ascending.
|
338
|
+
* @param properties The optional properties to return, defaults to all.
|
339
|
+
* @param cursor The cursor to request the next page of entities.
|
340
|
+
* @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
|
341
|
+
* @param userIdentity The user identity to use with storage operations.
|
342
|
+
* @param nodeIdentity The node identity to use with storage operations.
|
343
|
+
* @returns All the entities for the storage matching the conditions,
|
344
|
+
* and a cursor which can be used to request more entities.
|
345
|
+
*/
|
346
|
+
async query(conditions, orderBy, orderByDirection, properties, cursor, pageSize, userIdentity, nodeIdentity) {
|
347
|
+
const finalConditions = {
|
348
|
+
conditions: [],
|
349
|
+
logicalOperator: LogicalOperator.And
|
350
|
+
};
|
351
|
+
if (this._includeNodeIdentity) {
|
352
|
+
Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
353
|
+
finalConditions.conditions.push({
|
310
354
|
property: "nodeIdentity",
|
311
355
|
comparison: ComparisonOperator.Equals,
|
312
356
|
value: nodeIdentity
|
313
357
|
});
|
314
358
|
}
|
315
|
-
if (
|
316
|
-
|
317
|
-
|
359
|
+
if (this._includeUserIdentity) {
|
360
|
+
Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
|
361
|
+
finalConditions.conditions.push({
|
362
|
+
property: "userIdentity",
|
318
363
|
comparison: ComparisonOperator.Equals,
|
319
|
-
value:
|
364
|
+
value: userIdentity
|
320
365
|
});
|
321
366
|
}
|
322
|
-
if (conditions
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
}
|
367
|
+
if (!Is.empty(conditions)) {
|
368
|
+
finalConditions.conditions.push(conditions);
|
369
|
+
}
|
370
|
+
const result = await this._entityStorage.query(finalConditions.conditions.length > 0 ? finalConditions : undefined, Is.stringValue(orderBy)
|
371
|
+
? [{ property: orderBy, sortDirection: orderByDirection ?? SortDirection.Ascending }]
|
372
|
+
: undefined, properties, cursor, pageSize);
|
373
|
+
for (const entity of result.entities) {
|
327
374
|
ObjectHelper.propertyDelete(entity, "nodeIdentity");
|
328
375
|
ObjectHelper.propertyDelete(entity, "userIdentity");
|
329
|
-
return entity;
|
330
376
|
}
|
331
|
-
|
332
|
-
conditions,
|
333
|
-
logicalOperator: LogicalOperator.And
|
334
|
-
}, undefined, undefined, undefined, 1);
|
335
|
-
const entity = results.entities[0];
|
336
|
-
if (Is.empty(entity)) {
|
337
|
-
throw new NotFoundError(this.CLASS_NAME, "entityNotFound", id);
|
338
|
-
}
|
339
|
-
ObjectHelper.propertyDelete(entity, "nodeIdentity");
|
340
|
-
ObjectHelper.propertyDelete(entity, "userIdentity");
|
341
|
-
return entity;
|
377
|
+
return result;
|
342
378
|
}
|
343
379
|
/**
|
344
|
-
*
|
345
|
-
* @param id The id of the entity to
|
380
|
+
* Get an entity.
|
381
|
+
* @param id The id of the entity to get, or the index value if secondaryIndex is set.
|
382
|
+
* @param secondaryIndex Get the item using a secondary index.
|
346
383
|
* @param userIdentity The user identity to use with storage operations.
|
347
384
|
* @param nodeIdentity The node identity to use with storage operations.
|
348
|
-
* @returns
|
385
|
+
* @returns The object if it can be found or throws.
|
386
|
+
* @internal
|
349
387
|
*/
|
350
|
-
async
|
351
|
-
Guards.stringValue(this.CLASS_NAME, "id", id);
|
388
|
+
async internalGet(id, secondaryIndex, userIdentity, nodeIdentity) {
|
352
389
|
const conditions = [];
|
353
390
|
if (this._includeUserIdentity) {
|
354
391
|
Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
|
@@ -366,77 +403,39 @@ class EntityStorageService {
|
|
366
403
|
value: nodeIdentity
|
367
404
|
});
|
368
405
|
}
|
369
|
-
if (
|
370
|
-
const entity = await this._entityStorage.get(id);
|
371
|
-
if (Is.empty(entity)) {
|
372
|
-
throw new NotFoundError(this.CLASS_NAME, "entityNotFound", id);
|
373
|
-
}
|
374
|
-
await this._entityStorage.remove(id);
|
375
|
-
}
|
376
|
-
else {
|
377
|
-
const schema = this._entityStorage.getSchema();
|
378
|
-
const primaryKey = EntitySchemaHelper.getPrimaryKey(schema);
|
406
|
+
if (Is.stringValue(secondaryIndex)) {
|
379
407
|
conditions.push({
|
380
|
-
property:
|
408
|
+
property: secondaryIndex,
|
381
409
|
comparison: ComparisonOperator.Equals,
|
382
410
|
value: id
|
383
411
|
});
|
412
|
+
}
|
413
|
+
let entity;
|
414
|
+
if (conditions.length === 0) {
|
415
|
+
entity = await this._entityStorage.get(id, secondaryIndex);
|
416
|
+
}
|
417
|
+
else {
|
418
|
+
if (!Is.stringValue(secondaryIndex)) {
|
419
|
+
const schema = this._entityStorage.getSchema();
|
420
|
+
const primaryKey = EntitySchemaHelper.getPrimaryKey(schema);
|
421
|
+
conditions.unshift({
|
422
|
+
property: primaryKey.property,
|
423
|
+
comparison: ComparisonOperator.Equals,
|
424
|
+
value: id
|
425
|
+
});
|
426
|
+
}
|
384
427
|
const results = await this._entityStorage.query({
|
385
428
|
conditions,
|
386
429
|
logicalOperator: LogicalOperator.And
|
387
430
|
}, undefined, undefined, undefined, 1);
|
388
|
-
|
389
|
-
const firstEntity = results.entities[0];
|
390
|
-
await this._entityStorage.remove(firstEntity[primaryKey.property]);
|
391
|
-
}
|
392
|
-
else {
|
393
|
-
throw new NotFoundError(this.CLASS_NAME, "entityNotFound", id);
|
394
|
-
}
|
431
|
+
entity = results.entities[0];
|
395
432
|
}
|
396
|
-
|
397
|
-
|
398
|
-
* Query all the entities which match the conditions.
|
399
|
-
* @param conditions The conditions to match for the entities.
|
400
|
-
* @param sortProperties The optional sort order.
|
401
|
-
* @param properties The optional properties to return, defaults to all.
|
402
|
-
* @param cursor The cursor to request the next page of entities.
|
403
|
-
* @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
|
404
|
-
* @param userIdentity The user identity to use with storage operations.
|
405
|
-
* @param nodeIdentity The node identity to use with storage operations.
|
406
|
-
* @returns All the entities for the storage matching the conditions,
|
407
|
-
* and a cursor which can be used to request more entities.
|
408
|
-
*/
|
409
|
-
async query(conditions, sortProperties, properties, cursor, pageSize, userIdentity, nodeIdentity) {
|
410
|
-
const conditionsList = [];
|
411
|
-
if (this._includeUserIdentity) {
|
412
|
-
Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
|
413
|
-
conditionsList.push({
|
414
|
-
property: "userIdentity",
|
415
|
-
comparison: ComparisonOperator.Equals,
|
416
|
-
value: userIdentity
|
417
|
-
});
|
418
|
-
}
|
419
|
-
if (this._includeNodeIdentity) {
|
420
|
-
Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
421
|
-
conditionsList.push({
|
422
|
-
property: "nodeIdentity",
|
423
|
-
comparison: ComparisonOperator.Equals,
|
424
|
-
value: nodeIdentity
|
425
|
-
});
|
426
|
-
}
|
427
|
-
const finalConditions = {
|
428
|
-
conditions: conditionsList,
|
429
|
-
logicalOperator: LogicalOperator.And
|
430
|
-
};
|
431
|
-
if (!Is.empty(conditions)) {
|
432
|
-
finalConditions.conditions.push(conditions);
|
433
|
-
}
|
434
|
-
const result = await this._entityStorage.query(finalConditions.conditions.length > 0 ? finalConditions : undefined, sortProperties, properties, cursor, pageSize);
|
435
|
-
for (const entity of result.entities) {
|
436
|
-
ObjectHelper.propertyDelete(entity, "nodeIdentity");
|
437
|
-
ObjectHelper.propertyDelete(entity, "userIdentity");
|
433
|
+
if (Is.empty(entity)) {
|
434
|
+
throw new NotFoundError(this.CLASS_NAME, "entityNotFound", id);
|
438
435
|
}
|
439
|
-
|
436
|
+
ObjectHelper.propertyDelete(entity, "nodeIdentity");
|
437
|
+
ObjectHelper.propertyDelete(entity, "userIdentity");
|
438
|
+
return entity;
|
440
439
|
}
|
441
440
|
}
|
442
441
|
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import { type
|
1
|
+
import { type EntityCondition, SortDirection } from "@twin.org/entity";
|
2
2
|
import { type IEntityStorageComponent } from "@twin.org/entity-storage-models";
|
3
|
-
import type {
|
3
|
+
import type { IEntityStorageServiceConstructorOptions } from "./models/IEntityStorageServiceConstructorOptions";
|
4
4
|
/**
|
5
5
|
* Class for performing entity service operations.
|
6
6
|
*/
|
@@ -12,13 +12,8 @@ export declare class EntityStorageService<T = any> implements IEntityStorageComp
|
|
12
12
|
/**
|
13
13
|
* Create a new instance of EntityStorageService.
|
14
14
|
* @param options The dependencies for the entity storage service.
|
15
|
-
* @param options.config The configuration for the service.
|
16
|
-
* @param options.entityStorageType The entity storage type.
|
17
15
|
*/
|
18
|
-
constructor(options:
|
19
|
-
entityStorageType: string;
|
20
|
-
config?: IEntityStorageConfig;
|
21
|
-
});
|
16
|
+
constructor(options: IEntityStorageServiceConstructorOptions);
|
22
17
|
/**
|
23
18
|
* Set an entity.
|
24
19
|
* @param entity The entity to set.
|
@@ -47,7 +42,8 @@ export declare class EntityStorageService<T = any> implements IEntityStorageComp
|
|
47
42
|
/**
|
48
43
|
* Query all the entities which match the conditions.
|
49
44
|
* @param conditions The conditions to match for the entities.
|
50
|
-
* @param
|
45
|
+
* @param orderBy The order for the results.
|
46
|
+
* @param orderByDirection The direction for the order, defaults to ascending.
|
51
47
|
* @param properties The optional properties to return, defaults to all.
|
52
48
|
* @param cursor The cursor to request the next page of entities.
|
53
49
|
* @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
|
@@ -56,10 +52,7 @@ export declare class EntityStorageService<T = any> implements IEntityStorageComp
|
|
56
52
|
* @returns All the entities for the storage matching the conditions,
|
57
53
|
* and a cursor which can be used to request more entities.
|
58
54
|
*/
|
59
|
-
query(conditions?: EntityCondition<T>,
|
60
|
-
property: keyof T;
|
61
|
-
sortDirection: SortDirection;
|
62
|
-
}[], properties?: (keyof T)[], cursor?: string, pageSize?: number, userIdentity?: string, nodeIdentity?: string): Promise<{
|
55
|
+
query(conditions?: EntityCondition<T>, orderBy?: keyof T, orderByDirection?: SortDirection, properties?: (keyof T)[], cursor?: string, pageSize?: number, userIdentity?: string, nodeIdentity?: string): Promise<{
|
63
56
|
/**
|
64
57
|
* The entities, which can be partial if a limited keys list was provided.
|
65
58
|
*/
|
package/dist/types/index.d.ts
CHANGED
@@ -2,4 +2,5 @@ export * from "./entityStorageRoutes";
|
|
2
2
|
export * from "./entityStorageService";
|
3
3
|
export * from "./models/IEntityStorageConfig";
|
4
4
|
export * from "./models/IEntityStorageRoutesExamples";
|
5
|
+
export * from "./models/IEntityStorageServiceConstructorOptions";
|
5
6
|
export * from "./restEntryPoints";
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import type { IEntityStorageConfig } from "./IEntityStorageConfig";
|
2
|
+
/**
|
3
|
+
* Options for the Entity Storage Service constructor.
|
4
|
+
*/
|
5
|
+
export interface IEntityStorageServiceConstructorOptions {
|
6
|
+
/**
|
7
|
+
* The type of the entity storage.
|
8
|
+
*/
|
9
|
+
entityStorageType: string;
|
10
|
+
/**
|
11
|
+
* The configuration for the service.
|
12
|
+
*/
|
13
|
+
config?: IEntityStorageConfig;
|
14
|
+
}
|