@oimdb/core 1.2.2 → 1.3.0

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/index.d.cts CHANGED
@@ -12,6 +12,30 @@ declare abstract class OIMCollectionStore<TEntity extends object, TPk extends TO
12
12
  abstract clear(): void;
13
13
  }
14
14
 
15
+ declare abstract class OIMIndexStoreSetBased<TKey extends TOIMPk, TPk extends TOIMPk> {
16
+ abstract setOneByKey(key: TKey, pks: Set<TPk>): void;
17
+ abstract removeOneByKey(key: TKey): void;
18
+ abstract removeManyByKeys(keys: readonly TKey[]): void;
19
+ abstract getOneByKey(key: TKey): Set<TPk> | undefined;
20
+ abstract getManyByKeys(keys: readonly TKey[]): Map<TKey, Set<TPk>>;
21
+ abstract getAllKeys(): TKey[];
22
+ abstract getAll(): Map<TKey, Set<TPk>>;
23
+ abstract countAll(): number;
24
+ abstract clear(): void;
25
+ }
26
+
27
+ declare abstract class OIMIndexStoreArrayBased<TKey extends TOIMPk, TPk extends TOIMPk> {
28
+ abstract setOneByKey(key: TKey, pks: TPk[]): void;
29
+ abstract removeOneByKey(key: TKey): void;
30
+ abstract removeManyByKeys(keys: readonly TKey[]): void;
31
+ abstract getOneByKey(key: TKey): TPk[] | undefined;
32
+ abstract getManyByKeys(keys: readonly TKey[]): Map<TKey, TPk[]>;
33
+ abstract getAllKeys(): TKey[];
34
+ abstract getAll(): Map<TKey, TPk[]>;
35
+ abstract countAll(): number;
36
+ abstract clear(): void;
37
+ }
38
+
15
39
  type TOIMEventHandler<T> = (payload: T) => void;
16
40
 
17
41
  declare class OIMEventEmitter<T extends Record<string, unknown>> {
@@ -61,36 +85,20 @@ interface TOIMIndexUpdatePayload<TKey> {
61
85
  * Returns true if the arrays are considered equal, false if they differ. */
62
86
  type TOIMIndexComparator<TPk extends TOIMPk> = (existingPks: readonly TPk[], newPks: readonly TPk[]) => boolean;
63
87
 
64
- declare abstract class OIMIndexStore<TKey extends TOIMPk, TPk extends TOIMPk> {
65
- abstract setOneByKey(key: TKey, pks: Set<TPk>): void;
66
- abstract removeOneByKey(key: TKey): void;
67
- abstract removeManyByKeys(keys: readonly TKey[]): void;
68
- abstract getOneByKey(key: TKey): Set<TPk> | undefined;
69
- abstract getManyByKeys(keys: readonly TKey[]): Map<TKey, Set<TPk>>;
70
- abstract getAllKeys(): TKey[];
71
- abstract getAll(): Map<TKey, Set<TPk>>;
72
- abstract countAll(): number;
73
- abstract clear(): void;
74
- }
75
-
76
- /**
77
- * Configuration options for index instances */
78
- type TOIMIndexOptions<TIndexKey extends TOIMPk, TPk extends TOIMPk> = {
79
- comparePks?: TOIMIndexComparator<TPk>;
80
- store?: OIMIndexStore<TIndexKey, TPk>;
81
- };
82
-
83
88
  /**
84
- * Abstract base class for all index types.
85
- * Provides common functionality and event system for key-to-PKs mappings.
89
+ * Abstract base class for Set-based index types.
90
+ * Provides common functionality and event system for key-to-PKs mappings using Set storage.
86
91
  */
87
- declare abstract class OIMIndex<TKey extends TOIMPk, TPk extends TOIMPk> {
92
+ declare abstract class OIMIndexSetBased<TKey extends TOIMPk, TPk extends TOIMPk> {
88
93
  protected readonly comparePks?: TOIMIndexComparator<TPk>;
89
- protected readonly store: OIMIndexStore<TKey, TPk>;
94
+ protected readonly store: OIMIndexStoreSetBased<TKey, TPk>;
90
95
  readonly emitter: OIMEventEmitter<{
91
96
  0: TOIMIndexUpdatePayload<TKey>;
92
97
  }>;
93
- constructor(options?: TOIMIndexOptions<TKey, TPk>);
98
+ constructor(options?: {
99
+ comparePks?: TOIMIndexComparator<TPk>;
100
+ store?: OIMIndexStoreSetBased<TKey, TPk>;
101
+ });
94
102
  /**
95
103
  * Get primary keys for multiple index keys
96
104
  */
@@ -150,11 +158,81 @@ declare abstract class OIMIndex<TKey extends TOIMPk, TPk extends TOIMPk> {
150
158
  }
151
159
 
152
160
  /**
153
- * Manual index that allows direct manipulation of key-to-primary-keys mappings.
154
- * Extends the abstract OIMIndex with manual control methods and optional comparison.
161
+ * Abstract base class for Array-based index types.
162
+ * Provides common functionality and event system for key-to-PKs mappings using Array storage.
163
+ */
164
+ declare abstract class OIMIndexArrayBased<TKey extends TOIMPk, TPk extends TOIMPk> {
165
+ protected readonly comparePks?: TOIMIndexComparator<TPk>;
166
+ protected readonly store: OIMIndexStoreArrayBased<TKey, TPk>;
167
+ readonly emitter: OIMEventEmitter<{
168
+ 0: TOIMIndexUpdatePayload<TKey>;
169
+ }>;
170
+ constructor(options?: {
171
+ comparePks?: TOIMIndexComparator<TPk>;
172
+ store?: OIMIndexStoreArrayBased<TKey, TPk>;
173
+ });
174
+ /**
175
+ * Get primary keys for multiple index keys
176
+ */
177
+ getPksByKeys(keys: readonly TKey[]): Map<TKey, TPk[]>;
178
+ /**
179
+ * Get primary keys for a specific index key
180
+ */
181
+ getPksByKey(key: TKey): TPk[];
182
+ /**
183
+ * Check if an index key exists
184
+ */
185
+ hasKey(key: TKey): boolean;
186
+ /**
187
+ * Get all index keys
188
+ */
189
+ getKeys(): readonly TKey[];
190
+ /**
191
+ * Get the number of primary keys for a specific index key
192
+ */
193
+ getKeySize(key: TKey): number;
194
+ /**
195
+ * Get total number of index keys
196
+ */
197
+ get size(): number;
198
+ /**
199
+ * Check if the index is empty
200
+ */
201
+ get isEmpty(): boolean;
202
+ /**
203
+ * Get performance metrics for monitoring and debugging
204
+ */
205
+ getMetrics(): {
206
+ totalKeys: number;
207
+ totalPks: number;
208
+ averagePksPerKey: number;
209
+ maxBucketSize: number;
210
+ minBucketSize: number;
211
+ };
212
+ /**
213
+ * Clean up event listeners when index is no longer needed
214
+ */
215
+ destroy(): void;
216
+ /**
217
+ * Set primary keys for a specific index key with optional comparison.
218
+ * If comparator is provided and returns true (no changes), skip the update.
219
+ */
220
+ protected setPksWithComparison(key: TKey, newPks: TPk[]): boolean;
221
+ /**
222
+ * Emit update event with changed keys
223
+ */
224
+ protected emitUpdate(keys: TKey[]): void;
225
+ }
226
+
227
+ /**
228
+ * Manual Set-based index that allows direct manipulation of key-to-primary-keys mappings.
229
+ * Extends the abstract OIMIndexSetBased with manual control methods and optional comparison.
155
230
  */
156
- declare class OIMIndexManual<TIndexKey extends TOIMPk, TPk extends TOIMPk> extends OIMIndex<TIndexKey, TPk> {
157
- constructor(options?: TOIMIndexOptions<TIndexKey, TPk>);
231
+ declare class OIMIndexManualSetBased<TIndexKey extends TOIMPk, TPk extends TOIMPk> extends OIMIndexSetBased<TIndexKey, TPk> {
232
+ constructor(options?: {
233
+ comparePks?: TOIMIndexComparator<TPk>;
234
+ store?: OIMIndexStoreSetBased<TIndexKey, TPk>;
235
+ });
158
236
  /**
159
237
  * Set primary keys for a specific index key, replacing any existing values.
160
238
  * Uses optional comparator to skip updates if PKs haven't actually changed.
@@ -189,6 +267,7 @@ declare class OIMDBSettings {
189
267
  /**
190
268
  * Base abstract coalescer that groups updates by keys and emits consolidated events.
191
269
  * Designed to be extended for different entity types (collections, indexes, etc.).
270
+ * Uses SetBased indexes for efficient key tracking.
192
271
  */
193
272
  declare abstract class OIMUpdateEventCoalescer<TKey extends TOIMPk> {
194
273
  readonly emitter: OIMEventEmitter<{
@@ -196,9 +275,9 @@ declare abstract class OIMUpdateEventCoalescer<TKey extends TOIMPk> {
196
275
  1: void;
197
276
  2: void;
198
277
  }>;
199
- protected readonly updatedKeysIndex: OIMIndexManual<typeof OIMDBSettings.UPDATED_KEYS_INDEX_KEY, TKey>;
278
+ protected readonly updatedKeysIndex: OIMIndexManualSetBased<typeof OIMDBSettings.UPDATED_KEYS_INDEX_KEY, TKey>;
200
279
  private hasEmittedChanges;
201
- constructor(index?: OIMIndexManual<typeof OIMDBSettings.UPDATED_KEYS_INDEX_KEY, TKey>);
280
+ constructor(index?: OIMIndexManualSetBased<typeof OIMDBSettings.UPDATED_KEYS_INDEX_KEY, TKey>);
202
281
  /**
203
282
  * Get the set of keys that have been updated since last clear
204
283
  */
@@ -332,7 +411,7 @@ declare class OIMUpdateEventCoalescerIndex<TIndexKey extends TOIMPk> extends OIM
332
411
  private handleUpdate;
333
412
  }
334
413
 
335
- declare abstract class OIMReactiveIndex<TKey extends TOIMPk, TPk extends TOIMPk, TIndex extends OIMIndex<TKey, TPk>> {
414
+ declare abstract class OIMReactiveIndexSetBased<TKey extends TOIMPk, TPk extends TOIMPk, TIndex extends OIMIndexSetBased<TKey, TPk>> {
336
415
  readonly index: TIndex;
337
416
  readonly updateEventEmitter: OIMUpdateEventEmitter<TKey>;
338
417
  readonly coalescer: OIMUpdateEventCoalescerIndex<TKey>;
@@ -357,6 +436,31 @@ declare abstract class OIMReactiveIndex<TKey extends TOIMPk, TPk extends TOIMPk,
357
436
  destroy(): void;
358
437
  }
359
438
 
439
+ declare abstract class OIMReactiveIndexArrayBased<TKey extends TOIMPk, TPk extends TOIMPk, TIndex extends OIMIndexArrayBased<TKey, TPk>> {
440
+ readonly index: TIndex;
441
+ readonly updateEventEmitter: OIMUpdateEventEmitter<TKey>;
442
+ readonly coalescer: OIMUpdateEventCoalescerIndex<TKey>;
443
+ constructor(queue: OIMEventQueue, opts?: {
444
+ index: TIndex;
445
+ });
446
+ protected abstract createDefaultIndex(): TIndex;
447
+ getPksByKey(key: TKey): TPk[];
448
+ getPksByKeys(keys: readonly TKey[]): Map<TKey, TPk[]>;
449
+ hasKey(key: TKey): boolean;
450
+ getKeys(): readonly TKey[];
451
+ getKeySize(key: TKey): number;
452
+ get size(): number;
453
+ get isEmpty(): boolean;
454
+ getMetrics(): {
455
+ totalKeys: number;
456
+ totalPks: number;
457
+ averagePksPerKey: number;
458
+ maxBucketSize: number;
459
+ minBucketSize: number;
460
+ };
461
+ destroy(): void;
462
+ }
463
+
360
464
  type TOIMPkSelector<TEntity extends object, TPk extends TOIMPk> = (entity: TEntity) => TPk;
361
465
 
362
466
  type TOIMEntityUpdater<TEntity extends object> = (draft: Partial<TEntity>, prevEntity: TEntity) => TEntity;
@@ -423,18 +527,57 @@ declare class OIMReactiveCollection<TEntity extends object, TPk extends TOIMPk>
423
527
  constructor(queue: OIMEventQueue, opts?: TOIMCollectionOptions<TEntity, TPk>);
424
528
  }
425
529
 
426
- declare class OIMReactiveIndexManual<TKey extends TOIMPk, TPk extends TOIMPk> extends OIMReactiveIndex<TKey, TPk, OIMIndexManual<TKey, TPk>> {
530
+ declare class OIMReactiveIndexManualSetBased<TKey extends TOIMPk, TPk extends TOIMPk> extends OIMReactiveIndexSetBased<TKey, TPk, OIMIndexManualSetBased<TKey, TPk>> {
427
531
  constructor(queue: OIMEventQueue, opts?: {
428
- index: OIMIndexManual<TKey, TPk>;
532
+ index: OIMIndexManualSetBased<TKey, TPk>;
429
533
  });
430
- protected createDefaultIndex(): OIMIndexManual<TKey, TPk>;
534
+ protected createDefaultIndex(): OIMIndexManualSetBased<TKey, TPk>;
431
535
  setPks(key: TKey, pks: TPk[]): void;
432
536
  addPks(key: TKey, pks: readonly TPk[]): void;
433
537
  removePks(key: TKey, pks: readonly TPk[]): void;
434
538
  clear(key?: TKey): void;
435
539
  }
436
540
 
437
- declare class OIMRICollection<TEntity extends object, TPk extends TOIMPk, TIndexName extends string, TIndexKey extends TOIMPk, TIndex extends OIMIndex<TIndexKey, TPk>, TReactiveIndex extends OIMReactiveIndex<TIndexKey, TPk, TIndex>, TReactiveIndexMap extends Record<TIndexName, TReactiveIndex> = Record<TIndexName, TReactiveIndex>> extends OIMReactiveCollection<TEntity, TPk> {
541
+ /**
542
+ * Manual Array-based index that allows direct manipulation of key-to-primary-keys mappings.
543
+ * Extends the abstract OIMIndexArrayBased with manual control methods and optional comparison.
544
+ */
545
+ declare class OIMIndexManualArrayBased<TIndexKey extends TOIMPk, TPk extends TOIMPk> extends OIMIndexArrayBased<TIndexKey, TPk> {
546
+ constructor(options?: {
547
+ comparePks?: TOIMIndexComparator<TPk>;
548
+ store?: OIMIndexStoreArrayBased<TIndexKey, TPk>;
549
+ });
550
+ /**
551
+ * Set primary keys for a specific index key, replacing any existing values.
552
+ * Uses optional comparator to skip updates if PKs haven't actually changed.
553
+ */
554
+ setPks(key: TIndexKey, pks: TPk[]): void;
555
+ /**
556
+ * Add primary keys to a specific index key
557
+ */
558
+ addPks(key: TIndexKey, pks: readonly TPk[]): void;
559
+ /**
560
+ * Remove primary keys from a specific index key
561
+ */
562
+ removePks(key: TIndexKey, pks: readonly TPk[]): void;
563
+ /**
564
+ * Clear all primary keys for a specific index key, or all keys if no key specified
565
+ */
566
+ clear(key?: TIndexKey): void;
567
+ }
568
+
569
+ declare class OIMReactiveIndexManualArrayBased<TKey extends TOIMPk, TPk extends TOIMPk> extends OIMReactiveIndexArrayBased<TKey, TPk, OIMIndexManualArrayBased<TKey, TPk>> {
570
+ constructor(queue: OIMEventQueue, opts?: {
571
+ index: OIMIndexManualArrayBased<TKey, TPk>;
572
+ });
573
+ protected createDefaultIndex(): OIMIndexManualArrayBased<TKey, TPk>;
574
+ setPks(key: TKey, pks: TPk[]): void;
575
+ addPks(key: TKey, pks: readonly TPk[]): void;
576
+ removePks(key: TKey, pks: readonly TPk[]): void;
577
+ clear(key?: TKey): void;
578
+ }
579
+
580
+ declare class OIMRICollection<TEntity extends object, TPk extends TOIMPk, TIndexName extends string, TIndexKey extends TOIMPk, TReactiveIndex extends OIMReactiveIndexSetBased<TIndexKey, TPk, OIMIndexSetBased<TIndexKey, TPk>> | OIMReactiveIndexArrayBased<TIndexKey, TPk, OIMIndexArrayBased<TIndexKey, TPk>>, TReactiveIndexMap extends Record<TIndexName, TReactiveIndex> = Record<TIndexName, TReactiveIndex>> extends OIMReactiveCollection<TEntity, TPk> {
438
581
  readonly indexes?: TReactiveIndexMap;
439
582
  constructor(queue: OIMEventQueue, opts: {
440
583
  collectionOpts?: TOIMCollectionOptions<TEntity, TPk>;
@@ -456,6 +599,32 @@ declare class OIMCollectionStoreMapDriven<TEntity extends object, TPk extends TO
456
599
  getAllPks(): TPk[];
457
600
  }
458
601
 
602
+ declare class OIMIndexStoreMapDrivenSetBased<TKey extends TOIMPk, TPk extends TOIMPk> extends OIMIndexStoreSetBased<TKey, TPk> {
603
+ protected readonly pks: Map<TKey, Set<TPk>>;
604
+ setOneByKey(key: TKey, pks: Set<TPk>): void;
605
+ removeOneByKey(key: TKey): void;
606
+ removeManyByKeys(keys: readonly TKey[]): void;
607
+ getOneByKey(key: TKey): Set<TPk> | undefined;
608
+ getManyByKeys(keys: readonly TKey[]): Map<TKey, Set<TPk>>;
609
+ getAllKeys(): TKey[];
610
+ getAll(): Map<TKey, Set<TPk>>;
611
+ countAll(): number;
612
+ clear(): void;
613
+ }
614
+
615
+ declare class OIMIndexStoreMapDrivenArrayBased<TKey extends TOIMPk, TPk extends TOIMPk> extends OIMIndexStoreArrayBased<TKey, TPk> {
616
+ protected readonly pks: Map<TKey, TPk[]>;
617
+ setOneByKey(key: TKey, pks: TPk[]): void;
618
+ removeOneByKey(key: TKey): void;
619
+ removeManyByKeys(keys: readonly TKey[]): void;
620
+ getOneByKey(key: TKey): TPk[] | undefined;
621
+ getManyByKeys(keys: readonly TKey[]): Map<TKey, TPk[]>;
622
+ getAllKeys(): TKey[];
623
+ getAll(): Map<TKey, TPk[]>;
624
+ countAll(): number;
625
+ clear(): void;
626
+ }
627
+
459
628
  type TOIMComparator<TEntity extends object> = (a: TEntity, b: TEntity) => boolean;
460
629
 
461
630
  declare class OIMComparatorFactory<TEntity extends object> {
@@ -617,6 +786,23 @@ declare enum EOIMUpdateEventCoalescerEventType {
617
786
 
618
787
  type TOIMIndexKey = string | number | bigint;
619
788
 
789
+ /**
790
+ * Configuration options for Set-based index instances */
791
+ type TOIMIndexOptionsSetBased<TIndexKey extends TOIMPk, TPk extends TOIMPk> = {
792
+ comparePks?: TOIMIndexComparator<TPk>;
793
+ store?: OIMIndexStoreSetBased<TIndexKey, TPk>;
794
+ };
795
+ /**
796
+ * Configuration options for Array-based index instances */
797
+ type TOIMIndexOptionsArrayBased<TIndexKey extends TOIMPk, TPk extends TOIMPk> = {
798
+ comparePks?: TOIMIndexComparator<TPk>;
799
+ store?: OIMIndexStoreArrayBased<TIndexKey, TPk>;
800
+ };
801
+ /**
802
+ * @deprecated Use TOIMIndexOptionsSetBased or TOIMIndexOptionsArrayBased instead
803
+ * Configuration options for index instances */
804
+ type TOIMIndexOptions<TIndexKey extends TOIMPk, TPk extends TOIMPk> = TOIMIndexOptionsSetBased<TIndexKey, TPk>;
805
+
620
806
  type TOIMIndexPksUpdatePayload<TKey extends TOIMPk, TPk extends TOIMPk> = {
621
807
  key: TKey;
622
808
  pks: readonly TPk[];
@@ -628,4 +814,4 @@ type TOIMUpdatePayload<TKey> = {
628
814
  keys: readonly TKey[];
629
815
  };
630
816
 
631
- export { EOIMCollectionEventType, EOIMEventQueueEventType, EOIMEventQueueSchedulerEventType, EOIMIndexEventType, EOIMUpdateEventCoalescerEventType, type IOIMEventQueueSchedulerEvents, OIMCollection, OIMCollectionStore, OIMCollectionStoreMapDriven, OIMComparatorFactory, OIMDBSettings, OIMEntityUpdaterFactory, OIMEventEmitter, OIMEventQueue, OIMEventQueueScheduler, OIMEventQueueSchedulerAnimationFrame, OIMEventQueueSchedulerFactory, OIMEventQueueSchedulerImmediate, OIMEventQueueSchedulerMicrotask, OIMEventQueueSchedulerTimeout, OIMIndex, OIMIndexComparatorFactory, OIMIndexManual, OIMMap2Keys, OIMPkSelectorFactory, OIMRICollection, OIMReactiveCollection, OIMReactiveIndex, OIMReactiveIndexManual, OIMUpdateEventCoalescer, OIMUpdateEventCoalescerCollection, OIMUpdateEventCoalescerIndex, OIMUpdateEventEmitter, type TOIMCollectionOptions, type TOIMCollectionUpdatePayload, type TOIMComparator, type TOIMEntityUpdater, type TOIMEventHandler, type TOIMEventQueueOptions, type TOIMIndexComparator, type TOIMIndexKey, type TOIMIndexOptions, type TOIMIndexPksUpdatePayload, type TOIMIndexUpdatePayload, type TOIMPk, type TOIMPkSelector, type TOIMSchedulerOptions, type TOIMSchedulerType, type TOIMUpdateEventEmitterOptions, type TOIMUpdatePayload };
817
+ export { EOIMCollectionEventType, EOIMEventQueueEventType, EOIMEventQueueSchedulerEventType, EOIMIndexEventType, EOIMUpdateEventCoalescerEventType, type IOIMEventQueueSchedulerEvents, OIMCollection, OIMCollectionStore, OIMCollectionStoreMapDriven, OIMComparatorFactory, OIMDBSettings, OIMEntityUpdaterFactory, OIMEventEmitter, OIMEventQueue, OIMEventQueueScheduler, OIMEventQueueSchedulerAnimationFrame, OIMEventQueueSchedulerFactory, OIMEventQueueSchedulerImmediate, OIMEventQueueSchedulerMicrotask, OIMEventQueueSchedulerTimeout, OIMIndexArrayBased, OIMIndexComparatorFactory, OIMIndexManualArrayBased, OIMIndexManualSetBased, OIMIndexSetBased, OIMIndexStoreArrayBased, OIMIndexStoreMapDrivenArrayBased, OIMIndexStoreMapDrivenSetBased, OIMIndexStoreSetBased, OIMMap2Keys, OIMPkSelectorFactory, OIMRICollection, OIMReactiveCollection, OIMReactiveIndexArrayBased, OIMReactiveIndexManualArrayBased, OIMReactiveIndexManualSetBased, OIMReactiveIndexSetBased, OIMUpdateEventCoalescer, OIMUpdateEventCoalescerCollection, OIMUpdateEventCoalescerIndex, OIMUpdateEventEmitter, type TOIMCollectionOptions, type TOIMCollectionUpdatePayload, type TOIMComparator, type TOIMEntityUpdater, type TOIMEventHandler, type TOIMEventQueueOptions, type TOIMIndexComparator, type TOIMIndexKey, type TOIMIndexOptions, type TOIMIndexOptionsArrayBased, type TOIMIndexOptionsSetBased, type TOIMIndexPksUpdatePayload, type TOIMIndexUpdatePayload, type TOIMPk, type TOIMPkSelector, type TOIMSchedulerOptions, type TOIMSchedulerType, type TOIMUpdateEventEmitterOptions, type TOIMUpdatePayload };