@twin.org/entity-storage-connector-memory 0.0.1-next.7 → 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.
@@ -56,21 +56,23 @@ class MemoryEntityStorageConnector {
56
56
  * Get an entity.
57
57
  * @param id The id of the entity to get, or the index value if secondaryIndex is set.
58
58
  * @param secondaryIndex Get the item using a secondary index.
59
+ * @param conditions The optional conditions to match for the entities.
59
60
  * @returns The object if it can be found or undefined.
60
61
  */
61
- async get(id, secondaryIndex) {
62
+ async get(id, secondaryIndex, conditions) {
62
63
  core.Guards.stringValue(this.CLASS_NAME, "id", id);
63
- const lookupKey = secondaryIndex ?? this._primaryKey.property;
64
- return this._store.find(entity => entity[lookupKey] === id);
64
+ const index = this.findItem(id, secondaryIndex, conditions);
65
+ return index >= 0 ? this._store[index] : undefined;
65
66
  }
66
67
  /**
67
68
  * Set an entity.
68
69
  * @param entity The entity to set.
70
+ * @param conditions The optional conditions to match for the entities.
69
71
  * @returns The id of the entity.
70
72
  */
71
- async set(entity) {
73
+ async set(entity, conditions) {
72
74
  core.Guards.object(this.CLASS_NAME, "entity", entity);
73
- const existingIndex = this._store.findIndex(e => e[this._primaryKey.property] === entity[this._primaryKey.property]);
75
+ const existingIndex = this.findItem(entity[this._primaryKey.property], undefined, conditions);
74
76
  if (existingIndex >= 0) {
75
77
  this._store[existingIndex] = entity;
76
78
  }
@@ -81,11 +83,12 @@ class MemoryEntityStorageConnector {
81
83
  /**
82
84
  * Remove the entity.
83
85
  * @param id The id of the entity to remove.
86
+ * @param conditions The optional conditions to match for the entities.
84
87
  * @returns Nothing.
85
88
  */
86
- async remove(id) {
89
+ async remove(id, conditions) {
87
90
  core.Guards.stringValue(this.CLASS_NAME, "id", id);
88
- const index = this._store.findIndex(e => e[this._primaryKey.property] === id) ?? -1;
91
+ const index = this.findItem(id, undefined, conditions);
89
92
  if (index >= 0) {
90
93
  this._store.splice(index, 1);
91
94
  }
@@ -130,6 +133,50 @@ class MemoryEntityStorageConnector {
130
133
  getStore() {
131
134
  return this._store;
132
135
  }
136
+ /**
137
+ * Find the item in the store.
138
+ * @param id The id to search for.
139
+ * @param secondaryIndex The secondary index to search for.
140
+ * @param conditions The optional conditions to match for the entities.
141
+ * @returns The index of the item if found or -1.
142
+ * @internal
143
+ */
144
+ findItem(id, secondaryIndex, conditions) {
145
+ const finalConditions = [];
146
+ if (!core.Is.empty(secondaryIndex)) {
147
+ finalConditions.push({
148
+ property: secondaryIndex,
149
+ comparison: entity.ComparisonOperator.Equals,
150
+ value: id
151
+ });
152
+ }
153
+ if (core.Is.arrayValue(conditions)) {
154
+ // If we haven't added a secondary index condition we need to add the primary key condition.
155
+ if (finalConditions.length === 0) {
156
+ finalConditions.push({
157
+ property: this._primaryKey.property,
158
+ comparison: entity.ComparisonOperator.Equals,
159
+ value: id
160
+ });
161
+ }
162
+ finalConditions.push(...conditions.map(c => ({
163
+ property: c.property,
164
+ comparison: entity.ComparisonOperator.Equals,
165
+ value: c.value
166
+ })));
167
+ }
168
+ if (finalConditions.length > 0) {
169
+ for (let i = 0; i < this._store.length; i++) {
170
+ if (entity.EntityConditions.check(this._store[i], { conditions: finalConditions })) {
171
+ return i;
172
+ }
173
+ }
174
+ }
175
+ else {
176
+ return this._store.findIndex(e => e[this._primaryKey.property] === id);
177
+ }
178
+ return -1;
179
+ }
133
180
  }
134
181
 
135
182
  exports.MemoryEntityStorageConnector = MemoryEntityStorageConnector;
@@ -1,5 +1,5 @@
1
- import { Guards, Coerce, ObjectHelper } from '@twin.org/core';
2
- import { EntitySchemaFactory, EntitySchemaHelper, EntitySorter, EntityConditions } from '@twin.org/entity';
1
+ import { Guards, Coerce, ObjectHelper, Is } from '@twin.org/core';
2
+ import { EntitySchemaFactory, EntitySchemaHelper, EntitySorter, EntityConditions, ComparisonOperator } from '@twin.org/entity';
3
3
 
4
4
  // Copyright 2024 IOTA Stiftung.
5
5
  // SPDX-License-Identifier: Apache-2.0.
@@ -54,21 +54,23 @@ class MemoryEntityStorageConnector {
54
54
  * Get an entity.
55
55
  * @param id The id of the entity to get, or the index value if secondaryIndex is set.
56
56
  * @param secondaryIndex Get the item using a secondary index.
57
+ * @param conditions The optional conditions to match for the entities.
57
58
  * @returns The object if it can be found or undefined.
58
59
  */
59
- async get(id, secondaryIndex) {
60
+ async get(id, secondaryIndex, conditions) {
60
61
  Guards.stringValue(this.CLASS_NAME, "id", id);
61
- const lookupKey = secondaryIndex ?? this._primaryKey.property;
62
- return this._store.find(entity => entity[lookupKey] === id);
62
+ const index = this.findItem(id, secondaryIndex, conditions);
63
+ return index >= 0 ? this._store[index] : undefined;
63
64
  }
64
65
  /**
65
66
  * Set an entity.
66
67
  * @param entity The entity to set.
68
+ * @param conditions The optional conditions to match for the entities.
67
69
  * @returns The id of the entity.
68
70
  */
69
- async set(entity) {
71
+ async set(entity, conditions) {
70
72
  Guards.object(this.CLASS_NAME, "entity", entity);
71
- const existingIndex = this._store.findIndex(e => e[this._primaryKey.property] === entity[this._primaryKey.property]);
73
+ const existingIndex = this.findItem(entity[this._primaryKey.property], undefined, conditions);
72
74
  if (existingIndex >= 0) {
73
75
  this._store[existingIndex] = entity;
74
76
  }
@@ -79,11 +81,12 @@ class MemoryEntityStorageConnector {
79
81
  /**
80
82
  * Remove the entity.
81
83
  * @param id The id of the entity to remove.
84
+ * @param conditions The optional conditions to match for the entities.
82
85
  * @returns Nothing.
83
86
  */
84
- async remove(id) {
87
+ async remove(id, conditions) {
85
88
  Guards.stringValue(this.CLASS_NAME, "id", id);
86
- const index = this._store.findIndex(e => e[this._primaryKey.property] === id) ?? -1;
89
+ const index = this.findItem(id, undefined, conditions);
87
90
  if (index >= 0) {
88
91
  this._store.splice(index, 1);
89
92
  }
@@ -128,6 +131,50 @@ class MemoryEntityStorageConnector {
128
131
  getStore() {
129
132
  return this._store;
130
133
  }
134
+ /**
135
+ * Find the item in the store.
136
+ * @param id The id to search for.
137
+ * @param secondaryIndex The secondary index to search for.
138
+ * @param conditions The optional conditions to match for the entities.
139
+ * @returns The index of the item if found or -1.
140
+ * @internal
141
+ */
142
+ findItem(id, secondaryIndex, conditions) {
143
+ const finalConditions = [];
144
+ if (!Is.empty(secondaryIndex)) {
145
+ finalConditions.push({
146
+ property: secondaryIndex,
147
+ comparison: ComparisonOperator.Equals,
148
+ value: id
149
+ });
150
+ }
151
+ if (Is.arrayValue(conditions)) {
152
+ // If we haven't added a secondary index condition we need to add the primary key condition.
153
+ if (finalConditions.length === 0) {
154
+ finalConditions.push({
155
+ property: this._primaryKey.property,
156
+ comparison: ComparisonOperator.Equals,
157
+ value: id
158
+ });
159
+ }
160
+ finalConditions.push(...conditions.map(c => ({
161
+ property: c.property,
162
+ comparison: ComparisonOperator.Equals,
163
+ value: c.value
164
+ })));
165
+ }
166
+ if (finalConditions.length > 0) {
167
+ for (let i = 0; i < this._store.length; i++) {
168
+ if (EntityConditions.check(this._store[i], { conditions: finalConditions })) {
169
+ return i;
170
+ }
171
+ }
172
+ }
173
+ else {
174
+ return this._store.findIndex(e => e[this._primaryKey.property] === id);
175
+ }
176
+ return -1;
177
+ }
131
178
  }
132
179
 
133
180
  export { MemoryEntityStorageConnector };
@@ -25,21 +25,33 @@ export declare class MemoryEntityStorageConnector<T = unknown> implements IEntit
25
25
  * Get an entity.
26
26
  * @param id The id of the entity to get, or the index value if secondaryIndex is set.
27
27
  * @param secondaryIndex Get the item using a secondary index.
28
+ * @param conditions The optional conditions to match for the entities.
28
29
  * @returns The object if it can be found or undefined.
29
30
  */
30
- get(id: string, secondaryIndex?: keyof T): Promise<T | undefined>;
31
+ get(id: string, secondaryIndex?: keyof T, conditions?: {
32
+ property: keyof T;
33
+ value: unknown;
34
+ }[]): Promise<T | undefined>;
31
35
  /**
32
36
  * Set an entity.
33
37
  * @param entity The entity to set.
38
+ * @param conditions The optional conditions to match for the entities.
34
39
  * @returns The id of the entity.
35
40
  */
36
- set(entity: T): Promise<void>;
41
+ set(entity: T, conditions?: {
42
+ property: keyof T;
43
+ value: unknown;
44
+ }[]): Promise<void>;
37
45
  /**
38
46
  * Remove the entity.
39
47
  * @param id The id of the entity to remove.
48
+ * @param conditions The optional conditions to match for the entities.
40
49
  * @returns Nothing.
41
50
  */
42
- remove(id: string): Promise<void>;
51
+ remove(id: string, conditions?: {
52
+ property: keyof T;
53
+ value: unknown;
54
+ }[]): Promise<void>;
43
55
  /**
44
56
  * Find all the entities which match the conditions.
45
57
  * @param conditions The conditions to match for the entities.
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/entity-storage-connector-memory - Changelog
2
2
 
3
- ## v0.0.1-next.7
3
+ ## v0.0.1-next.9
4
4
 
5
5
  - Initial Release
@@ -66,7 +66,7 @@ The schema for the entities.
66
66
 
67
67
  ### get()
68
68
 
69
- > **get**(`id`, `secondaryIndex`?): `Promise`\<`undefined` \| `T`\>
69
+ > **get**(`id`, `secondaryIndex`?, `conditions`?): `Promise`\<`undefined` \| `T`\>
70
70
 
71
71
  Get an entity.
72
72
 
@@ -80,6 +80,10 @@ The id of the entity to get, or the index value if secondaryIndex is set.
80
80
 
81
81
  Get the item using a secondary index.
82
82
 
83
+ • **conditions?**: `object`[]
84
+
85
+ The optional conditions to match for the entities.
86
+
83
87
  #### Returns
84
88
 
85
89
  `Promise`\<`undefined` \| `T`\>
@@ -94,7 +98,7 @@ The object if it can be found or undefined.
94
98
 
95
99
  ### set()
96
100
 
97
- > **set**(`entity`): `Promise`\<`void`\>
101
+ > **set**(`entity`, `conditions`?): `Promise`\<`void`\>
98
102
 
99
103
  Set an entity.
100
104
 
@@ -104,6 +108,10 @@ Set an entity.
104
108
 
105
109
  The entity to set.
106
110
 
111
+ • **conditions?**: `object`[]
112
+
113
+ The optional conditions to match for the entities.
114
+
107
115
  #### Returns
108
116
 
109
117
  `Promise`\<`void`\>
@@ -118,7 +126,7 @@ The id of the entity.
118
126
 
119
127
  ### remove()
120
128
 
121
- > **remove**(`id`): `Promise`\<`void`\>
129
+ > **remove**(`id`, `conditions`?): `Promise`\<`void`\>
122
130
 
123
131
  Remove the entity.
124
132
 
@@ -128,6 +136,10 @@ Remove the entity.
128
136
 
129
137
  The id of the entity to remove.
130
138
 
139
+ • **conditions?**: `object`[]
140
+
141
+ The optional conditions to match for the entities.
142
+
131
143
  #### Returns
132
144
 
133
145
  `Promise`\<`void`\>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/entity-storage-connector-memory",
3
- "version": "0.0.1-next.7",
3
+ "version": "0.0.1-next.9",
4
4
  "description": "Entity Storage connector implementation using in-memory storage",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,7 +16,7 @@
16
16
  "dependencies": {
17
17
  "@twin.org/core": "next",
18
18
  "@twin.org/entity": "next",
19
- "@twin.org/entity-storage-models": "0.0.1-next.7",
19
+ "@twin.org/entity-storage-models": "0.0.1-next.9",
20
20
  "@twin.org/nameof": "next"
21
21
  },
22
22
  "main": "./dist/cjs/index.cjs",