@twin.org/entity-storage-service 0.0.1-next.8 → 0.0.1-next.9

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.
@@ -256,6 +256,11 @@ class EntityStorageService {
256
256
  * @internal
257
257
  */
258
258
  _includeUserIdentity;
259
+ /**
260
+ * The primary key for the entity.
261
+ * @internal
262
+ */
263
+ _primaryKey;
259
264
  /**
260
265
  * Create a new instance of EntityStorageService.
261
266
  * @param options The dependencies for the entity storage service.
@@ -267,6 +272,7 @@ class EntityStorageService {
267
272
  this._entityStorage = entityStorageModels.EntityStorageConnectorFactory.get(options.entityStorageType);
268
273
  this._includeNodeIdentity = options.config?.includeNodeIdentity ?? true;
269
274
  this._includeUserIdentity = options.config?.includeUserIdentity ?? true;
275
+ this._primaryKey = entity.EntitySchemaHelper.getPrimaryKey(this._entityStorage.getSchema());
270
276
  }
271
277
  /**
272
278
  * Set an entity.
@@ -277,15 +283,24 @@ class EntityStorageService {
277
283
  */
278
284
  async set(entity, userIdentity, nodeIdentity) {
279
285
  core.Guards.object(this.CLASS_NAME, "entity", entity);
286
+ const conditions = [];
280
287
  if (this._includeUserIdentity) {
281
288
  core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
289
+ conditions.push({
290
+ property: "userIdentity",
291
+ value: userIdentity
292
+ });
282
293
  core.ObjectHelper.propertySet(entity, "userIdentity", userIdentity);
283
294
  }
284
295
  if (this._includeNodeIdentity) {
285
296
  core.Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
297
+ conditions.push({
298
+ property: "nodeIdentity",
299
+ value: nodeIdentity
300
+ });
286
301
  core.ObjectHelper.propertySet(entity, "nodeIdentity", nodeIdentity);
287
302
  }
288
- return this._entityStorage.set(entity);
303
+ return this._entityStorage.set(entity, conditions);
289
304
  }
290
305
  /**
291
306
  * Get an entity.
@@ -297,50 +312,7 @@ class EntityStorageService {
297
312
  */
298
313
  async get(id, secondaryIndex, userIdentity, nodeIdentity) {
299
314
  core.Guards.stringValue(this.CLASS_NAME, "id", id);
300
- const conditions = [];
301
- if (this._includeUserIdentity) {
302
- core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
303
- conditions.push({
304
- property: "userIdentity",
305
- comparison: entity.ComparisonOperator.Equals,
306
- value: userIdentity
307
- });
308
- }
309
- if (this._includeNodeIdentity) {
310
- core.Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
311
- conditions.push({
312
- property: "nodeIdentity",
313
- comparison: entity.ComparisonOperator.Equals,
314
- value: nodeIdentity
315
- });
316
- }
317
- if (core.Is.stringValue(secondaryIndex)) {
318
- conditions.push({
319
- property: secondaryIndex,
320
- comparison: entity.ComparisonOperator.Equals,
321
- value: id
322
- });
323
- }
324
- if (conditions.length === 0) {
325
- const entity = await this._entityStorage.get(id, secondaryIndex);
326
- if (core.Is.empty(entity)) {
327
- throw new core.NotFoundError(this.CLASS_NAME, "entityNotFound", id);
328
- }
329
- core.ObjectHelper.propertyDelete(entity, "nodeIdentity");
330
- core.ObjectHelper.propertyDelete(entity, "userIdentity");
331
- return entity;
332
- }
333
- const results = await this._entityStorage.query({
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;
315
+ return this.internalGet(id, secondaryIndex, userIdentity, nodeIdentity);
344
316
  }
345
317
  /**
346
318
  * Remove the entity.
@@ -356,7 +328,6 @@ class EntityStorageService {
356
328
  core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
357
329
  conditions.push({
358
330
  property: "userIdentity",
359
- comparison: entity.ComparisonOperator.Equals,
360
331
  value: userIdentity
361
332
  });
362
333
  }
@@ -364,37 +335,10 @@ class EntityStorageService {
364
335
  core.Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
365
336
  conditions.push({
366
337
  property: "nodeIdentity",
367
- comparison: entity.ComparisonOperator.Equals,
368
338
  value: nodeIdentity
369
339
  });
370
340
  }
371
- if (conditions.length === 0) {
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);
381
- conditions.push({
382
- property: primaryKey.property,
383
- comparison: entity.ComparisonOperator.Equals,
384
- value: id
385
- });
386
- const results = await this._entityStorage.query({
387
- conditions,
388
- logicalOperator: entity.LogicalOperator.And
389
- }, undefined, undefined, undefined, 1);
390
- if (results.entities.length > 0) {
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
- }
397
- }
341
+ await this._entityStorage.remove(id, conditions);
398
342
  }
399
343
  /**
400
344
  * Query all the entities which match the conditions.
@@ -409,27 +353,26 @@ class EntityStorageService {
409
353
  * and a cursor which can be used to request more entities.
410
354
  */
411
355
  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
- }
356
+ const finalConditions = {
357
+ conditions: [],
358
+ logicalOperator: entity.LogicalOperator.And
359
+ };
421
360
  if (this._includeNodeIdentity) {
422
361
  core.Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
423
- conditionsList.push({
362
+ finalConditions.conditions.push({
424
363
  property: "nodeIdentity",
425
364
  comparison: entity.ComparisonOperator.Equals,
426
365
  value: nodeIdentity
427
366
  });
428
367
  }
429
- const finalConditions = {
430
- conditions: conditionsList,
431
- logicalOperator: entity.LogicalOperator.And
432
- };
368
+ if (this._includeUserIdentity) {
369
+ core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
370
+ finalConditions.conditions.push({
371
+ property: "userIdentity",
372
+ comparison: entity.ComparisonOperator.Equals,
373
+ value: userIdentity
374
+ });
375
+ }
433
376
  if (!core.Is.empty(conditions)) {
434
377
  finalConditions.conditions.push(conditions);
435
378
  }
@@ -440,6 +383,67 @@ class EntityStorageService {
440
383
  }
441
384
  return result;
442
385
  }
386
+ /**
387
+ * Get an entity.
388
+ * @param id The id of the entity to get, or the index value if secondaryIndex is set.
389
+ * @param secondaryIndex Get the item using a secondary index.
390
+ * @param userIdentity The user identity to use with storage operations.
391
+ * @param nodeIdentity The node identity to use with storage operations.
392
+ * @returns The object if it can be found or throws.
393
+ * @internal
394
+ */
395
+ async internalGet(id, secondaryIndex, userIdentity, nodeIdentity) {
396
+ const conditions = [];
397
+ if (this._includeUserIdentity) {
398
+ core.Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
399
+ conditions.push({
400
+ property: "userIdentity",
401
+ comparison: entity.ComparisonOperator.Equals,
402
+ value: userIdentity
403
+ });
404
+ }
405
+ if (this._includeNodeIdentity) {
406
+ core.Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
407
+ conditions.push({
408
+ property: "nodeIdentity",
409
+ comparison: entity.ComparisonOperator.Equals,
410
+ value: nodeIdentity
411
+ });
412
+ }
413
+ if (core.Is.stringValue(secondaryIndex)) {
414
+ conditions.push({
415
+ property: secondaryIndex,
416
+ comparison: entity.ComparisonOperator.Equals,
417
+ value: id
418
+ });
419
+ }
420
+ let entity$1;
421
+ if (conditions.length === 0) {
422
+ entity$1 = await this._entityStorage.get(id, secondaryIndex);
423
+ }
424
+ else {
425
+ if (!core.Is.stringValue(secondaryIndex)) {
426
+ const schema = this._entityStorage.getSchema();
427
+ const primaryKey = entity.EntitySchemaHelper.getPrimaryKey(schema);
428
+ conditions.unshift({
429
+ property: primaryKey.property,
430
+ comparison: entity.ComparisonOperator.Equals,
431
+ value: id
432
+ });
433
+ }
434
+ const results = await this._entityStorage.query({
435
+ conditions,
436
+ logicalOperator: entity.LogicalOperator.And
437
+ }, undefined, undefined, undefined, 1);
438
+ entity$1 = results.entities[0];
439
+ }
440
+ if (core.Is.empty(entity$1)) {
441
+ throw new core.NotFoundError(this.CLASS_NAME, "entityNotFound", id);
442
+ }
443
+ core.ObjectHelper.propertyDelete(entity$1, "nodeIdentity");
444
+ core.ObjectHelper.propertyDelete(entity$1, "userIdentity");
445
+ return entity$1;
446
+ }
443
447
  }
444
448
 
445
449
  /**
@@ -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, LogicalOperator, EntitySchemaHelper } from '@twin.org/entity';
4
+ import { EntitySchemaHelper, LogicalOperator, ComparisonOperator } from '@twin.org/entity';
5
5
  import { EntityStorageConnectorFactory } from '@twin.org/entity-storage-models';
6
6
 
7
7
  // Copyright 2024 IOTA Stiftung.
@@ -254,6 +254,11 @@ class EntityStorageService {
254
254
  * @internal
255
255
  */
256
256
  _includeUserIdentity;
257
+ /**
258
+ * The primary key for the entity.
259
+ * @internal
260
+ */
261
+ _primaryKey;
257
262
  /**
258
263
  * Create a new instance of EntityStorageService.
259
264
  * @param options The dependencies for the entity storage service.
@@ -265,6 +270,7 @@ class EntityStorageService {
265
270
  this._entityStorage = EntityStorageConnectorFactory.get(options.entityStorageType);
266
271
  this._includeNodeIdentity = options.config?.includeNodeIdentity ?? true;
267
272
  this._includeUserIdentity = options.config?.includeUserIdentity ?? true;
273
+ this._primaryKey = EntitySchemaHelper.getPrimaryKey(this._entityStorage.getSchema());
268
274
  }
269
275
  /**
270
276
  * Set an entity.
@@ -275,15 +281,24 @@ class EntityStorageService {
275
281
  */
276
282
  async set(entity, userIdentity, nodeIdentity) {
277
283
  Guards.object(this.CLASS_NAME, "entity", entity);
284
+ const conditions = [];
278
285
  if (this._includeUserIdentity) {
279
286
  Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
287
+ conditions.push({
288
+ property: "userIdentity",
289
+ value: userIdentity
290
+ });
280
291
  ObjectHelper.propertySet(entity, "userIdentity", userIdentity);
281
292
  }
282
293
  if (this._includeNodeIdentity) {
283
294
  Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
295
+ conditions.push({
296
+ property: "nodeIdentity",
297
+ value: nodeIdentity
298
+ });
284
299
  ObjectHelper.propertySet(entity, "nodeIdentity", nodeIdentity);
285
300
  }
286
- return this._entityStorage.set(entity);
301
+ return this._entityStorage.set(entity, conditions);
287
302
  }
288
303
  /**
289
304
  * Get an entity.
@@ -295,50 +310,7 @@ class EntityStorageService {
295
310
  */
296
311
  async get(id, secondaryIndex, userIdentity, nodeIdentity) {
297
312
  Guards.stringValue(this.CLASS_NAME, "id", id);
298
- const conditions = [];
299
- if (this._includeUserIdentity) {
300
- Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
301
- conditions.push({
302
- property: "userIdentity",
303
- comparison: ComparisonOperator.Equals,
304
- value: userIdentity
305
- });
306
- }
307
- if (this._includeNodeIdentity) {
308
- Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
309
- conditions.push({
310
- property: "nodeIdentity",
311
- comparison: ComparisonOperator.Equals,
312
- value: nodeIdentity
313
- });
314
- }
315
- if (Is.stringValue(secondaryIndex)) {
316
- conditions.push({
317
- property: secondaryIndex,
318
- comparison: ComparisonOperator.Equals,
319
- value: id
320
- });
321
- }
322
- if (conditions.length === 0) {
323
- const entity = await this._entityStorage.get(id, secondaryIndex);
324
- if (Is.empty(entity)) {
325
- throw new NotFoundError(this.CLASS_NAME, "entityNotFound", id);
326
- }
327
- ObjectHelper.propertyDelete(entity, "nodeIdentity");
328
- ObjectHelper.propertyDelete(entity, "userIdentity");
329
- return entity;
330
- }
331
- const results = await this._entityStorage.query({
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;
313
+ return this.internalGet(id, secondaryIndex, userIdentity, nodeIdentity);
342
314
  }
343
315
  /**
344
316
  * Remove the entity.
@@ -354,7 +326,6 @@ class EntityStorageService {
354
326
  Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
355
327
  conditions.push({
356
328
  property: "userIdentity",
357
- comparison: ComparisonOperator.Equals,
358
329
  value: userIdentity
359
330
  });
360
331
  }
@@ -362,37 +333,10 @@ class EntityStorageService {
362
333
  Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
363
334
  conditions.push({
364
335
  property: "nodeIdentity",
365
- comparison: ComparisonOperator.Equals,
366
336
  value: nodeIdentity
367
337
  });
368
338
  }
369
- if (conditions.length === 0) {
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);
379
- conditions.push({
380
- property: primaryKey.property,
381
- comparison: ComparisonOperator.Equals,
382
- value: id
383
- });
384
- const results = await this._entityStorage.query({
385
- conditions,
386
- logicalOperator: LogicalOperator.And
387
- }, undefined, undefined, undefined, 1);
388
- if (results.entities.length > 0) {
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
- }
395
- }
339
+ await this._entityStorage.remove(id, conditions);
396
340
  }
397
341
  /**
398
342
  * Query all the entities which match the conditions.
@@ -407,27 +351,26 @@ class EntityStorageService {
407
351
  * and a cursor which can be used to request more entities.
408
352
  */
409
353
  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
- }
354
+ const finalConditions = {
355
+ conditions: [],
356
+ logicalOperator: LogicalOperator.And
357
+ };
419
358
  if (this._includeNodeIdentity) {
420
359
  Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
421
- conditionsList.push({
360
+ finalConditions.conditions.push({
422
361
  property: "nodeIdentity",
423
362
  comparison: ComparisonOperator.Equals,
424
363
  value: nodeIdentity
425
364
  });
426
365
  }
427
- const finalConditions = {
428
- conditions: conditionsList,
429
- logicalOperator: LogicalOperator.And
430
- };
366
+ if (this._includeUserIdentity) {
367
+ Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
368
+ finalConditions.conditions.push({
369
+ property: "userIdentity",
370
+ comparison: ComparisonOperator.Equals,
371
+ value: userIdentity
372
+ });
373
+ }
431
374
  if (!Is.empty(conditions)) {
432
375
  finalConditions.conditions.push(conditions);
433
376
  }
@@ -438,6 +381,67 @@ class EntityStorageService {
438
381
  }
439
382
  return result;
440
383
  }
384
+ /**
385
+ * Get an entity.
386
+ * @param id The id of the entity to get, or the index value if secondaryIndex is set.
387
+ * @param secondaryIndex Get the item using a secondary index.
388
+ * @param userIdentity The user identity to use with storage operations.
389
+ * @param nodeIdentity The node identity to use with storage operations.
390
+ * @returns The object if it can be found or throws.
391
+ * @internal
392
+ */
393
+ async internalGet(id, secondaryIndex, userIdentity, nodeIdentity) {
394
+ const conditions = [];
395
+ if (this._includeUserIdentity) {
396
+ Guards.stringValue(this.CLASS_NAME, "userIdentity", userIdentity);
397
+ conditions.push({
398
+ property: "userIdentity",
399
+ comparison: ComparisonOperator.Equals,
400
+ value: userIdentity
401
+ });
402
+ }
403
+ if (this._includeNodeIdentity) {
404
+ Guards.stringValue(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
405
+ conditions.push({
406
+ property: "nodeIdentity",
407
+ comparison: ComparisonOperator.Equals,
408
+ value: nodeIdentity
409
+ });
410
+ }
411
+ if (Is.stringValue(secondaryIndex)) {
412
+ conditions.push({
413
+ property: secondaryIndex,
414
+ comparison: ComparisonOperator.Equals,
415
+ value: id
416
+ });
417
+ }
418
+ let entity;
419
+ if (conditions.length === 0) {
420
+ entity = await this._entityStorage.get(id, secondaryIndex);
421
+ }
422
+ else {
423
+ if (!Is.stringValue(secondaryIndex)) {
424
+ const schema = this._entityStorage.getSchema();
425
+ const primaryKey = EntitySchemaHelper.getPrimaryKey(schema);
426
+ conditions.unshift({
427
+ property: primaryKey.property,
428
+ comparison: ComparisonOperator.Equals,
429
+ value: id
430
+ });
431
+ }
432
+ const results = await this._entityStorage.query({
433
+ conditions,
434
+ logicalOperator: LogicalOperator.And
435
+ }, undefined, undefined, undefined, 1);
436
+ entity = results.entities[0];
437
+ }
438
+ if (Is.empty(entity)) {
439
+ throw new NotFoundError(this.CLASS_NAME, "entityNotFound", id);
440
+ }
441
+ ObjectHelper.propertyDelete(entity, "nodeIdentity");
442
+ ObjectHelper.propertyDelete(entity, "userIdentity");
443
+ return entity;
444
+ }
441
445
  }
442
446
 
443
447
  /**
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/entity-storage-service - Changelog
2
2
 
3
- ## v0.0.1-next.8
3
+ ## v0.0.1-next.9
4
4
 
5
5
  - Initial Release
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/entity-storage-service",
3
- "version": "0.0.1-next.8",
3
+ "version": "0.0.1-next.9",
4
4
  "description": "Entity Storage contract implementation and REST endpoint definitions",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,7 +17,7 @@
17
17
  "@twin.org/api-models": "next",
18
18
  "@twin.org/core": "next",
19
19
  "@twin.org/entity": "next",
20
- "@twin.org/entity-storage-models": "0.0.1-next.8",
20
+ "@twin.org/entity-storage-models": "0.0.1-next.9",
21
21
  "@twin.org/nameof": "next",
22
22
  "@twin.org/web": "next"
23
23
  },