UNPKG

@just-in/core

Version:

A TypeScript-first framework for building adaptive digital health interventions.

117 lines 5.54 kB
import { EventEmitter } from 'events'; import { CollectionChangeType } from './data-manager.type'; import { DBType } from './data-manager.constants'; import { Readable } from 'stream'; /** * Manages database operations and collection change listeners. */ declare class DataManager extends EventEmitter { protected static instance: DataManager | null; private db; private changeListenerManager; private isInitialized; private initializedAt; private constructor(); /** * Retrieves the singleton instance of DataManager. * @returns {DataManager} The singleton instance. */ static getInstance(): DataManager; /** * Deletes the singleton instance of DataManager. */ protected static killInstance(): void; /** * Initializes the DataManager with the specified database type. * Sets up listeners for collection changes. * @param {DBType} dbType - The type of database to initialize. Defaults to MongoDB. * @returns {Promise<void>} Resolves when initialization is complete. */ init(dbType?: DBType): Promise<void>; /** * Handles `insert` changes in the EVENTS_QUEUE collection. * Emits an `eventAdded` signal to notify external systems. * @param {any} data - Data from the change stream. * @private */ private handleEventsQueueInsert; /** * Checks if DataManager is initialized. * @returns {boolean} Initialization status. */ getInitializationStatus(): boolean; /** * Closes the DataManager and removes all listeners. * @returns {Promise<void>} Resolves when closed. */ close(): Promise<void>; checkInitialization(): void; /** * Adds an item to a specified collection, ensuring the collection exists. * Emits a specific event (e.g., `userAdded`) if applicable to the collection. * @param {string} collectionName - The name of the collection to which the item will be added. * @param {object} item - The item to add to the collection. * @returns {Promise<object | null>} Resolves with the added item, or `null` if an error occurs. */ addItemToCollection(collectionName: string, item: object): Promise<object | null>; /** * Updates an item in a collection by ID and emits an event. * @param {string} collectionName - The name of the collection. * @param {string} id - The ID of the item to update. * @param {object} updateObject - The update data. * @returns {Promise<object | null>} Resolves with the updated item or `null` on error. */ updateItemByIdInCollection(collectionName: string, id: string, updateObject: object): Promise<object | null>; /** * Removes an item from a collection by ID and emits an event. * @param {string} collectionName - The name of the collection. * @param {string} id - The ID of the item to remove. * @returns {Promise<boolean>} Resolves with `true` if removed, `false` on error. */ removeItemFromCollection(collectionName: string, id: string): Promise<boolean>; /** * Retrieves all items from a collection. * @param {string} collectionName - The name of the collection. * @returns {Promise<T[] | null>} Resolves with items or `null` on error. */ getAllInCollection<T>(collectionName: string): Promise<T[] | null>; /** * Clears all items in a collection. * @param {string} collectionName - The name of the collection. * @returns {Promise<void>} Resolves when the collection is cleared. */ clearCollection(collectionName: string): Promise<void>; /** * Checks if a collection is empty. * @param {string} collectionName - The name of the collection. * @returns {Promise<boolean>} Resolves with `true` if empty, `false` on error. */ isCollectionEmpty(collectionName: string): Promise<boolean>; /** * Finds an item by ID in a specified collection. * @template T - The expected type of the item in the collection. * @param {string} collectionName - The name of the collection. * @param {string} id - The ID of the item to find. * @returns {Promise<T | null>} Resolves with the found item of type `T` or `null` if not found or on error. */ findItemByIdInCollection<T>(collectionName: string, id: string): Promise<T | null>; /** * Finds items by criteria in a specified collection. * @template T - The expected type of the item in the collection. * @param {string} collectionName - The name of the collection. * @param {object} criteria - An object containing the key-value pair to search for. An empty object will return all items. * If `null`, it will return `null`. * @returns {Promise<T[] | null>} Resolves with the found item of type `T` or `null` if not found or on error. */ findItemsInCollection<T>(collectionName: string, criteria: Record<string, any>): Promise<T[] | null>; /** * Provides a change stream for a specific collection and change type. * This method is used by the ChangeListenerManager to abstract away database-specific logic. * @param {string} collectionName - The name of the collection to monitor. * @param {CollectionChangeType} changeType - The type of change to monitor. * @returns {Readable} A readable stream of change events. */ getChangeStream(collectionName: string, changeType: CollectionChangeType): Readable; } export default DataManager; //# sourceMappingURL=data-manager.d.ts.map