import type MemoryAdapter from '../types/MemoryAdapter'; import type ReactivityAdapter from '../types/ReactivityAdapter'; import type PersistenceAdapter from '../types/PersistenceAdapter'; import EventEmitter from '../utils/EventEmitter'; import type Selector from '../types/Selector'; import type Modifier from '../types/Modifier'; import type IndexProvider from '../types/IndexProvider'; import Cursor from './Cursor'; import type { BaseItem, FindOptions, Transform, TransformAll } from './types'; export type { BaseItem, Transform, TransformAll, SortSpecifier, FieldSpecifier, FindOptions } from './types'; export type { CursorOptions } from './Cursor'; export type { ObserveCallbacks } from './Observer'; export { default as createIndex } from './createIndex'; export interface CollectionOptions, I, E extends BaseItem = T, U = E> { name?: string; memory?: MemoryAdapter; reactivity?: ReactivityAdapter; transform?: Transform; persistence?: PersistenceAdapter; indices?: IndexProvider[]; enableDebugMode?: boolean; fieldTracking?: boolean; transformAll?: TransformAll; primaryKeyGenerator?: (item: Omit) => I; } interface CollectionEvents { 'added': (item: T) => void; 'changed': (item: T, modifier: Modifier) => void; 'removed': (item: T) => void; 'persistence.init': () => void; 'persistence.error': (error: Error) => void; 'persistence.transmitted': () => void; 'persistence.received': () => void; 'persistence.pullStarted': () => void; 'persistence.pullCompleted': () => void; 'persistence.pushStarted': () => void; 'persistence.pushCompleted': () => void; 'observer.created': >(selector?: Selector, options?: O) => void; 'observer.disposed': >(selector?: Selector, options?: O) => void; 'getItems': (selector: Selector | undefined) => void; 'find': >(selector: Selector | undefined, options: O | undefined, cursor: Cursor) => void; 'findOne': >(selector: Selector, options: O | undefined, item: U | undefined) => void; 'insert': (item: Omit & Partial>) => void; 'updateOne': (selector: Selector, modifier: Modifier) => void; 'updateMany': (selector: Selector, modifier: Modifier) => void; 'replaceOne': (selector: Selector, item: Omit & Partial>) => void; 'removeOne': (selector: Selector) => void; 'removeMany': (selector: Selector) => void; 'validate': (item: T) => void; '_debug.getItems': (callstack: string, selector: Selector | undefined, measuredTime: number) => void; '_debug.find': >(callstack: string, selector: Selector | undefined, options: O | undefined, cursor: Cursor) => void; '_debug.findOne': >(callstack: string, selector: Selector, options: O | undefined, item: U | undefined) => void; '_debug.insert': (callstack: string, item: Omit & Partial>) => void; '_debug.updateOne': (callstack: string, selector: Selector, modifier: Modifier) => void; '_debug.updateMany': (callstack: string, selector: Selector, modifier: Modifier) => void; '_debug.replaceOne': (callstack: string, selector: Selector, item: Omit & Partial>) => void; '_debug.removeOne': (callstack: string, selector: Selector) => void; '_debug.removeMany': (callstack: string, selector: Selector) => void; } /** * Represents a collection of data items with support for in-memory operations, * persistence, reactivity, and event-based notifications. The collection provides * CRUD operations, observer patterns, and batch operations. * @template T - The type of the items stored in the collection. * @template I - The type of the unique identifier for the items. * @template U - The transformed item type after applying transformations (default is T). */ export default class Collection = BaseItem, I = any, E extends BaseItem = T, U = E> extends EventEmitter> { private static collections; private static debugMode; private static batchOperationInProgress; private static fieldTracking; private static onCreationCallbacks; private static onDisposeCallbacks; static getCollections(): Collection[]; static onCreation(callback: (collection: Collection) => void): void; static onDispose(callback: (collection: Collection) => void): void; /** * Enables debug mode for all collections. */ static enableDebugMode: () => void; /** * Enables field tracking for all collections. * @param enable - A boolean indicating whether to enable field tracking. */ static setFieldTracking: (enable: boolean) => void; /** * Executes a batch operation, allowing multiple modifications to the collection * while deferring index rebuilding until all operations in the batch are completed. * This improves performance by avoiding repetitive index recalculations and * provides atomicity for the batch of operations. * @param callback - The batch operation to execute. */ static batch(callback: () => void): void; readonly name: string; private options; private persistenceAdapter; private isPullingSignal; private isPushingSignal; private indexProviders; private indicesOutdated; private idIndex; private debugMode; private batchOperationInProgress; private isDisposed; private postBatchCallbacks; private fieldTracking; private persistenceReadyPromise; private pendingUpdates; /** * Initializes a new instance of the `Collection` class with optional configuration. * Sets up memory, persistence, reactivity, and indices as specified in the options. * @template T - The type of the items stored in the collection. * @template I - The type of the unique identifier for the items. * @template U - The transformed item type after applying transformations (default is T). * @param options - Optional configuration for the collection. * @param options.name - An optional name for the collection. * @param options.memory - The in-memory adapter for storing items. * @param options.reactivity - The reactivity adapter for observing changes in the collection. * @param options.transform - A transformation function to apply to items when retrieving them. * @param options.persistence - The persistence adapter for saving and loading items. * @param options.indices - An array of index providers for optimized querying. * @param options.enableDebugMode - A boolean to enable or disable debug mode. * @param options.fieldTracking - A boolean to enable or disable field tracking by default. * @param options.transformAll - A function that will be able to solve the n+1 problem */ constructor(options?: CollectionOptions); /** * Resets the collection's data by clearing the in-memory items and reloading from the persistence adapter. * @returns A promise that resolves when the data has been reset and reloaded. */ resetData(): Promise; /** * Loads data from the persistence adapter and updates the in-memory collection accordingly. * @param data - Optional data to load, containing either a full list of items or a set of changes. If not provided, data will be loaded from the persistence adapter. * @param hasOngoingSaves - A boolean indicating whether there are ongoing save operations. If `true`, the method will skip loading data to avoid conflicts with pending updates. * @returns A promise that resolves when the data has been loaded and the in-memory collection has been updated. */ private loadPersistentData; /** * Checks whether the collection is currently performing a pull operation * ⚡️ this function is reactive! * (loading data from the persistence adapter). * @returns A boolean indicating if the collection is in the process of pulling data. */ isPulling(): boolean; /** * Checks whether the collection is currently performing a push operation * ⚡️ this function is reactive! * (saving data to the persistence adapter). * @returns A boolean indicating if the collection is in the process of pushing data. */ isPushing(): boolean; /** * Checks whether the collection is currently performing either a pull or push operation, * ⚡️ this function is reactive! * indicating that it is loading or saving data. * @returns A boolean indicating if the collection is in the process of loading or saving data. */ isLoading(): boolean; /** * Retrieves the current debug mode status of the collection. * @returns A boolean indicating whether debug mode is enabled for the collection. */ getDebugMode(): boolean; /** * Enables or disables debug mode for the collection. * When debug mode is enabled, additional debugging information and events are emitted. * @param enable - A boolean indicating whether to enable (`true`) or disable (`false`) debug mode. */ setDebugMode(enable: boolean): void; /** * Enables or disables field tracking for the collection. * @param enable - A boolean indicating whether to enable (`true`) or disable (`false`) field tracking. */ setFieldTracking(enable: boolean): void; /** * Resolves when the persistence adapter finished initializing * and the collection is ready to be used. * @returns A promise that resolves when the collection is ready. * @example * ```ts * const collection = new Collection({ * persistence: // ... * }) * await collection.isReady() * * collection.insert({ name: 'Item 1' }) */ isReady(): Promise; private profile; private executeInDebugMode; private rebuildIndices; private rebuildAllIndices; private getIndexInfo; private getItemAndIndex; private deleteFromIdIndex; private memory; private memoryArray; private transform; private transformAll; private getItems; /** * Disposes the collection, unregisters persistence adapters, clears memory, and * cleans up all resources used by the collection. * @returns A promise that resolves when the collection is disposed. */ dispose(): Promise; /** * Finds multiple items in the collection based on a selector and optional options. * Returns a cursor for reactive data queries. * @template O - The options type for the find operation. * @param [selector] - The criteria to select items. * @param [options] - Options for the find operation, such as limit and sort. * @returns A cursor to fetch and observe the matching items. */ find>(selector?: Selector, options?: O): Cursor; /** * Finds a single item in the collection based on a selector and optional options. * ⚡️ this function is reactive! * Returns the found item or undefined if no item matches. * @template O - The options type for the find operation. * @param selector - The criteria to select the item. * @param [options] - Options for the find operation, such as projection. * @returns The found item or `undefined`. */ findOne, 'limit'>>(selector: Selector, options?: O): NonNullable | undefined; /** * Performs a batch operation, deferring index rebuilds and allowing multiple * modifications to be made atomically. Executes any post-batch callbacks afterwards. * @param callback - The batch operation to execute. */ batch(callback: () => void): void; /** * Inserts a single item into the collection. Generates a unique ID if not provided. * @param item - The item to insert. * @returns The ID of the inserted item. * @throws {Error} If the collection is disposed or the item has an invalid ID. */ insert(item: Omit & Partial>): I; /** * Inserts multiple items into the collection. Generates unique IDs for items if not provided. * @param items - The items to insert. * @returns An array of IDs of the inserted items. * @throws {Error} If the collection is disposed or the items are invalid. */ insertMany(items: Array & Partial>>): I[]; /** * Updates a single item in the collection that matches the given selector. * @param selector - The criteria to select the item to update. * @param modifier - The modifications to apply to the item. * @param [options] - Optional settings for the update operation. * @param [options.upsert] - If `true`, creates a new item if no item matches the selector. * @returns The number of items updated (0 or 1). * @throws {Error} If the collection is disposed or invalid arguments are provided. */ updateOne(selector: Selector, modifier: Modifier, options?: { upsert?: boolean; }): 0 | 1; /** * Updates multiple items in the collection that match the given selector. * @param selector - The criteria to select the items to update. * @param modifier - The modifications to apply to the items. * @param [options] - Optional settings for the update operation. * @param [options.upsert] - If `true`, creates new items if no items match the selector. * @returns The number of items updated. * @throws {Error} If the collection is disposed or invalid arguments are provided. */ updateMany(selector: Selector, modifier: Modifier, options?: { upsert?: boolean; }): number; /** * Replaces a single item in the collection that matches the given selector. * @param selector - The criteria to select the item to replace. * @param replacement - The item to replace the selected item with. * @param [options] - Optional settings for the replace operation. * @param [options.upsert] - If `true`, creates a new item if no item matches the selector. * @returns The number of items replaced (0 or 1). * @throws {Error} If the collection is disposed or invalid arguments are provided. */ replaceOne(selector: Selector, replacement: Omit & Partial>, options?: { upsert?: boolean; }): 0 | 1; /** * Removes a single item from the collection that matches the given selector. * @param selector - The criteria to select the item to remove. * @returns The number of items removed (0 or 1). * @throws {Error} If the collection is disposed or invalid arguments are provided. */ removeOne(selector: Selector): 0 | 1; /** * Removes multiple items from the collection that match the given selector. * @param selector - The criteria to select the items to remove. * @returns The number of items removed. * @throws {Error} If the collection is disposed or invalid arguments are provided. */ removeMany(selector: Selector): number; }