@signaldb/sync 2.0.0-beta.5 → 2.0.0-beta.7

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 (57) hide show
  1. package/dist/core/src/AsyncDataAdapter.d.ts +64 -0
  2. package/dist/core/src/AutoFetchDataAdapter.d.ts +112 -0
  3. package/dist/core/src/Collection/Cursor.d.ts +113 -0
  4. package/dist/core/src/Collection/Observer.d.ts +64 -0
  5. package/dist/core/src/Collection/index.d.ts +294 -0
  6. package/dist/core/src/Collection/types.d.ts +28 -0
  7. package/dist/core/src/DataAdapter.d.ts +35 -0
  8. package/dist/core/src/DefaultDataAdapter.d.ts +35 -0
  9. package/dist/core/src/WorkerDataAdapter.d.ts +25 -0
  10. package/dist/core/src/WorkerDataAdapterHost.d.ts +62 -0
  11. package/dist/core/src/createIndex.d.ts +7 -0
  12. package/dist/core/src/createIndexProvider.d.ts +8 -0
  13. package/dist/core/src/createReactivityAdapter.d.ts +8 -0
  14. package/dist/core/src/createStorageAdapter.d.ts +9 -0
  15. package/dist/core/src/getIndexInfo.d.ts +39 -0
  16. package/dist/core/src/index.d.ts +22 -0
  17. package/dist/core/src/types/Dependency.d.ts +4 -0
  18. package/dist/core/src/types/IndexProvider.d.ts +26 -0
  19. package/dist/core/src/types/Modifier.d.ts +46 -0
  20. package/dist/core/src/types/ReactivityAdapter.d.ts +6 -0
  21. package/dist/core/src/types/Selector.d.ts +46 -0
  22. package/dist/core/src/types/Signal.d.ts +4 -0
  23. package/dist/core/src/types/StorageAdapter.d.ts +20 -0
  24. package/dist/core/src/utils/EventEmitter.d.ts +71 -0
  25. package/dist/core/src/utils/batchOnNextTick.d.ts +16 -0
  26. package/dist/core/src/utils/compact.d.ts +9 -0
  27. package/dist/core/src/utils/createSignal.d.ts +14 -0
  28. package/dist/core/src/utils/deepClone.d.ts +17 -0
  29. package/dist/core/src/utils/get.d.ts +9 -0
  30. package/dist/core/src/utils/getMatchingKeys.d.ts +19 -0
  31. package/dist/core/src/utils/intersection.d.ts +9 -0
  32. package/dist/core/src/utils/isEqual.d.ts +14 -0
  33. package/dist/core/src/utils/isFieldExpression.d.ts +11 -0
  34. package/dist/core/src/utils/match.d.ts +12 -0
  35. package/dist/core/src/utils/modify.d.ts +14 -0
  36. package/dist/core/src/utils/project.d.ts +15 -0
  37. package/dist/core/src/utils/queryId.d.ts +9 -0
  38. package/dist/core/src/utils/randomId.d.ts +7 -0
  39. package/dist/core/src/utils/reactiveOrAsync.d.ts +59 -0
  40. package/dist/core/src/utils/serializeValue.d.ts +12 -0
  41. package/dist/core/src/utils/set.d.ts +13 -0
  42. package/dist/core/src/utils/sortItems.d.ts +12 -0
  43. package/dist/core/src/utils/uniqueBy.d.ts +10 -0
  44. package/dist/index.mjs +532 -589
  45. package/dist/index.mjs.map +1 -1
  46. package/dist/index.umd.js +3 -4
  47. package/dist/index.umd.js.map +1 -1
  48. package/package.json +2 -2
  49. /package/dist/{SyncManager.d.ts → sync/src/SyncManager.d.ts} +0 -0
  50. /package/dist/{applyChanges.d.ts → sync/src/applyChanges.d.ts} +0 -0
  51. /package/dist/{computeChanges.d.ts → sync/src/computeChanges.d.ts} +0 -0
  52. /package/dist/{getSnapshot.d.ts → sync/src/getSnapshot.d.ts} +0 -0
  53. /package/dist/{index.d.ts → sync/src/index.d.ts} +0 -0
  54. /package/dist/{sync.d.ts → sync/src/sync.d.ts} +0 -0
  55. /package/dist/{types.d.ts → sync/src/types.d.ts} +0 -0
  56. /package/dist/{utils → sync/src/utils}/PromiseQueue.d.ts +0 -0
  57. /package/dist/{utils → sync/src/utils}/debounce.d.ts +0 -0
@@ -0,0 +1,28 @@
1
+ import type { QueryOptions } from '../DataAdapter';
2
+ import type ReactivityAdapter from '../types/ReactivityAdapter';
3
+ export type BaseItem<I = any> = {
4
+ id: I;
5
+ } & Record<string, any>;
6
+ export type Transform<T, U = T> = ((document: T) => U) | null | undefined;
7
+ export type TransformAll<T extends BaseItem, O extends BaseItem = T> = ((items: T[], fields: FieldSpecifier<O> | undefined) => O[]) | null | undefined;
8
+ export type SortSpecifier<T> = {
9
+ [P in keyof T]?: -1 | 1;
10
+ } & Record<string, -1 | 1>;
11
+ export type FieldSpecifier<T> = {
12
+ [P in keyof T]?: 0 | 1;
13
+ } & Record<string, 0 | 1>;
14
+ export interface FindOptions<T extends BaseItem, Async extends boolean> extends QueryOptions<T> {
15
+ /** pass `false` to disable reactivity */
16
+ reactive?: ReactivityAdapter | false;
17
+ /** pass `true` to enable automatic field-level reactitivy */
18
+ fieldTracking?: boolean;
19
+ /** pass `true` to execute the query asynchronously */
20
+ async?: Async;
21
+ }
22
+ export type AsyncFindOptions<T extends BaseItem> = Omit<FindOptions<T, true>, 'async'> & {
23
+ async: true;
24
+ };
25
+ export type SyncFindOptions<T extends BaseItem> = Omit<FindOptions<T, false>, 'async'> & {
26
+ async?: false;
27
+ };
28
+ export type AnyFindOptions<T extends BaseItem> = AsyncFindOptions<T> | SyncFindOptions<T>;
@@ -0,0 +1,35 @@
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 type StateChangeCallback = (state: 'active' | 'complete' | 'error') => void;
16
+ export interface CollectionBackend<T extends BaseItem<I>, I> {
17
+ insert(item: T): Promise<T>;
18
+ updateOne(selector: Selector<T>, modifier: Modifier<T>): Promise<T[]>;
19
+ updateMany(selector: Selector<T>, modifier: Modifier<T>): Promise<T[]>;
20
+ replaceOne(selector: Selector<T>, replacement: Omit<T, 'id'> & Partial<Pick<T, 'id'>>): Promise<T[]>;
21
+ removeOne(selector: Selector<T>): Promise<T[]>;
22
+ removeMany(selector: Selector<T>): Promise<T[]>;
23
+ registerQuery<O extends QueryOptions<T>>(selector: Selector<T>, options: O): void;
24
+ unregisterQuery<O extends QueryOptions<T>>(selector: Selector<T>, options: O): void;
25
+ getQueryState<O extends QueryOptions<T>>(selector: Selector<T>, options: O): 'active' | 'complete' | 'error';
26
+ getQueryError<O extends QueryOptions<T>>(selector: Selector<T>, options: O): Error | null;
27
+ getQueryResult<O extends QueryOptions<T>>(selector: Selector<T>, options: O): T[];
28
+ executeQuery<O extends QueryOptions<T>>(selector: Selector<T>, options: O): Promise<T[]>;
29
+ onQueryStateChange<O extends QueryOptions<T>>(selector: Selector<T>, options: O, callback: StateChangeCallback): () => void;
30
+ dispose(): Promise<void>;
31
+ isReady(): Promise<void>;
32
+ }
33
+ export default interface DataAdapter {
34
+ createCollectionBackend<T extends BaseItem<I>, I = any, E extends BaseItem = T, U = E>(collection: Collection<T, I, E, U>, indices: string[]): CollectionBackend<T, I>;
35
+ }
@@ -0,0 +1,35 @@
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 collections;
15
+ private indices;
16
+ private activeQueries;
17
+ private queryEmitters;
18
+ private queuedQueryUpdates;
19
+ private cachedQueryResults;
20
+ constructor(options?: DefaultDataAdapterOptions);
21
+ private ensureStorageAdapter;
22
+ private rebuildIndices;
23
+ private setupStorageAdapter;
24
+ private getIndexInfo;
25
+ private applyIndexDeltas;
26
+ private getItem;
27
+ private queryItems;
28
+ private executeQuery;
29
+ private flushQueuedQueryUpdates;
30
+ private executeAndCacheQuery;
31
+ private updateQueries;
32
+ createCollectionBackend<T extends BaseItem<I>, I = any, E extends BaseItem = T, U = E>(collection: Collection<T, I, E, U>, indices: string[]): CollectionBackend<T, I>;
33
+ fetchItemsFromStorage<T extends BaseItem<I>, I = any, E extends BaseItem = T, U = E>(collection?: Collection<T, I, E, U>): Promise<void>;
34
+ }
35
+ export {};
@@ -0,0 +1,25 @@
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
+ log?: (message: string, ...args: any[]) => void;
8
+ }
9
+ export default class WorkerDataAdapter implements DataAdapter {
10
+ private worker;
11
+ private options;
12
+ private id;
13
+ private isDisposed;
14
+ private workerReady;
15
+ private log;
16
+ private collectionReady;
17
+ private batchExecutionHelpers;
18
+ private queries;
19
+ constructor(worker: Worker, options: WorkerDataAdapterOptions);
20
+ private exec;
21
+ private enqueueBatched;
22
+ private updateQuery;
23
+ createCollectionBackend<T extends BaseItem<I>, I = any, E extends BaseItem = T, U = E>(collection: Collection<T, I, E, U>, indices: string[]): CollectionBackend<T, I>;
24
+ }
25
+ export {};
@@ -0,0 +1,62 @@
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
+ log?: (message: string, ...args: any[]) => void;
15
+ }
16
+ type CollectionMethods<T extends BaseItem<I>, I = any> = {
17
+ registerCollection: (collectionName: string, indices: string[]) => Promise<void>;
18
+ unregisterCollection: (collectionName: string) => Promise<void>;
19
+ registerQuery: <O extends QueryOptions<T>>(collectionName: string, selector: Selector<T>, options?: O) => Promise<void>;
20
+ unregisterQuery: <O extends QueryOptions<T>>(collectionName: string, selector: Selector<T>, options?: O) => Promise<void>;
21
+ executeQuery: <O extends QueryOptions<T>>(collectionName: string, selector: Selector<T>, options?: O) => Promise<T[]>;
22
+ insert: (collectionName: string, items: [T][]) => Promise<(T | Error)[]>;
23
+ updateOne: (collectionName: string, args: [Selector<T>, Modifier<T>][]) => Promise<(T[] | Error)[]>;
24
+ updateMany: (collectionName: string, args: [Selector<T>, Modifier<T>][]) => Promise<(T[] | Error)[]>;
25
+ replaceOne: (collectionName: string, args: [Selector<T>, Omit<T, 'id'> & Partial<Pick<T, 'id'>>][]) => Promise<(T[] | Error)[]>;
26
+ removeOne: (collectionName: string, selectors: [Selector<T>][]) => Promise<(T[] | Error)[]>;
27
+ removeMany: (collectionName: string, selectors: [Selector<T>][]) => Promise<(T[] | Error)[]>;
28
+ isReady: (collectionName: string) => Promise<void>;
29
+ };
30
+ export default class WorkerDataAdapterHost<T extends BaseItem<I>, I = any> {
31
+ private workerContext;
32
+ private options;
33
+ private id;
34
+ private log;
35
+ private storageAdapters;
36
+ private storageAdapterReady;
37
+ private collectionIndices;
38
+ private queries;
39
+ private onError;
40
+ constructor(workerContext: WorkerContext, options: WorkerDataAdapterHostOptions);
41
+ private respond;
42
+ private handleMessage;
43
+ private getIndexInfo;
44
+ private queryItems;
45
+ private executeQuery;
46
+ private ensureQuery;
47
+ private emitQueryUpdate;
48
+ private ensureStorageAdapter;
49
+ private checkQueryUpdates;
50
+ protected registerCollection: CollectionMethods<T, I>['registerCollection'];
51
+ protected unregisterCollection: CollectionMethods<T, I>['unregisterCollection'];
52
+ protected registerQuery: CollectionMethods<T, I>['registerQuery'];
53
+ protected unregisterQuery: CollectionMethods<T, I>['unregisterQuery'];
54
+ protected insert: CollectionMethods<T, I>['insert'];
55
+ protected updateOne: CollectionMethods<T, I>['updateOne'];
56
+ protected updateMany: CollectionMethods<T, I>['updateMany'];
57
+ protected replaceOne: CollectionMethods<T, I>['replaceOne'];
58
+ protected removeOne: CollectionMethods<T, I>['removeOne'];
59
+ protected removeMany: CollectionMethods<T, I>['removeMany'];
60
+ protected isReady: CollectionMethods<T, I>['isReady'];
61
+ }
62
+ 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,8 @@
1
+ import type { BaseItem } from './Collection';
2
+ import type IndexProvider from './types/IndexProvider';
3
+ /**
4
+ * Creates an IndexProvider based on the given definition.
5
+ * @param definition - The definition of the IndexProvider.
6
+ * @returns The created IndexProvider.
7
+ */
8
+ export default function createIndexProvider<T extends BaseItem<I> = BaseItem, I = any>(definition: IndexProvider<T, I>): IndexProvider<T, I>;
@@ -0,0 +1,8 @@
1
+ import type Dependency from './types/Dependency';
2
+ import type ReactivityAdapter from './types/ReactivityAdapter';
3
+ /**
4
+ * Creates an ReactivityAdapter based on the given definition.
5
+ * @param definition - The definition of the ReactivityAdapter.
6
+ * @returns The created ReactivityAdapter.
7
+ */
8
+ export default function createReactivityAdapter<T extends Dependency = Dependency>(definition: ReactivityAdapter<T>): ReactivityAdapter<T>;
@@ -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>;
@@ -0,0 +1,39 @@
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
+ };
10
+ /**
11
+ * Retrieves merged index information for a given flat selector by querying multiple
12
+ * index providers. Combines results from all index providers to determine matched ids
13
+ * and an optimized selector.
14
+ * @template T - The type of the items in the collection.
15
+ * @template I - The type of the unique identifier for the items.
16
+ * @param queryFunctions - An array of index providers to query.
17
+ * @param selector - The flat selector used to filter items.
18
+ * @returns An object containing:
19
+ * - `matched`: A boolean indicating if the selector matched any items.
20
+ * - `ids`: An array of matched item ids.
21
+ * - `optimizedSelector`: A flat selector optimized based on the index results.
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>>;
24
+ /**
25
+ * Retrieves index information for a given complex selector by querying multiple
26
+ * index providers. Handles nested `$and` and `$or` conditions in the selector and
27
+ * optimizes the selector to minimize processing overhead.
28
+ * @template T - The type of the items in the collection.
29
+ * @template I - The type of the unique identifier for the items.
30
+ * @param queryFunctions - An array of index providers to query.
31
+ * @param selector - The complex selector used to filter items.
32
+ * @returns An object containing:
33
+ * - `matched`: A boolean indicating if the selector matched any items.
34
+ * - `ids`: An array of matched item ids.
35
+ * - `optimizedSelector`: A selector optimized based on the index results, with unused
36
+ * conditions removed.
37
+ */
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 {};
@@ -0,0 +1,22 @@
1
+ export type { default as ReactivityAdapter } from './types/ReactivityAdapter';
2
+ export type { default as StorageAdapter, Changeset, } from './types/StorageAdapter';
3
+ export type { default as Selector } from './types/Selector';
4
+ export type { default as Modifier } from './types/Modifier';
5
+ export type { BaseItem, ObserveCallbacks, CursorOptions, Transform, TransformAll, SortSpecifier, FieldSpecifier, AnyFindOptions, AsyncFindOptions, FindOptions, SyncFindOptions, CollectionOptions, } from './Collection';
6
+ export type { default as DataAdapter } from './DataAdapter';
7
+ export { default as Cursor } from './Collection/Cursor';
8
+ export { default as Collection } from './Collection';
9
+ export { default as createStorageAdapter } from './createStorageAdapter';
10
+ export { default as createReactivityAdapter } from './createReactivityAdapter';
11
+ export { default as isEqual } from './utils/isEqual';
12
+ export { default as modify } from './utils/modify';
13
+ export { default as randomId } from './utils/randomId';
14
+ export { default as EventEmitter } from './utils/EventEmitter';
15
+ export { default as get } from './utils/get';
16
+ export { default as serializeValue } from './utils/serializeValue';
17
+ export { default as reactiveOrAsync, unwrap } from './utils/reactiveOrAsync';
18
+ export { default as DefaultDataAdapter } from './DefaultDataAdapter';
19
+ export { default as AsyncDataAdapter } from './AsyncDataAdapter';
20
+ export { default as WorkerDataAdapter } from './WorkerDataAdapter';
21
+ export { default as WorkerDataAdapterHost } from './WorkerDataAdapterHost';
22
+ export { default as AutoFetchDataAdapter } from './AutoFetchDataAdapter';
@@ -0,0 +1,4 @@
1
+ export default interface Dependency {
2
+ depend(): void;
3
+ notify(): void;
4
+ }
@@ -0,0 +1,26 @@
1
+ import type { BaseItem } from '../Collection';
2
+ import type { FlatSelector } from './Selector';
3
+ export type IndexResult<IdType> = {
4
+ ids: IdType[];
5
+ fields: string[];
6
+ keepSelector?: boolean;
7
+ matched: true;
8
+ } | {
9
+ ids?: never;
10
+ fields?: never;
11
+ keepSelector?: never;
12
+ matched: false;
13
+ };
14
+ export type SynchronousQueryFunction<T extends BaseItem<I> = BaseItem, I = any> = (selector: FlatSelector<T>) => IndexResult<I>;
15
+ export type AsynchronousQueryFunction<T extends BaseItem<I> = BaseItem, I = any> = (selector: FlatSelector<T>) => Promise<IndexResult<I>>;
16
+ interface IndexProvider<T extends BaseItem<I> = BaseItem, I = any> {
17
+ query: SynchronousQueryFunction<T, I>;
18
+ rebuild(items: T[]): void;
19
+ insert(items: T[]): void;
20
+ remove(items: T[]): void;
21
+ update(pairs: {
22
+ oldItem: T;
23
+ newItem: T;
24
+ }[]): void;
25
+ }
26
+ export default IndexProvider;
@@ -0,0 +1,46 @@
1
+ import type { DotNotation, GetType } from './Selector';
2
+ type Dictionary<T> = Record<string, T>;
3
+ type PartialMapTo<T, M> = Partial<Record<DotNotation<T>, M>> & Dictionary<M>;
4
+ type OnlyElementsOfArrays<T> = T extends any[] ? Partial<T[0]> : never;
5
+ type ElementsOf<T> = {
6
+ [P in DotNotation<T>]?: OnlyElementsOfArrays<GetType<T, P>>;
7
+ };
8
+ type PushModifier<T> = {
9
+ [P in DotNotation<T>]?: OnlyElementsOfArrays<GetType<T, P>> | {
10
+ $each?: GetType<T, P> | undefined;
11
+ $position?: number | undefined;
12
+ $slice?: number | undefined;
13
+ $sort?: 1 | -1 | Dictionary<number> | undefined;
14
+ };
15
+ };
16
+ type ArraysOrEach<T> = {
17
+ [P in DotNotation<T>]?: OnlyElementsOfArrays<GetType<T, P>> | {
18
+ $each: GetType<T, P>;
19
+ };
20
+ };
21
+ type CurrentDateModifier = {
22
+ $type: 'timestamp' | 'date';
23
+ } | true;
24
+ type Modifier<T extends Dictionary<any> = Dictionary<any>> = {
25
+ $currentDate?: (Partial<Record<DotNotation<T>, CurrentDateModifier>> & Dictionary<CurrentDateModifier>) | undefined;
26
+ $inc?: (PartialMapTo<T, number> & Dictionary<number>) | undefined;
27
+ $min?: (PartialMapTo<T, Date | number> & Dictionary<Date | number>) | undefined;
28
+ $max?: (PartialMapTo<T, Date | number> & Dictionary<Date | number>) | undefined;
29
+ $mul?: (PartialMapTo<T, number> & Dictionary<number>) | undefined;
30
+ $rename?: (PartialMapTo<T, string> & Dictionary<string>) | undefined;
31
+ $set?: ({
32
+ [P in DotNotation<T>]?: GetType<T, P>;
33
+ } & Dictionary<any>) | undefined;
34
+ $setOnInsert?: ({
35
+ [P in DotNotation<T>]?: GetType<T, P>;
36
+ } & Dictionary<any>) | undefined;
37
+ $unset?: (PartialMapTo<T, string | boolean | 1 | 0> & Dictionary<any>) | undefined;
38
+ $addToSet?: (ArraysOrEach<T> & Dictionary<any>) | undefined;
39
+ $push?: (PushModifier<T> & Dictionary<any>) | undefined;
40
+ $pull?: (ElementsOf<T> & Dictionary<any>) | undefined;
41
+ $pullAll?: ({
42
+ [P in DotNotation<T>]?: GetType<T, P>;
43
+ } & Dictionary<any>) | undefined;
44
+ $pop?: (PartialMapTo<T, 1 | -1> & Dictionary<1 | -1>) | undefined;
45
+ };
46
+ export default Modifier;
@@ -0,0 +1,6 @@
1
+ import type Dependency from './Dependency';
2
+ export default interface ReactivityAdapter<T extends Dependency = Dependency> {
3
+ create(): T;
4
+ onDispose?(callback: () => void, Dependency: T): void;
5
+ isInScope?(): boolean;
6
+ }
@@ -0,0 +1,46 @@
1
+ export interface FieldExpression<T> {
2
+ $eq?: T;
3
+ $gt?: T;
4
+ $gte?: T;
5
+ $lt?: T;
6
+ $lte?: T;
7
+ $in?: T[] | undefined;
8
+ $nin?: T[];
9
+ $ne?: T;
10
+ $exists?: boolean;
11
+ $not?: FieldExpression<T>;
12
+ $expr?: FieldExpression<T>;
13
+ $jsonSchema?: any;
14
+ $mod?: number[];
15
+ $regex?: RegExp | string;
16
+ $options?: string;
17
+ $where?: string | ((this: T) => boolean);
18
+ $all?: T[];
19
+ $elemMatch?: T extends object ? Query<T> : FieldExpression<T>;
20
+ $size?: number;
21
+ $bitsAllClear?: any;
22
+ $bitsAllSet?: any;
23
+ $bitsAnyClear?: any;
24
+ $bitsAnySet?: any;
25
+ }
26
+ export type DotNotation<T> = {
27
+ [K in keyof T & string]: T[K] extends Array<infer U> ? `${K}` | `${K}.$` | `${K}.${DotNotation<U>}` : T[K] extends object ? `${K}` | `${K}.${DotNotation<T[K]>}` : `${K}`;
28
+ }[keyof T & string];
29
+ export type GetType<T, P extends string> = P extends `${infer H}.${infer R}` ? H extends keyof T ? T[H] extends Array<infer U> ? GetType<U, R> : GetType<T[H], R> : H extends '$' ? T extends Array<infer U> ? GetType<U, R> : never : never : P extends keyof T ? T[P] : never;
30
+ type FlatQuery<T> = {
31
+ [P in DotNotation<T>]?: FlatQueryValue<T, P>;
32
+ };
33
+ type FieldValue<U> = U extends string ? string | RegExp | FieldExpression<string> : U | FieldExpression<U>;
34
+ type FlatQueryValue<T, P extends string> = GetType<T, P> extends never ? never : GetType<T, P> extends Array<infer U> ? FieldValue<U> | FieldValue<U[]> : FieldValue<GetType<T, P>>;
35
+ type Query<T> = FlatQuery<T> & {
36
+ $or?: Query<T>[];
37
+ $and?: Query<T>[];
38
+ $nor?: Query<T>[];
39
+ };
40
+ export type FlatSelector<T extends Record<string, any>> = FlatQuery<T> & {
41
+ $or?: never;
42
+ $and?: never;
43
+ $nor?: never;
44
+ };
45
+ type Selector<T extends Record<string, any>> = Query<T>;
46
+ export default Selector;
@@ -0,0 +1,4 @@
1
+ export default interface Signal<T> {
2
+ get(): T;
3
+ set(value: T): void;
4
+ }
@@ -0,0 +1,20 @@
1
+ export interface Changeset<T> {
2
+ added: T[];
3
+ modified: T[];
4
+ removed: T[];
5
+ }
6
+ export default interface StorageAdapter<T extends {
7
+ id: I;
8
+ } & Record<string, any>, I> {
9
+ setup(): Promise<void>;
10
+ teardown(): Promise<void>;
11
+ readAll(): Promise<T[]>;
12
+ readIds(positions: I[]): Promise<T[]>;
13
+ createIndex(field: string): Promise<void>;
14
+ dropIndex(field: string): Promise<void>;
15
+ readIndex(field: string): Promise<Map<any, Set<I>>>;
16
+ insert(items: T[]): Promise<void>;
17
+ replace(items: T[]): Promise<void>;
18
+ remove(items: T[]): Promise<void>;
19
+ removeAll(): Promise<void>;
20
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * A strongly‑typed EventEmitter.
3
+ */
4
+ export default class EventEmitter<Events extends Record<string | symbol, any>> {
5
+ private _maxListeners;
6
+ /**
7
+ * We store a set of the listeners for each event.
8
+ */
9
+ private _listenerStore;
10
+ setMaxListeners(max: number): this;
11
+ /**
12
+ * Subscribe to an event with a listener function.
13
+ * @param eventName The event name (key of E).
14
+ * @param listener A function that receives the emitted arguments.
15
+ * @returns The emitter instance (for chaining).
16
+ */
17
+ on<K extends keyof Events>(eventName: K, listener: Events[K]): this;
18
+ /**
19
+ * Subscribe to an event with a listener function.
20
+ * @param eventName The event name (key of E).
21
+ * @param listener A function that receives the emitted arguments.
22
+ * @returns The emitter instance (for chaining).
23
+ */
24
+ addListener<K extends keyof Events>(eventName: K, listener: Events[K]): this;
25
+ /**
26
+ * Subscribe to an event, handling it only once. Automatically removes
27
+ * the listener after it fires the first time.
28
+ * @param eventName The event name (key of E).
29
+ * @param listener A function that receives the emitted arguments.
30
+ * @returns The emitter instance (for chaining).
31
+ */
32
+ once<K extends keyof Events>(eventName: K, listener: Events[K]): this;
33
+ /**
34
+ * Unsubscribe a previously subscribed listener.
35
+ * @param eventName The event name (key of E).
36
+ * @param listener The original function passed to `on` or `once`.
37
+ * @returns The emitter instance (for chaining).
38
+ */
39
+ off<K extends keyof Events>(eventName: K, listener: Events[K]): this;
40
+ /**
41
+ * Unsubscribe a previously subscribed listener.
42
+ * @param eventName The event name (key of E).
43
+ * @param listener The original function passed to `on` or `once`.
44
+ * @returns The emitter instance (for chaining).
45
+ */
46
+ removeListener<K extends keyof Events>(eventName: K, listener: Events[K]): this;
47
+ /**
48
+ * Emit (dispatch) an event with a variable number of arguments.
49
+ * @param eventName The event name (key of E).
50
+ * @param args The arguments to pass to subscribed listeners.
51
+ */
52
+ emit<K extends keyof Events>(eventName: K, ...args: Parameters<Events[K]>): void;
53
+ /**
54
+ * Returns the array of listener functions currently registered for a given event.
55
+ * @param eventName The event name (key of E).
56
+ * @returns An array of listener functions.
57
+ */
58
+ listeners<K extends keyof Events>(eventName: K): Array<(...args: Parameters<Events[K]>) => void>;
59
+ /**
60
+ * Returns the number of listeners for a given event.
61
+ * @param eventName The event name (key of E).
62
+ * @returns The number of listeners.
63
+ */
64
+ listenerCount<K extends keyof Events>(eventName: K): number;
65
+ /**
66
+ * Removes all listeners for a given event, or all events if none is specified.
67
+ * @param eventName Optional. If omitted, clears all events’ listeners.
68
+ * @returns The emitter instance (for chaining).
69
+ */
70
+ removeAllListeners<K extends keyof Events>(eventName?: K): this;
71
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Groups multiple calls by key and flushes them on the next tick (macrotask).
3
+ * @param onFlush - Function that will be called with the key and all queued items when flushing.
4
+ * @returns An object with `enqueue` and `flush` methods.
5
+ * @example
6
+ * const batcher = batchOnNextTick<string>(async (key, items) => {
7
+ * // items is an array of { args, resolve, reject }
8
+ * // do something once with all args...
9
+ * })
10
+ *
11
+ * batcher.enqueue("my-key", [arg1, arg2])
12
+ */
13
+ export default function batchOnNextTick<TKey>(onFlush: (key: TKey, items: any[][]) => Promise<any[]>): {
14
+ enqueue: (key: TKey, args: any[]) => Promise<any>;
15
+ flush: (key: TKey) => Promise<void>;
16
+ };
@@ -0,0 +1,9 @@
1
+ type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T;
2
+ /**
3
+ * Filters out falsy values (`false`, `''`, `0`, `null`, `undefined`) from an array.
4
+ * @template T - The type of the elements in the array.
5
+ * @param array - The array to filter.
6
+ * @returns A new array containing only the truthy values from the input array.
7
+ */
8
+ export default function compact<T>(array: T[]): Truthy<T>[];
9
+ export {};
@@ -0,0 +1,14 @@
1
+ import type ReactivityAdapter from '../types/ReactivityAdapter';
2
+ import type Signal from '../types/Signal';
3
+ /**
4
+ * Creates a reactive signal for managing state and triggering dependencies.
5
+ * The signal holds a value and provides methods to get and set the value,
6
+ * with optional equality checks and dependency tracking.
7
+ * @template T - The type of the value held by the signal.
8
+ * @param reactivityAdapter - An optional reactivity adapter for managing dependencies.
9
+ * @param initialValue - The initial value of the signal.
10
+ * @param isEqual - A custom equality function to determine if the new value is different
11
+ * from the current value (default is `Object.is`).
12
+ * @returns A signal object with `get` and `set` methods to manage the value.
13
+ */
14
+ export default function createSignal<T>(reactivityAdapter: ReactivityAdapter | undefined, initialValue: T, isEqual?: (a: T, b: T) => boolean): Signal<T>;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Performs a deep clone of a value, supporting various types including arrays, objects,
3
+ * Maps, Sets, Dates, and RegExps. Functions are not supported and will throw an error.
4
+ * @template T - The type of the value to clone.
5
+ * @param value - The value to deep clone.
6
+ * @returns A deep copy of the provided value.
7
+ * @throws {Error} An error if the value is a function, as cloning functions is not supported.
8
+ */
9
+ export declare function clone<T>(value: T): T;
10
+ /**
11
+ * Creates a deep clone of an object. Uses the `structuredClone` function if available,
12
+ * otherwise falls back to a manual deep clone implementation.
13
+ * @template T - The type of the object to clone.
14
+ * @param object - The object to deep clone.
15
+ * @returns A deep copy of the provided object.
16
+ */
17
+ export default function deepClone<T>(object: T): T;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Retrieves the value at a specified path within an object.
3
+ * Supports dot and bracket notation for navigating nested properties.
4
+ * @template T - The type of the object to retrieve the value from.
5
+ * @param value - The object to navigate.
6
+ * @param path - The path (dot or bracket notation) to the desired value.
7
+ * @returns The value at the specified path, or `undefined` if the path does not exist.
8
+ */
9
+ export default function get<T extends Record<string, any>>(value: T, path: string): any;
@@ -0,0 +1,19 @@
1
+ import type { BaseItem } from '../Collection/types';
2
+ import type { FlatSelector } from '../types/Selector';
3
+ type KeyResult = {
4
+ include: (string | null)[] | null;
5
+ exclude: (string | null)[] | null;
6
+ };
7
+ /**
8
+ * Extracts the matching and excluded keys for a given field in a selector.
9
+ * Supports serialized values and `$in`/`$nin` field expressions for optimization.
10
+ * Returns `null` for include/exclude if the field cannot be optimized.
11
+ * @template T - The type of the items in the selector.
12
+ * @template I - The type of the unique identifier for the items.
13
+ * @param field - The name of the field to extract matching keys for.
14
+ * @param selector - The selector object containing query criteria.
15
+ * @returns An object containing arrays of serialized included and excluded keys,
16
+ * or `null` if the field cannot be optimized.
17
+ */
18
+ export default function getMatchingKeys<T extends BaseItem<I> = BaseItem, I = any>(field: string, selector: FlatSelector<T>): KeyResult;
19
+ export {};
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Computes the intersection of multiple arrays, returning an array of unique elements
3
+ * that are present in all the input arrays.
4
+ * @template T - The type of elements in the arrays.
5
+ * @param arrays - A variable number of arrays to compute the intersection of.
6
+ * @returns An array containing the unique elements found in all the input arrays.
7
+ * - If no arrays are provided, returns an empty array.
8
+ */
9
+ export default function intersection<T>(...arrays: T[][]): T[];
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Compares two values for deep equality.
3
+ * @param a - The first value to compare.
4
+ * @param b - The second value to compare.
5
+ * @returns - Returns `true` if the two values are deeply equal, otherwise `false`.
6
+ * @example
7
+ * isEqual({ a: 1 }, { a: 1 }); // true
8
+ * isEqual([1, 2], [1, 2]); // true
9
+ * isEqual(new Date(0), new Date(0)); // true
10
+ * isEqual(/abc/, /abc/); // true
11
+ * isEqual({ a: 1 }, { a: 2 }); // false
12
+ * isEqual(null, null); // true
13
+ */
14
+ export default function isEqual<T, K>(a: T, b: K): boolean;