@signaldb/core 1.7.0 → 2.0.0-beta.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.
Files changed (88) hide show
  1. package/dist/.vite/manifest.json +125 -82
  2. package/dist/AsyncDataAdapter.d.ts +64 -0
  3. package/dist/AutoFetchDataAdapter.d.ts +113 -0
  4. package/dist/Collection/Cursor.d.ts +9 -9
  5. package/dist/Collection/Observer.d.ts +3 -2
  6. package/dist/Collection/index.d.ts +52 -46
  7. package/dist/Collection/types.d.ts +4 -9
  8. package/dist/DataAdapter.d.ts +33 -0
  9. package/dist/DefaultDataAdapter.d.ts +32 -0
  10. package/dist/WorkerDataAdapter.d.ts +22 -0
  11. package/dist/WorkerDataAdapterHost.d.ts +59 -0
  12. package/dist/createIndex.d.ts +7 -0
  13. package/dist/createStorageAdapter.d.ts +9 -0
  14. package/dist/{Collection/getIndexInfo.d.ts → getIndexInfo.d.ts} +17 -19
  15. package/dist/index.cjs.js +21 -17
  16. package/dist/index.cjs10.js +15 -26
  17. package/dist/index.cjs11.js +13 -10
  18. package/dist/index.cjs12.js +337 -3
  19. package/dist/index.cjs13.js +384 -115
  20. package/dist/index.cjs14.js +183 -67
  21. package/dist/index.cjs15.js +339 -8
  22. package/dist/index.cjs16.js +563 -20
  23. package/dist/index.cjs17.js +16 -12
  24. package/dist/index.cjs18.js +24 -5
  25. package/dist/index.cjs2.js +27 -36
  26. package/dist/index.cjs20.js +102 -13
  27. package/dist/index.cjs21.js +124 -21
  28. package/dist/index.cjs22.js +5 -68
  29. package/dist/index.cjs23.js +22 -82
  30. package/dist/index.cjs24.js +5 -16
  31. package/dist/index.cjs25.js +8 -27
  32. package/dist/index.cjs26.js +27 -7
  33. package/dist/index.cjs27.js +3 -5
  34. package/dist/index.cjs28.js +5 -27
  35. package/dist/index.cjs29.js +27 -40
  36. package/dist/index.cjs3.js +242 -497
  37. package/dist/index.cjs30.js +42 -0
  38. package/dist/index.cjs31.js +9 -0
  39. package/dist/index.cjs4.js +3 -170
  40. package/dist/index.cjs5.js +3 -67
  41. package/dist/index.cjs6.js +27 -3
  42. package/dist/index.cjs7.js +10 -3
  43. package/dist/index.cjs8.js +3 -3
  44. package/dist/index.cjs9.js +131 -3
  45. package/dist/index.d.ts +10 -8
  46. package/dist/index.mjs +15 -11
  47. package/dist/index10.mjs +15 -26
  48. package/dist/index11.mjs +13 -10
  49. package/dist/index12.mjs +337 -3
  50. package/dist/index13.mjs +384 -115
  51. package/dist/index14.mjs +183 -66
  52. package/dist/index15.mjs +339 -8
  53. package/dist/index16.mjs +563 -20
  54. package/dist/index17.mjs +16 -12
  55. package/dist/index18.mjs +24 -5
  56. package/dist/index2.mjs +27 -36
  57. package/dist/index20.mjs +102 -13
  58. package/dist/index21.mjs +123 -21
  59. package/dist/index22.mjs +5 -67
  60. package/dist/index23.mjs +22 -81
  61. package/dist/index24.mjs +5 -16
  62. package/dist/index25.mjs +8 -27
  63. package/dist/index26.mjs +27 -7
  64. package/dist/index27.mjs +3 -5
  65. package/dist/index28.mjs +5 -27
  66. package/dist/index29.mjs +27 -40
  67. package/dist/index3.mjs +241 -497
  68. package/dist/index30.mjs +43 -0
  69. package/dist/index31.mjs +10 -0
  70. package/dist/index4.mjs +3 -170
  71. package/dist/index5.mjs +3 -66
  72. package/dist/index6.mjs +27 -3
  73. package/dist/index7.mjs +10 -3
  74. package/dist/index8.mjs +3 -3
  75. package/dist/index9.mjs +131 -3
  76. package/dist/types/IndexProvider.d.ts +12 -7
  77. package/dist/types/StorageAdapter.d.ts +20 -0
  78. package/dist/utils/objectId.d.ts +8 -0
  79. package/dist/utils/queryId.d.ts +9 -0
  80. package/package.json +2 -2
  81. package/dist/AutoFetchCollection.d.ts +0 -60
  82. package/dist/Collection/createIndex.d.ts +0 -15
  83. package/dist/ReplicatedCollection.d.ts +0 -60
  84. package/dist/createMemoryAdapter.d.ts +0 -7
  85. package/dist/persistence/combinePersistenceAdapters.d.ts +0 -32
  86. package/dist/persistence/createPersistenceAdapter.d.ts +0 -9
  87. package/dist/types/MemoryAdapter.d.ts +0 -15
  88. package/dist/types/PersistenceAdapter.d.ts +0 -20
@@ -1,44 +1,41 @@
1
- import type MemoryAdapter from '../types/MemoryAdapter';
2
1
  import type ReactivityAdapter from '../types/ReactivityAdapter';
3
- import type PersistenceAdapter from '../types/PersistenceAdapter';
4
2
  import EventEmitter from '../utils/EventEmitter';
5
3
  import type Selector from '../types/Selector';
6
4
  import type Modifier from '../types/Modifier';
7
- import type IndexProvider from '../types/IndexProvider';
5
+ import type DataAdapter from '../DataAdapter';
6
+ import type { QueryOptions } from '../DataAdapter';
7
+ import type StorageAdapter from '../types/StorageAdapter';
8
8
  import Cursor from './Cursor';
9
9
  import type { BaseItem, FindOptions, Transform } from './types';
10
10
  export type { BaseItem, Transform, SortSpecifier, FieldSpecifier, FindOptions } from './types';
11
11
  export type { CursorOptions } from './Cursor';
12
12
  export type { ObserveCallbacks } from './Observer';
13
- export { default as createIndex } from './createIndex';
13
+ export { default as createIndex } from '../createIndex';
14
14
  export interface CollectionOptions<T extends BaseItem<I>, I, U = T> {
15
+ /**
16
+ * @deprecated Use new constructor parameters instead.
17
+ */
15
18
  name?: string;
16
- memory?: MemoryAdapter;
19
+ /**
20
+ * @deprecated Use `DataAdapter` options instead.
21
+ */
22
+ persistence?: StorageAdapter<T, I>;
23
+ primaryKeyGenerator?: (item: Omit<T, 'id'>) => I;
17
24
  reactivity?: ReactivityAdapter;
18
25
  transform?: Transform<T, U>;
19
- persistence?: PersistenceAdapter<T, I>;
20
- indices?: IndexProvider<T, I>[];
26
+ indices?: string[];
21
27
  enableDebugMode?: boolean;
22
28
  fieldTracking?: boolean;
23
- primaryKeyGenerator?: (item: Omit<T, 'id'>) => I;
24
29
  }
25
30
  interface CollectionEvents<T extends BaseItem, U = T> {
26
31
  'added': (item: T) => void;
27
32
  'changed': (item: T, modifier: Modifier<T>) => void;
28
33
  'removed': (item: T) => void;
29
- 'persistence.init': () => void;
30
- 'persistence.error': (error: Error) => void;
31
- 'persistence.transmitted': () => void;
32
- 'persistence.received': () => void;
33
- 'persistence.pullStarted': () => void;
34
- 'persistence.pullCompleted': () => void;
35
- 'persistence.pushStarted': () => void;
36
- 'persistence.pushCompleted': () => void;
37
- 'observer.created': <O extends FindOptions<T>>(selector?: Selector<T>, options?: O) => void;
38
- 'observer.disposed': <O extends FindOptions<T>>(selector?: Selector<T>, options?: O) => void;
34
+ 'observer.created': <O extends QueryOptions<T>>(selector?: Selector<T>, options?: O) => void;
35
+ 'observer.disposed': <O extends QueryOptions<T>>(selector?: Selector<T>, options?: O) => void;
39
36
  'getItems': (selector: Selector<T> | undefined) => void;
40
- 'find': <O extends FindOptions<T>>(selector: Selector<T> | undefined, options: O | undefined, cursor: Cursor<T, U>) => void;
41
- 'findOne': <O extends FindOptions<T>>(selector: Selector<T>, options: O | undefined, item: U | undefined) => void;
37
+ 'find': <O extends FindOptions<T, Async>, Async extends boolean>(selector: Selector<T> | undefined, options: O | undefined, cursor: Cursor<T, U, Async>) => void;
38
+ 'findOne': <O extends QueryOptions<T>>(selector: Selector<T>, options: O | undefined, item: U | undefined) => void;
42
39
  'insert': (item: Omit<T, 'id'> & Partial<Pick<T, 'id'>>) => void;
43
40
  'updateOne': (selector: Selector<T>, modifier: Modifier<T>) => void;
44
41
  'updateMany': (selector: Selector<T>, modifier: Modifier<T>) => void;
@@ -47,8 +44,8 @@ interface CollectionEvents<T extends BaseItem, U = T> {
47
44
  'removeMany': (selector: Selector<T>) => void;
48
45
  'validate': (item: T) => void;
49
46
  '_debug.getItems': (callstack: string, selector: Selector<T> | undefined, measuredTime: number) => void;
50
- '_debug.find': <O extends FindOptions<T>>(callstack: string, selector: Selector<T> | undefined, options: O | undefined, cursor: Cursor<T, U>) => void;
51
- '_debug.findOne': <O extends FindOptions<T>>(callstack: string, selector: Selector<T>, options: O | undefined, item: U | undefined) => void;
47
+ '_debug.find': <O extends FindOptions<T, Async>, Async extends boolean>(callstack: string, selector: Selector<T> | undefined, options: O | undefined, cursor: Cursor<T, U, Async>) => void;
48
+ '_debug.findOne': <O extends FindOptions<T, Async>, Async extends boolean>(callstack: string, selector: Selector<T>, options: O | undefined, item: U | undefined) => void;
52
49
  '_debug.insert': (callstack: string, item: Omit<T, 'id'> & Partial<Pick<T, 'id'>>) => void;
53
50
  '_debug.updateOne': (callstack: string, selector: Selector<T>, modifier: Modifier<T>) => void;
54
51
  '_debug.updateMany': (callstack: string, selector: Selector<T>, modifier: Modifier<T>) => void;
@@ -88,29 +85,31 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
88
85
  * while deferring index rebuilding until all operations in the batch are completed.
89
86
  * This improves performance by avoiding repetitive index recalculations and
90
87
  * provides atomicity for the batch of operations.
88
+ * Supports both synchronous and asynchronous callbacks.
91
89
  * @param callback - The batch operation to execute.
90
+ * @returns A promise if the callback returns a promise, otherwise `void`.
92
91
  */
93
- static batch(callback: () => void): void;
92
+ static batch<ReturnType>(callback: () => Promise<ReturnType>): Promise<void>;
93
+ static batch<ReturnType>(callback: () => ReturnType): void;
94
94
  readonly name: string;
95
+ private backend;
95
96
  private options;
96
- private persistenceAdapter;
97
97
  private isPullingSignal;
98
98
  private isPushingSignal;
99
- private indexProviders;
100
- private indicesOutdated;
101
- private idIndex;
99
+ private readySignal;
102
100
  private debugMode;
103
101
  private batchOperationInProgress;
104
102
  private isDisposed;
105
103
  private postBatchCallbacks;
106
104
  private fieldTracking;
107
- private persistenceReadyPromise;
108
105
  /**
109
106
  * Initializes a new instance of the `Collection` class with optional configuration.
110
107
  * Sets up memory, persistence, reactivity, and indices as specified in the options.
111
108
  * @template T - The type of the items stored in the collection.
112
109
  * @template I - The type of the unique identifier for the items.
113
110
  * @template U - The transformed item type after applying transformations (default is T).
111
+ * @param name - The name of the collection.
112
+ * @param dataAdapter - The data adapter for creating the collection backend.
114
113
  * @param options - Optional configuration for the collection.
115
114
  * @param options.name - An optional name for the collection.
116
115
  * @param options.memory - The in-memory adapter for storing items.
@@ -122,6 +121,8 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
122
121
  * @param options.fieldTracking - A boolean to enable or disable field tracking by default.
123
122
  */
124
123
  constructor(options?: CollectionOptions<T, I, U>);
124
+ constructor(name: string, dataAdapter: DataAdapter, options?: CollectionOptions<T, I, U>);
125
+ isBatchOperationInProgress(): boolean;
125
126
  /**
126
127
  * Checks whether the collection is currently performing a pull operation
127
128
  * ⚡️ this function is reactive!
@@ -172,18 +173,19 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
172
173
  *
173
174
  * collection.insert({ name: 'Item 1' })
174
175
  */
175
- isReady(): Promise<void>;
176
+ ready(): Promise<void>;
177
+ /**
178
+ * Checks if the collection is ready.
179
+ * ⚡️ this function is reactive!
180
+ * @returns A boolean indicating whether the collection is ready.
181
+ */
182
+ isReady(): boolean;
176
183
  private profile;
177
184
  private executeInDebugMode;
178
- private rebuildIndices;
179
- private rebuildAllIndices;
180
- private getIndexInfo;
181
- private getItemAndIndex;
182
- private deleteFromIdIndex;
183
- private memory;
184
- private memoryArray;
185
185
  private transform;
186
+ private getItem;
186
187
  private getItems;
188
+ private withPushState;
187
189
  /**
188
190
  * Disposes the collection, unregisters persistence adapters, clears memory, and
189
191
  * cleans up all resources used by the collection.
@@ -198,37 +200,41 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
198
200
  * @param [options] - Options for the find operation, such as limit and sort.
199
201
  * @returns A cursor to fetch and observe the matching items.
200
202
  */
201
- find<O extends FindOptions<T>>(selector?: Selector<T>, options?: O): Cursor<T, U>;
203
+ find<Async extends boolean = false, O extends FindOptions<T, Async> = FindOptions<T, Async>>(selector?: Selector<T>, options?: O): Cursor<T, U, Async>;
202
204
  /**
203
205
  * Finds a single item in the collection based on a selector and optional options.
204
206
  * ⚡️ this function is reactive!
205
207
  * Returns the found item or undefined if no item matches.
208
+ * @template Async - Whether to perform the operation asynchronously.
206
209
  * @template O - The options type for the find operation.
207
210
  * @param selector - The criteria to select the item.
208
211
  * @param [options] - Options for the find operation, such as projection.
209
212
  * @returns The found item or `undefined`.
210
213
  */
211
- findOne<O extends Omit<FindOptions<T>, 'limit'>>(selector: Selector<T>, options?: O): NonNullable<U> | undefined;
214
+ findOne<Async extends boolean = false, O extends Omit<FindOptions<T, Async>, 'limit'> = Omit<FindOptions<T, Async>, 'limit'>>(selector: Selector<T>, options?: O): Async extends true ? Promise<U | undefined> : U | undefined;
212
215
  /**
213
216
  * Performs a batch operation, deferring index rebuilds and allowing multiple
214
217
  * modifications to be made atomically. Executes any post-batch callbacks afterwards.
215
218
  * @param callback - The batch operation to execute.
219
+ * @returns A promise if the callback returns a promise, otherwise void.
216
220
  */
217
- batch(callback: () => void): void;
221
+ batch<ReturnType>(callback: () => Promise<ReturnType>): Promise<void>;
222
+ batch<ReturnType>(callback: () => ReturnType): void;
223
+ onPostBatch(callback: () => void): void;
218
224
  /**
219
225
  * Inserts a single item into the collection. Generates a unique ID if not provided.
220
226
  * @param item - The item to insert.
221
227
  * @returns The ID of the inserted item.
222
228
  * @throws {Error} If the collection is disposed or the item has an invalid ID.
223
229
  */
224
- insert(item: Omit<T, 'id'> & Partial<Pick<T, 'id'>>): I;
230
+ insert(item: Omit<T, 'id'> & Partial<Pick<T, 'id'>>): Promise<I>;
225
231
  /**
226
232
  * Inserts multiple items into the collection. Generates unique IDs for items if not provided.
227
233
  * @param items - The items to insert.
228
234
  * @returns An array of IDs of the inserted items.
229
235
  * @throws {Error} If the collection is disposed or the items are invalid.
230
236
  */
231
- insertMany(items: Array<Omit<T, 'id'> & Partial<Pick<T, 'id'>>>): I[];
237
+ insertMany(items: Array<Omit<T, 'id'> & Partial<Pick<T, 'id'>>>): Promise<I[]>;
232
238
  /**
233
239
  * Updates a single item in the collection that matches the given selector.
234
240
  * @param selector - The criteria to select the item to update.
@@ -240,7 +246,7 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
240
246
  */
241
247
  updateOne(selector: Selector<T>, modifier: Modifier<T>, options?: {
242
248
  upsert?: boolean;
243
- }): 0 | 1;
249
+ }): Promise<number>;
244
250
  /**
245
251
  * Updates multiple items in the collection that match the given selector.
246
252
  * @param selector - The criteria to select the items to update.
@@ -252,7 +258,7 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
252
258
  */
253
259
  updateMany(selector: Selector<T>, modifier: Modifier<T>, options?: {
254
260
  upsert?: boolean;
255
- }): number;
261
+ }): Promise<number>;
256
262
  /**
257
263
  * Replaces a single item in the collection that matches the given selector.
258
264
  * @param selector - The criteria to select the item to replace.
@@ -264,19 +270,19 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
264
270
  */
265
271
  replaceOne(selector: Selector<T>, replacement: Omit<T, 'id'> & Partial<Pick<T, 'id'>>, options?: {
266
272
  upsert?: boolean;
267
- }): 0 | 1;
273
+ }): Promise<number>;
268
274
  /**
269
275
  * Removes a single item from the collection that matches the given selector.
270
276
  * @param selector - The criteria to select the item to remove.
271
277
  * @returns The number of items removed (0 or 1).
272
278
  * @throws {Error} If the collection is disposed or invalid arguments are provided.
273
279
  */
274
- removeOne(selector: Selector<T>): 0 | 1;
280
+ removeOne(selector: Selector<T>): Promise<number>;
275
281
  /**
276
282
  * Removes multiple items from the collection that match the given selector.
277
283
  * @param selector - The criteria to select the items to remove.
278
284
  * @returns The number of items removed.
279
285
  * @throws {Error} If the collection is disposed or invalid arguments are provided.
280
286
  */
281
- removeMany(selector: Selector<T>): number;
287
+ removeMany(selector: Selector<T>): Promise<number>;
282
288
  }
@@ -1,3 +1,4 @@
1
+ import type { QueryOptions } from '../DataAdapter';
1
2
  import type ReactivityAdapter from '../types/ReactivityAdapter';
2
3
  export type BaseItem<I = any> = {
3
4
  id: I;
@@ -9,17 +10,11 @@ export type SortSpecifier<T> = {
9
10
  export type FieldSpecifier<T> = {
10
11
  [P in keyof T]?: 0 | 1;
11
12
  } & Record<string, 0 | 1>;
12
- export interface FindOptions<T extends BaseItem> {
13
- /** Sort order (default: natural order) */
14
- sort?: SortSpecifier<T> | undefined;
15
- /** Number of results to skip at the beginning */
16
- skip?: number | undefined;
17
- /** Maximum number of results to return */
18
- limit?: number | undefined;
19
- /** Dictionary of fields to return or exclude. */
20
- fields?: FieldSpecifier<T> | undefined;
13
+ export interface FindOptions<T extends BaseItem, Async extends boolean> extends QueryOptions<T> {
21
14
  /** pass `false` to disable reactivity */
22
15
  reactive?: ReactivityAdapter | false;
23
16
  /** pass `true` to enable automatic field-level reactitivy */
24
17
  fieldTracking?: boolean;
18
+ /** pass `true` to execute the query asynchronously */
19
+ async?: Async;
25
20
  }
@@ -0,0 +1,33 @@
1
+ import type { BaseItem, FieldSpecifier, SortSpecifier } from './Collection';
2
+ import type Collection from './Collection';
3
+ import type Modifier from './types/Modifier';
4
+ import type Selector from './types/Selector';
5
+ export interface QueryOptions<T extends BaseItem> {
6
+ /** Sort order (default: natural order) */
7
+ sort?: SortSpecifier<T> | undefined;
8
+ /** Number of results to skip at the beginning */
9
+ skip?: number | undefined;
10
+ /** Maximum number of results to return */
11
+ limit?: number | undefined;
12
+ /** Dictionary of fields to return or exclude. */
13
+ fields?: FieldSpecifier<T> | undefined;
14
+ }
15
+ export interface CollectionBackend<T extends BaseItem<I>, I> {
16
+ insert(item: T): Promise<T>;
17
+ updateOne(selector: Selector<T>, modifier: Modifier<T>): Promise<T[]>;
18
+ updateMany(selector: Selector<T>, modifier: Modifier<T>): Promise<T[]>;
19
+ replaceOne(selector: Selector<T>, replacement: Omit<T, 'id'> & Partial<Pick<T, 'id'>>): Promise<T[]>;
20
+ removeOne(selector: Selector<T>): Promise<T[]>;
21
+ removeMany(selector: Selector<T>): Promise<T[]>;
22
+ registerQuery<O extends QueryOptions<T>>(selector: Selector<T>, options?: O): void;
23
+ unregisterQuery<O extends QueryOptions<T>>(selector: Selector<T>, options?: O): void;
24
+ getQueryState<O extends QueryOptions<T>>(selector: Selector<T>, options?: O): 'active' | 'complete' | 'error';
25
+ getQueryError<O extends QueryOptions<T>>(selector: Selector<T>, options?: O): Error | null;
26
+ getQueryResult<O extends QueryOptions<T>>(selector: Selector<T>, options?: O): T[];
27
+ onQueryStateChange<O extends QueryOptions<T>>(selector: Selector<T> | undefined, options: O | undefined, callback: (state: 'active' | 'complete' | 'error') => void): () => void;
28
+ dispose(): Promise<void>;
29
+ isReady(): Promise<void>;
30
+ }
31
+ export default interface DataAdapter {
32
+ createCollectionBackend<T extends BaseItem<I>, I = any, U = T>(collection: Collection<T, I, U>, indices: string[]): CollectionBackend<T, I>;
33
+ }
@@ -0,0 +1,32 @@
1
+ import type { BaseItem } from './Collection';
2
+ import type Collection from './Collection';
3
+ import type DataAdapter from './DataAdapter';
4
+ import type { CollectionBackend } from './DataAdapter';
5
+ import type StorageAdapter from './types/StorageAdapter';
6
+ interface DefaultDataAdapterOptions {
7
+ storage?: (name: string) => StorageAdapter<any, any> | undefined;
8
+ onError?: (name: string, error: Error) => void;
9
+ }
10
+ export default class DefaultDataAdapter implements DataAdapter {
11
+ private items;
12
+ private options;
13
+ private storageAdapters;
14
+ private indices;
15
+ private activeQueries;
16
+ private queryEmitters;
17
+ private queuedQueryUpdates;
18
+ private cachedQueryResults;
19
+ constructor(options?: DefaultDataAdapterOptions);
20
+ private ensureStorageAdapter;
21
+ private rebuildIndices;
22
+ private setupStorageAdapter;
23
+ private getIndexInfo;
24
+ private applyIndexDeltas;
25
+ private getItem;
26
+ private queryItems;
27
+ private executeQuery;
28
+ private flushQueuedQueryUpdates;
29
+ private updateQueries;
30
+ createCollectionBackend<T extends BaseItem<I>, I = any, U = T>(collection: Collection<T, I, U>, indices: string[]): CollectionBackend<T, I>;
31
+ }
32
+ export {};
@@ -0,0 +1,22 @@
1
+ import type { BaseItem } from './Collection';
2
+ import type Collection from './Collection';
3
+ import type DataAdapter from './DataAdapter';
4
+ import type { CollectionBackend } from './DataAdapter';
5
+ interface WorkerDataAdapterOptions {
6
+ id?: string;
7
+ }
8
+ export default class WorkerDataAdapter implements DataAdapter {
9
+ private worker;
10
+ private options;
11
+ private id;
12
+ private isDisposed;
13
+ private workerReady;
14
+ private collectionReady;
15
+ private queries;
16
+ constructor(worker: Worker, options: WorkerDataAdapterOptions);
17
+ private exec;
18
+ private queryListeners;
19
+ private updateQuery;
20
+ createCollectionBackend<T extends BaseItem<I>, I = any, U = T>(collection: Collection<T, I, U>, indices?: string[]): CollectionBackend<T, I>;
21
+ }
22
+ export {};
@@ -0,0 +1,59 @@
1
+ import type { BaseItem } from './Collection';
2
+ import type { QueryOptions } from './DataAdapter';
3
+ import type Modifier from './types/Modifier';
4
+ import type StorageAdapter from './types/StorageAdapter';
5
+ import type Selector from './types/Selector';
6
+ interface WorkerContext {
7
+ addEventListener: (type: 'message', listener: (event: MessageEvent) => any) => void;
8
+ postMessage: (message: any) => void;
9
+ }
10
+ interface WorkerDataAdapterHostOptions {
11
+ id?: string;
12
+ storage: (name: string) => StorageAdapter<any, any>;
13
+ onError?: (error: Error) => void;
14
+ }
15
+ type CollectionMethods<T extends BaseItem<I>, I = any> = {
16
+ registerCollection: (collectionName: string, indices: string[]) => Promise<void>;
17
+ unregisterCollection: (collectionName: string) => Promise<void>;
18
+ registerQuery: <O extends QueryOptions<T>>(collectionName: string, selector: Selector<T>, options?: O) => Promise<void>;
19
+ unregisterQuery: <O extends QueryOptions<T>>(collectionName: string, selector: Selector<T>, options?: O) => Promise<void>;
20
+ insert: (collectionName: string, item: T) => Promise<T>;
21
+ updateOne: (collectionName: string, selector: Selector<T>, modifier: Modifier<T>) => Promise<T[]>;
22
+ updateMany: (collectionName: string, selector: Selector<T>, modifier: Modifier<T>) => Promise<T[]>;
23
+ replaceOne: (collectionName: string, selector: Selector<T>, replacement: Omit<T, 'id'> & Partial<Pick<T, 'id'>>) => Promise<T[]>;
24
+ removeOne: (collectionName: string, selector: Selector<T>) => Promise<T[]>;
25
+ removeMany: (collectionName: string, selector: Selector<T>) => Promise<T[]>;
26
+ isReady: (collectionName: string) => Promise<void>;
27
+ };
28
+ export default class WorkerDataAdapterHost<T extends BaseItem<I>, I = any> {
29
+ private workerContext;
30
+ private options;
31
+ private id;
32
+ private storageAdapters;
33
+ private storageAdapterReady;
34
+ private collectionIndices;
35
+ private queries;
36
+ private onError;
37
+ constructor(workerContext: WorkerContext, options: WorkerDataAdapterHostOptions);
38
+ private respond;
39
+ private handleMessage;
40
+ private getIndexInfo;
41
+ private queryItems;
42
+ private executeQuery;
43
+ private ensureQuery;
44
+ private emitQueryUpdate;
45
+ private ensureStorageAdapter;
46
+ private checkQueryUpdates;
47
+ protected registerCollection: CollectionMethods<T, I>['registerCollection'];
48
+ protected unregisterCollection: CollectionMethods<T, I>['unregisterCollection'];
49
+ protected registerQuery: CollectionMethods<T, I>['registerQuery'];
50
+ protected unregisterQuery: CollectionMethods<T, I>['unregisterQuery'];
51
+ protected insert: CollectionMethods<T, I>['insert'];
52
+ protected updateOne: CollectionMethods<T, I>['updateOne'];
53
+ protected updateMany: CollectionMethods<T, I>['updateMany'];
54
+ protected replaceOne: CollectionMethods<T, I>['replaceOne'];
55
+ protected removeOne: CollectionMethods<T, I>['removeOne'];
56
+ protected removeMany: CollectionMethods<T, I>['removeMany'];
57
+ protected isReady: CollectionMethods<T, I>['isReady'];
58
+ }
59
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { BaseItem } from './Collection/types';
2
+ /**
3
+ * creates an index for a specific field
4
+ * @param field name of the field
5
+ * @returns an index provider to pass to the `indices` option of the collection constructor
6
+ */
7
+ export default function createIndex<T extends BaseItem<I> = BaseItem, I = any>(field: string): import("./types/IndexProvider").default<T, I>;
@@ -0,0 +1,9 @@
1
+ import type StorageAdapter from './types/StorageAdapter';
2
+ /**
3
+ * Creates an StorageAdapter based on the given definition.
4
+ * @param definition - The definition of the StorageAdapter.
5
+ * @returns The created StorageAdapter.
6
+ */
7
+ export default function createStorageAdapter<T extends {
8
+ id: I;
9
+ } & Record<string, any>, I>(definition: StorageAdapter<T, I>): StorageAdapter<T, I>;
@@ -1,41 +1,39 @@
1
- import type IndexProvider from '../types/IndexProvider';
2
- import type { FlatSelector } from '../types/Selector';
3
- import type Selector from '../types/Selector';
4
- import type { BaseItem } from './types';
1
+ import type { AsynchronousQueryFunction, SynchronousQueryFunction } from './types/IndexProvider';
2
+ import type { FlatSelector } from './types/Selector';
3
+ import type Selector from './types/Selector';
4
+ import type { BaseItem } from './Collection/types';
5
+ type IndexInfo<T extends BaseItem<I> = BaseItem, I = any> = {
6
+ matched: boolean;
7
+ ids: I[];
8
+ optimizedSelector: FlatSelector<T>;
9
+ };
5
10
  /**
6
11
  * Retrieves merged index information for a given flat selector by querying multiple
7
- * index providers. Combines results from all index providers to determine matched positions
12
+ * index providers. Combines results from all index providers to determine matched ids
8
13
  * and an optimized selector.
9
14
  * @template T - The type of the items in the collection.
10
15
  * @template I - The type of the unique identifier for the items.
11
- * @param indexProviders - An array of index providers to query.
16
+ * @param queryFunctions - An array of index providers to query.
12
17
  * @param selector - The flat selector used to filter items.
13
18
  * @returns An object containing:
14
19
  * - `matched`: A boolean indicating if the selector matched any items.
15
- * - `positions`: An array of matched item positions.
20
+ * - `ids`: An array of matched item ids.
16
21
  * - `optimizedSelector`: A flat selector optimized based on the index results.
17
22
  */
18
- export declare function getMergedIndexInfo<T extends BaseItem<I> = BaseItem, I = any>(indexProviders: IndexProvider<T, I>[], selector: FlatSelector<T>): {
19
- matched: boolean;
20
- positions: number[];
21
- optimizedSelector: FlatSelector<T>;
22
- };
23
+ export declare function getMergedIndexInfo<T extends BaseItem<I> = BaseItem, I = any>(queryFunctions: (SynchronousQueryFunction<T, I> | AsynchronousQueryFunction<T, I>)[], selector: FlatSelector<T>): IndexInfo<T, I> | Promise<IndexInfo<T, I>>;
23
24
  /**
24
25
  * Retrieves index information for a given complex selector by querying multiple
25
26
  * index providers. Handles nested `$and` and `$or` conditions in the selector and
26
27
  * optimizes the selector to minimize processing overhead.
27
28
  * @template T - The type of the items in the collection.
28
29
  * @template I - The type of the unique identifier for the items.
29
- * @param indexProviders - An array of index providers to query.
30
+ * @param queryFunctions - An array of index providers to query.
30
31
  * @param selector - The complex selector used to filter items.
31
32
  * @returns An object containing:
32
33
  * - `matched`: A boolean indicating if the selector matched any items.
33
- * - `positions`: An array of matched item positions.
34
+ * - `ids`: An array of matched item ids.
34
35
  * - `optimizedSelector`: A selector optimized based on the index results, with unused
35
36
  * conditions removed.
36
37
  */
37
- export default function getIndexInfo<T extends BaseItem<I> = BaseItem, I = any>(indexProviders: IndexProvider<T, I>[], selector: Selector<T>): {
38
- matched: boolean;
39
- positions: number[];
40
- optimizedSelector: Selector<T>;
41
- };
38
+ export default function getIndexInfo<QueryFunction extends SynchronousQueryFunction<T, I> | AsynchronousQueryFunction<T, I>, T extends BaseItem<I> = BaseItem, I = any>(queryFunctions: QueryFunction[], selector: Selector<T>): QueryFunction extends AsynchronousQueryFunction<T, I> ? Promise<IndexInfo<T, I>> : IndexInfo<T, I>;
39
+ export {};
package/dist/index.cjs.js CHANGED
@@ -2,27 +2,31 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const Cursor = require("./index.cjs2.js");
4
4
  const index = require("./index.cjs3.js");
5
- const AutoFetchCollection = require("./index.cjs4.js");
6
- const combinePersistenceAdapters = require("./index.cjs5.js");
7
- const createIndexProvider = require("./index.cjs6.js");
8
- const createMemoryAdapter = require("./index.cjs7.js");
9
- const createPersistenceAdapter = require("./index.cjs8.js");
10
- const createReactivityAdapter = require("./index.cjs9.js");
11
- const isEqual = require("./index.cjs10.js");
12
- const modify = require("./index.cjs11.js");
13
- const randomId = require("./index.cjs12.js");
14
- const EventEmitter = require("./index.cjs13.js");
15
- const createIndex = require("./index.cjs14.js");
5
+ const createStorageAdapter = require("./index.cjs4.js");
6
+ const createReactivityAdapter = require("./index.cjs5.js");
7
+ const isEqual = require("./index.cjs6.js");
8
+ const modify = require("./index.cjs7.js");
9
+ const randomId = require("./index.cjs8.js");
10
+ const EventEmitter = require("./index.cjs9.js");
11
+ const get = require("./index.cjs10.js");
12
+ const serializeValue = require("./index.cjs11.js");
13
+ const DefaultDataAdapter = require("./index.cjs12.js");
14
+ const AsyncDataAdapter = require("./index.cjs13.js");
15
+ const WorkerDataAdapter = require("./index.cjs14.js");
16
+ const WorkerDataAdapterHost = require("./index.cjs15.js");
17
+ const AutoFetchDataAdapter = require("./index.cjs16.js");
16
18
  exports.Cursor = Cursor.default;
17
19
  exports.Collection = index.default;
18
- exports.AutoFetchCollection = AutoFetchCollection;
19
- exports.combinePersistenceAdapters = combinePersistenceAdapters.default;
20
- exports.createIndexProvider = createIndexProvider;
21
- exports.createMemoryAdapter = createMemoryAdapter;
22
- exports.createPersistenceAdapter = createPersistenceAdapter;
20
+ exports.createStorageAdapter = createStorageAdapter;
23
21
  exports.createReactivityAdapter = createReactivityAdapter;
24
22
  exports.isEqual = isEqual;
25
23
  exports.modify = modify;
26
24
  exports.randomId = randomId;
27
25
  exports.EventEmitter = EventEmitter;
28
- exports.createIndex = createIndex.default;
26
+ exports.get = get;
27
+ exports.serializeValue = serializeValue;
28
+ exports.DefaultDataAdapter = DefaultDataAdapter;
29
+ exports.AsyncDataAdapter = AsyncDataAdapter;
30
+ exports.WorkerDataAdapter = WorkerDataAdapter;
31
+ exports.WorkerDataAdapterHost = WorkerDataAdapterHost;
32
+ exports.AutoFetchDataAdapter = AutoFetchDataAdapter;
@@ -1,29 +1,18 @@
1
1
  "use strict";
2
- function isEqual(a, b) {
3
- if (Object.is(a, b))
4
- return true;
5
- if (a instanceof RegExp && b instanceof RegExp)
6
- return a.toString() === b.toString();
7
- if (a instanceof Date && b instanceof Date)
8
- return a.getTime() === b.getTime();
9
- if (typeof a !== "object")
10
- return false;
11
- if (typeof b !== "object")
12
- return false;
13
- if (a === null)
14
- return false;
15
- if (b === null)
16
- return false;
17
- const aKeys = Object.keys(a);
18
- const bKeys = Object.keys(b);
19
- if (aKeys.length !== bKeys.length)
20
- return false;
21
- for (const key of aKeys) {
22
- if (!bKeys.includes(key))
23
- return false;
24
- if (!isEqual(a[key], b[key]))
25
- return false;
2
+ function get(value, path) {
3
+ const normalized = path.replaceAll(/\[(\w+)\]/g, ".$1");
4
+ if (normalized.includes("..") || normalized.startsWith(".") || normalized.endsWith(".")) {
5
+ return;
26
6
  }
27
- return true;
7
+ const segments = normalized.split(".");
8
+ let current = value;
9
+ for (const key of segments) {
10
+ if (current == null)
11
+ return;
12
+ current = current[key];
13
+ }
14
+ if (current === void 0)
15
+ return;
16
+ return current;
28
17
  }
29
- module.exports = isEqual;
18
+ module.exports = get;
@@ -1,12 +1,15 @@
1
1
  "use strict";
2
- const mingo = require("mingo");
3
- const deepClone = require("./index.cjs19.js");
4
- function modify(item, modifier) {
5
- const hasOperators = Object.keys(modifier).some((key) => key.startsWith("$"));
6
- if (!hasOperators)
7
- return modifier;
8
- const clonedItem = deepClone.default(item);
9
- mingo.update(clonedItem, modifier);
10
- return clonedItem;
2
+ function serializeValue(value) {
3
+ if (value == null)
4
+ return null;
5
+ if (typeof value === "string")
6
+ return value;
7
+ if (typeof value === "number")
8
+ return value.toString();
9
+ if (typeof value === "boolean")
10
+ return value.toString();
11
+ if (value instanceof Date)
12
+ return value.toISOString();
13
+ return JSON.stringify(value);
11
14
  }
12
- module.exports = modify;
15
+ module.exports = serializeValue;