@twin.org/entity-storage-connector-memory 0.0.3-next.3 → 0.0.3-next.30

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 Connector Memory
1
+ # Entity Storage Connector Memory
2
2
 
3
- Entity Storage connector implementation using in-memory storage.
3
+ This package provides an in-memory backend suited to local development, automated testing and short-lived workloads. 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
 
package/dist/es/index.js CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright 2024 IOTA Stiftung.
1
+ // Copyright 2026 IOTA Stiftung.
2
2
  // SPDX-License-Identifier: Apache-2.0.
3
3
  export * from "./memoryEntityStorageConnector.js";
4
4
  export * from "./models/IMemoryEntityStorageConnectorConstructorOptions.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,mCAAmC,CAAC;AAClD,cAAc,6DAA6D,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./memoryEntityStorageConnector.js\";\nexport * from \"./models/IMemoryEntityStorageConnectorConstructorOptions.js\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,mCAAmC,CAAC;AAClD,cAAc,6DAA6D,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./memoryEntityStorageConnector.js\";\nexport * from \"./models/IMemoryEntityStorageConnectorConstructorOptions.js\";\n"]}
@@ -1,10 +1,19 @@
1
1
  // Copyright 2024 IOTA Stiftung.
2
2
  // SPDX-License-Identifier: Apache-2.0.
3
3
  import { ContextIdHelper, ContextIdStore } from "@twin.org/context";
4
- import { Coerce, Guards, Is, ObjectHelper } from "@twin.org/core";
4
+ import { Coerce, ComponentFactory, Guards, HealthStatus, Is, Mutex, ObjectHelper, SharedObjectBuffer, Validation } from "@twin.org/core";
5
5
  import { ComparisonOperator, EntityConditions, EntitySchemaFactory, EntitySchemaHelper, EntitySorter, LogicalOperator } from "@twin.org/entity";
6
+ import { EntityStorageHelper } from "@twin.org/entity-storage-models";
6
7
  /**
7
- * Class for performing entity storage operations in-memory.
8
+ * Class for performing entity storage operations in-memory backed by a shared object buffer.
9
+ *
10
+ * All reads and writes are serialised with a per-schema lock so that concurrent async
11
+ * access, including across worker threads, never produces torn or lost updates.
12
+ *
13
+ * All connector instances that share the same entity schema name share the same underlying
14
+ * buffer, making data written in one instance immediately visible in another, including
15
+ * across worker threads when the main thread forwards worker messages to the lock and
16
+ * buffer handlers.
8
17
  */
9
18
  export class MemoryEntityStorageConnector {
10
19
  /**
@@ -37,10 +46,21 @@ export class MemoryEntityStorageConnector {
37
46
  */
38
47
  _primaryKey;
39
48
  /**
40
- * The storage for the in-memory items.
49
+ * The resolved schema name used as the shared buffer and lock key.
50
+ * Stored separately so it is always a plain string rather than string | undefined.
41
51
  * @internal
42
52
  */
43
- _store;
53
+ _schemaName;
54
+ /**
55
+ * Initial capacity hint in bytes for the shared entity buffer.
56
+ * @internal
57
+ */
58
+ _initialCapacityBytes;
59
+ /**
60
+ * Maximum capacity in bytes for the shared entity buffer.
61
+ * @internal
62
+ */
63
+ _maxCapacityBytes;
44
64
  /**
45
65
  * Create a new instance of MemoryEntityStorageConnector.
46
66
  * @param options The options for the connector.
@@ -49,9 +69,11 @@ export class MemoryEntityStorageConnector {
49
69
  Guards.object(MemoryEntityStorageConnector.CLASS_NAME, "options", options);
50
70
  Guards.stringValue(MemoryEntityStorageConnector.CLASS_NAME, "options.entitySchema", options.entitySchema);
51
71
  this._entitySchema = EntitySchemaFactory.get(options.entitySchema);
72
+ this._schemaName = this._entitySchema.type ?? options.entitySchema;
52
73
  this._partitionContextIds = options.partitionContextIds;
53
74
  this._primaryKey = EntitySchemaHelper.getPrimaryKey(this._entitySchema);
54
- this._store = [];
75
+ this._initialCapacityBytes = options.initialCapacityBytes;
76
+ this._maxCapacityBytes = options.maxCapacityBytes;
55
77
  }
56
78
  /**
57
79
  * Returns the class name of the component.
@@ -60,6 +82,20 @@ export class MemoryEntityStorageConnector {
60
82
  className() {
61
83
  return MemoryEntityStorageConnector.CLASS_NAME;
62
84
  }
85
+ /**
86
+ * Returns the health status of the component.
87
+ * @returns The health status of the component.
88
+ */
89
+ async health() {
90
+ return [
91
+ {
92
+ source: MemoryEntityStorageConnector.CLASS_NAME,
93
+ status: HealthStatus.Ok,
94
+ description: "healthDescription",
95
+ data: { entityType: this._schemaName }
96
+ }
97
+ ];
98
+ }
63
99
  /**
64
100
  * Get the schema for the entities.
65
101
  * @returns The schema for the entities.
@@ -67,6 +103,18 @@ export class MemoryEntityStorageConnector {
67
103
  getSchema() {
68
104
  return this._entitySchema;
69
105
  }
106
+ /**
107
+ * Bootstrap the component by creating and initializing any resources it needs.
108
+ * @param nodeLoggingComponentType The node logging component type.
109
+ * @returns True if the bootstrapping process was successful.
110
+ */
111
+ async bootstrap(nodeLoggingComponentType) {
112
+ await SharedObjectBuffer.create(this._schemaName, {
113
+ initialCapacityBytes: this._initialCapacityBytes,
114
+ maxCapacityBytes: this._maxCapacityBytes
115
+ });
116
+ return true;
117
+ }
70
118
  /**
71
119
  * Get an entity.
72
120
  * @param id The id of the entity to get, or the index value if secondaryIndex is set.
@@ -85,40 +133,84 @@ export class MemoryEntityStorageConnector {
85
133
  value: partitionKey
86
134
  });
87
135
  }
88
- const index = this.findItem(id, secondaryIndex, finalConditions);
89
- const item = index >= 0 ? ObjectHelper.clone(this._store[index]) : undefined;
90
- if (Is.objectValue(item)) {
91
- ObjectHelper.propertyDelete(item, MemoryEntityStorageConnector._PARTITION_KEY);
92
- }
93
- return item;
136
+ return this.withLock(entities => {
137
+ const index = this.findItem(entities, id, secondaryIndex, finalConditions);
138
+ const item = index >= 0 ? entities[index] : undefined;
139
+ if (Is.objectValue(item)) {
140
+ return {
141
+ result: EntityStorageHelper.unPrepareEntity(item, [
142
+ MemoryEntityStorageConnector._PARTITION_KEY
143
+ ])
144
+ };
145
+ }
146
+ return { result: undefined };
147
+ });
94
148
  }
95
149
  /**
96
150
  * Set an entity.
97
151
  * @param entity The entity to set.
98
152
  * @param conditions The optional conditions to match for the entities.
99
- * @returns The id of the entity.
153
+ * @returns Resolves when the entity has been stored.
100
154
  */
101
155
  async set(entity, conditions) {
102
156
  Guards.object(MemoryEntityStorageConnector.CLASS_NAME, "entity", entity);
103
157
  const contextIds = await ContextIdStore.getContextIds();
104
158
  const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
105
- EntitySchemaHelper.validateEntity(entity, this.getSchema());
106
159
  const finalConditions = conditions ?? [];
107
- const finalEntity = ObjectHelper.clone(entity);
160
+ const prepared = EntityStorageHelper.prepareEntity(entity, this._entitySchema, Is.stringValue(partitionKey)
161
+ ? [{ property: MemoryEntityStorageConnector._PARTITION_KEY, value: partitionKey }]
162
+ : undefined, { nullBehavior: "omit" });
108
163
  if (Is.stringValue(partitionKey)) {
109
164
  finalConditions.push({
110
165
  property: MemoryEntityStorageConnector._PARTITION_KEY,
111
166
  value: partitionKey
112
167
  });
113
- ObjectHelper.propertySet(finalEntity, MemoryEntityStorageConnector._PARTITION_KEY, partitionKey);
114
- }
115
- const existingIndex = this.findItem(finalEntity[this._primaryKey.property], undefined, finalConditions);
116
- if (existingIndex >= 0) {
117
- this._store[existingIndex] = finalEntity;
118
- }
119
- else {
120
- this._store.push(finalEntity);
121
168
  }
169
+ return this.withLock(entities => {
170
+ const existingIndex = this.findItem(entities, prepared[this._primaryKey.property], undefined, finalConditions);
171
+ if (existingIndex >= 0) {
172
+ entities[existingIndex] = prepared;
173
+ }
174
+ else {
175
+ entities.push(prepared);
176
+ }
177
+ return { updated: entities, result: undefined };
178
+ });
179
+ }
180
+ /**
181
+ * Set multiple entities in a batch.
182
+ * @param entities The entities to set.
183
+ * @returns Nothing.
184
+ */
185
+ async setBatch(entities) {
186
+ Guards.arrayValue(MemoryEntityStorageConnector.CLASS_NAME, "entities", entities);
187
+ const contextIds = await ContextIdStore.getContextIds();
188
+ const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
189
+ const preparedItems = entities.map(entity => EntityStorageHelper.prepareEntity(entity, this._entitySchema, Is.stringValue(partitionKey)
190
+ ? [{ property: MemoryEntityStorageConnector._PARTITION_KEY, value: partitionKey }]
191
+ : undefined, { nullBehavior: "omit" }));
192
+ return this.withLock(store => {
193
+ const indexMap = new Map();
194
+ for (let i = 0; i < store.length; i++) {
195
+ const stored = store[i];
196
+ const storedPartition = ObjectHelper.propertyGet(stored, MemoryEntityStorageConnector._PARTITION_KEY);
197
+ if (!Is.stringValue(partitionKey) || storedPartition === partitionKey) {
198
+ indexMap.set(stored[this._primaryKey.property], i);
199
+ }
200
+ }
201
+ for (const prepared of preparedItems) {
202
+ const id = prepared[this._primaryKey.property];
203
+ const existingIndex = indexMap.get(id);
204
+ if (existingIndex !== undefined) {
205
+ store[existingIndex] = prepared;
206
+ }
207
+ else {
208
+ const newIndex = store.push(prepared) - 1;
209
+ indexMap.set(id, newIndex);
210
+ }
211
+ }
212
+ return { updated: store, result: undefined };
213
+ });
122
214
  }
123
215
  /**
124
216
  * Remove the entity.
@@ -137,10 +229,13 @@ export class MemoryEntityStorageConnector {
137
229
  value: partitionKey
138
230
  });
139
231
  }
140
- const index = this.findItem(id, undefined, finalConditions);
141
- if (index >= 0) {
142
- this._store.splice(index, 1);
143
- }
232
+ return this.withLock(entities => {
233
+ const index = this.findItem(entities, id, undefined, finalConditions);
234
+ if (index >= 0) {
235
+ entities.splice(index, 1);
236
+ }
237
+ return { updated: entities, result: undefined };
238
+ });
144
239
  }
145
240
  /**
146
241
  * Find all the entities which match the conditions.
@@ -155,7 +250,135 @@ export class MemoryEntityStorageConnector {
155
250
  async query(conditions, sortProperties, properties, cursor, limit) {
156
251
  const contextIds = await ContextIdStore.getContextIds();
157
252
  const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
158
- let allEntities = this._store.slice();
253
+ EntityStorageHelper.validateSortProperties(this._entitySchema, sortProperties);
254
+ EntityStorageHelper.validateProperties(this._entitySchema, properties);
255
+ if (!Is.empty(limit)) {
256
+ const validationFailures = [];
257
+ Validation.integer("limit", limit, validationFailures, undefined, { minValue: 1 });
258
+ Validation.asValidationError(MemoryEntityStorageConnector.CLASS_NAME, "query", validationFailures);
259
+ }
260
+ return this.withLock(store => {
261
+ let allEntities = store.slice();
262
+ const finalConditions = {
263
+ conditions: [],
264
+ logicalOperator: LogicalOperator.And
265
+ };
266
+ if (Is.stringValue(partitionKey)) {
267
+ finalConditions.conditions.push({
268
+ property: MemoryEntityStorageConnector._PARTITION_KEY,
269
+ comparison: ComparisonOperator.Equals,
270
+ value: partitionKey
271
+ });
272
+ }
273
+ if (!Is.empty(conditions)) {
274
+ finalConditions.conditions.push(EntityStorageHelper.normalizeConditionValues(conditions));
275
+ }
276
+ const resultEntities = [];
277
+ const finalLimit = limit ?? MemoryEntityStorageConnector._DEFAULT_LIMIT;
278
+ let nextCursor;
279
+ if (allEntities.length > 0) {
280
+ const finalSortKeys = EntitySchemaHelper.buildSortProperties(this._entitySchema, sortProperties);
281
+ allEntities = EntitySorter.sort(allEntities, finalSortKeys);
282
+ const startIndex = Coerce.number(cursor) ?? 0;
283
+ for (let i = startIndex; i < allEntities.length; i++) {
284
+ if (EntityConditions.check(allEntities[i], finalConditions) &&
285
+ resultEntities.length < finalLimit) {
286
+ const entity = Is.arrayValue(properties)
287
+ ? ObjectHelper.pick(allEntities[i], properties)
288
+ : allEntities[i];
289
+ resultEntities.push(EntityStorageHelper.unPrepareEntity(entity, [
290
+ MemoryEntityStorageConnector._PARTITION_KEY
291
+ ]));
292
+ if (resultEntities.length >= finalLimit) {
293
+ if (i < allEntities.length - 1) {
294
+ nextCursor = (i + 1).toString();
295
+ }
296
+ break;
297
+ }
298
+ }
299
+ }
300
+ }
301
+ return { result: { entities: resultEntities, cursor: nextCursor } };
302
+ });
303
+ }
304
+ /**
305
+ * Remove all entities from the storage.
306
+ * @returns Nothing.
307
+ */
308
+ async empty() {
309
+ const contextIds = await ContextIdStore.getContextIds();
310
+ const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
311
+ return this.withLock(entities => {
312
+ if (Is.stringValue(partitionKey)) {
313
+ const filtered = entities.filter(item => ObjectHelper.propertyGet(item, MemoryEntityStorageConnector._PARTITION_KEY) !==
314
+ partitionKey);
315
+ return { updated: filtered, result: undefined };
316
+ }
317
+ return { updated: [], result: undefined };
318
+ });
319
+ }
320
+ /**
321
+ * Remove multiple entities by id.
322
+ * @param ids The ids of the entities to remove.
323
+ * @returns Nothing.
324
+ */
325
+ async removeBatch(ids) {
326
+ Guards.arrayValue(MemoryEntityStorageConnector.CLASS_NAME, "ids", ids);
327
+ const contextIds = await ContextIdStore.getContextIds();
328
+ const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
329
+ const finalConditions = [];
330
+ if (Is.stringValue(partitionKey)) {
331
+ finalConditions.push({
332
+ property: MemoryEntityStorageConnector._PARTITION_KEY,
333
+ value: partitionKey
334
+ });
335
+ }
336
+ return this.withLock(entities => {
337
+ for (const id of ids) {
338
+ const index = this.findItem(entities, id, undefined, finalConditions);
339
+ if (index >= 0) {
340
+ entities.splice(index, 1);
341
+ }
342
+ }
343
+ return { updated: entities, result: undefined };
344
+ });
345
+ }
346
+ /**
347
+ * Teardown the storage by clearing the underlying shared buffer for this schema.
348
+ * @param nodeLoggingComponentType The node logging component type.
349
+ * @returns True if the teardown process was successful.
350
+ */
351
+ async teardown(nodeLoggingComponentType) {
352
+ const nodeLogging = ComponentFactory.getIfExists(nodeLoggingComponentType);
353
+ await nodeLogging?.log({
354
+ level: "info",
355
+ source: MemoryEntityStorageConnector.CLASS_NAME,
356
+ ts: Date.now(),
357
+ message: "storeTearingDown"
358
+ });
359
+ await Mutex.lock(this._schemaName, { throwOnTimeout: true });
360
+ try {
361
+ SharedObjectBuffer.remove(this._schemaName);
362
+ }
363
+ finally {
364
+ Mutex.unlock(this._schemaName);
365
+ }
366
+ await nodeLogging?.log({
367
+ level: "info",
368
+ source: MemoryEntityStorageConnector.CLASS_NAME,
369
+ ts: Date.now(),
370
+ message: "storeTornDown"
371
+ });
372
+ return true;
373
+ }
374
+ /**
375
+ * Count all the entities which match the conditions.
376
+ * @param conditions The optional conditions to match for the entities.
377
+ * @returns The total count of entities in the storage.
378
+ */
379
+ async count(conditions) {
380
+ const contextIds = await ContextIdStore.getContextIds();
381
+ const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
159
382
  const finalConditions = {
160
383
  conditions: [],
161
384
  logicalOperator: LogicalOperator.And
@@ -168,51 +391,124 @@ export class MemoryEntityStorageConnector {
168
391
  });
169
392
  }
170
393
  if (!Is.empty(conditions)) {
171
- finalConditions.conditions.push(conditions);
172
- }
173
- const entities = [];
174
- const finalLimit = limit ?? MemoryEntityStorageConnector._DEFAULT_LIMIT;
175
- let nextCursor;
176
- if (allEntities.length > 0) {
177
- const finalSortKeys = EntitySchemaHelper.buildSortProperties(this._entitySchema, sortProperties);
178
- allEntities = EntitySorter.sort(allEntities, finalSortKeys);
179
- const startIndex = Coerce.number(cursor) ?? 0;
180
- for (let i = startIndex; i < allEntities.length; i++) {
181
- if (EntityConditions.check(allEntities[i], finalConditions) &&
182
- entities.length < finalLimit) {
183
- const entity = ObjectHelper.clone(ObjectHelper.pick(allEntities[i], properties));
184
- ObjectHelper.propertyDelete(entity, MemoryEntityStorageConnector._PARTITION_KEY);
185
- entities.push(entity);
186
- if (entities.length >= finalLimit) {
187
- if (i < allEntities.length - 1) {
188
- nextCursor = (i + 1).toString();
189
- }
190
- break;
191
- }
394
+ finalConditions.conditions.push(EntityStorageHelper.normalizeConditionValues(conditions));
395
+ }
396
+ return this.withLock(entities => {
397
+ if (finalConditions.conditions.length === 0) {
398
+ return { result: entities.length };
399
+ }
400
+ return {
401
+ result: entities.filter(item => EntityConditions.check(item, finalConditions)).length
402
+ };
403
+ });
404
+ }
405
+ /**
406
+ * Get all entities in the memory store.
407
+ * @returns All stored entities with partition keys removed.
408
+ */
409
+ async getStore() {
410
+ return this.withLock(entities => ({
411
+ result: entities.map(item => EntityStorageHelper.unPrepareEntity(item, [MemoryEntityStorageConnector._PARTITION_KEY]))
412
+ }));
413
+ }
414
+ /**
415
+ * Get a unique list of all the context ids from the storage.
416
+ * @returns The list of unique context ids.
417
+ */
418
+ async getPartitionContextIds() {
419
+ return this.withLock(entities => {
420
+ const contextIds = {};
421
+ for (const entity of entities) {
422
+ const partitionId = ObjectHelper.propertyGet(entity, MemoryEntityStorageConnector._PARTITION_KEY);
423
+ if (Is.stringValue(partitionId)) {
424
+ contextIds[partitionId] = ContextIdHelper.shortSplit(this._partitionContextIds ?? [], partitionId);
192
425
  }
193
426
  }
427
+ return { result: Object.values(contextIds) };
428
+ });
429
+ }
430
+ /**
431
+ * Create the target connector for performing the migration it will use a temporary storage location.
432
+ * @param newEntitySchema The name of the new entity schema to create the connector for.
433
+ * @returns Connector for performing the migration.
434
+ */
435
+ async createTargetConnector(newEntitySchema) {
436
+ // Resolve the target schema name the same way _schemaName is resolved in the constructor.
437
+ const targetSchemaEntry = EntitySchemaFactory.get(newEntitySchema);
438
+ const targetSchemaName = targetSchemaEntry.type ?? newEntitySchema;
439
+ // When migrating to a different schema, wipe the target buffer so that every
440
+ // migration starts from an empty store regardless of any previous connector
441
+ // instances that shared the same schema name.
442
+ if (targetSchemaName !== this._schemaName) {
443
+ await Mutex.lock(targetSchemaName, { throwOnTimeout: true });
444
+ try {
445
+ SharedObjectBuffer.remove(targetSchemaName);
446
+ }
447
+ finally {
448
+ Mutex.unlock(targetSchemaName);
449
+ }
194
450
  }
195
- return {
196
- entities,
197
- cursor: nextCursor
198
- };
451
+ return new MemoryEntityStorageConnector({
452
+ entitySchema: newEntitySchema,
453
+ partitionContextIds: this._partitionContextIds,
454
+ initialCapacityBytes: this._initialCapacityBytes,
455
+ maxCapacityBytes: this._maxCapacityBytes
456
+ });
457
+ }
458
+ /**
459
+ * Finalize the migration by tearing down the old connector and replacing it with the target connector.
460
+ * @param targetConnector The target connector to finalize the migration with.
461
+ * @param options The options to control how the migration is finalized.
462
+ * @param loggingComponentType The optional component type to use for logging the migration progress.
463
+ * @returns A promise that resolves when the migration is finalized.
464
+ */
465
+ async finalizeMigration(targetConnector, options, loggingComponentType) {
466
+ return targetConnector;
199
467
  }
200
468
  /**
201
- * Get the memory store.
202
- * @returns The store.
469
+ * Cleanup the migration if a migration fails or needs to be aborted.
470
+ * @param targetConnector The target connector to cleanup the migration with.
471
+ * @param options The options to control how the migration is cleaned up.
472
+ * @param loggingComponentType The optional component type to use for logging the migration progress.
473
+ * @returns A promise that resolves when the migration is cleaned up.
474
+ */
475
+ async cleanupMigration(targetConnector, options, loggingComponentType) { }
476
+ /**
477
+ * Acquires the schema-keyed lock, runs fn with the current entity array, optionally
478
+ * writes back a modified array, then releases the lock.
479
+ * @param fn The synchronous function to run while the lock is held.
480
+ * @returns The result produced by fn.
481
+ * @internal
203
482
  */
204
- getStore() {
205
- return this._store;
483
+ async withLock(fn) {
484
+ const key = this._schemaName;
485
+ await Mutex.lock(key, { throwOnTimeout: true });
486
+ try {
487
+ await SharedObjectBuffer.create(key, {
488
+ initialCapacityBytes: this._initialCapacityBytes,
489
+ maxCapacityBytes: this._maxCapacityBytes
490
+ });
491
+ const entities = (await SharedObjectBuffer.read(key)) ?? [];
492
+ const outcome = fn(entities);
493
+ if (outcome.updated !== undefined) {
494
+ await SharedObjectBuffer.write(key, outcome.updated);
495
+ }
496
+ return outcome.result;
497
+ }
498
+ finally {
499
+ Mutex.unlock(key);
500
+ }
206
501
  }
207
502
  /**
208
- * Find the item in the store.
503
+ * Find the item in the provided entity array.
504
+ * @param entities The current entity array.
209
505
  * @param id The id to search for.
210
506
  * @param secondaryIndex The secondary index to search for.
211
507
  * @param conditions The optional conditions to match for the entities.
212
508
  * @returns The index of the item if found or -1.
213
509
  * @internal
214
510
  */
215
- findItem(id, secondaryIndex, conditions) {
511
+ findItem(entities, id, secondaryIndex, conditions) {
216
512
  const finalConditions = [];
217
513
  if (!Is.empty(secondaryIndex)) {
218
514
  finalConditions.push({
@@ -222,7 +518,6 @@ export class MemoryEntityStorageConnector {
222
518
  });
223
519
  }
224
520
  if (Is.arrayValue(conditions)) {
225
- // If we haven't added a secondary index condition we need to add the primary key condition.
226
521
  if (finalConditions.length === 0) {
227
522
  finalConditions.push({
228
523
  property: this._primaryKey.property,
@@ -237,14 +532,14 @@ export class MemoryEntityStorageConnector {
237
532
  })));
238
533
  }
239
534
  if (finalConditions.length > 0) {
240
- for (let i = 0; i < this._store.length; i++) {
241
- if (EntityConditions.check(this._store[i], { conditions: finalConditions })) {
535
+ for (let i = 0; i < entities.length; i++) {
536
+ if (EntityConditions.check(entities[i], { conditions: finalConditions })) {
242
537
  return i;
243
538
  }
244
539
  }
245
540
  }
246
541
  else {
247
- return this._store.findIndex(e => e[this._primaryKey.property] === id);
542
+ return entities.findIndex(e => e[this._primaryKey.property] === id);
248
543
  }
249
544
  return -1;
250
545
  }