UNPKG

@push.rocks/smartdata

Version:

An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.

133 lines (132 loc) 4.61 kB
import * as plugins from './plugins.js'; import { SmartdataDb } from './classes.db.js'; import { SmartdataDbCursor } from './classes.cursor.js'; import { SmartDataDbDoc, type IIndexOptions } from './classes.doc.js'; import { SmartdataDbWatcher } from './classes.watcher.js'; export interface IFindOptions { limit?: number; } /** * */ export interface IDocValidationFunc<T> { (doc: T): boolean; } export type TDelayed<TDelayedArg> = () => TDelayedArg; /** * This is a decorator that will tell the decorated class what dbTable to use * @param dbArg */ export declare function Collection(dbArg: SmartdataDb | TDelayed<SmartdataDb>): (value: Function, context: ClassDecoratorContext) => any; export interface IManager { db: SmartdataDb; } export declare const setDefaultManagerForDoc: <T>(managerArg: IManager, dbDocArg: T) => T; /** * This is a decorator that will tell the decorated class what dbTable to use * @param dbArg */ export declare function managed<TManager extends IManager>(managerArg?: TManager | TDelayed<TManager>): (value: Function, context: ClassDecoratorContext) => any; /** * @dpecrecated use @managed instead */ export declare const Manager: typeof managed; export declare class SmartdataCollection<T> { /** * the collection that is used */ mongoDbCollection: plugins.mongodb.Collection; objectValidation: IDocValidationFunc<T> | null; collectionName: string; smartdataDb: SmartdataDb; uniqueIndexes: string[]; regularIndexes: Array<{ field: string; options: IIndexOptions; }>; private textIndexCreated; constructor(classNameArg: string, smartDataDbArg: SmartdataDb); /** * makes sure a collection exists within MongoDb that maps to the SmartdataCollection */ init(): Promise<void>; /** * mark unique index */ markUniqueIndexes(keyArrayArg?: string[]): Promise<void>; /** * creates regular indexes for the collection */ createRegularIndexes(indexesArg?: Array<{ field: string; options: IIndexOptions; }>): Promise<void>; /** * Logs duplicate values for a field to help diagnose unique index creation failures. */ private logDuplicatesForField; /** * adds a validation function that all newly inserted and updated objects have to pass */ addDocValidation(funcArg: IDocValidationFunc<T>): void; /** * finds an object in the DbCollection */ findOne(filterObject: any, opts?: { session?: plugins.mongodb.ClientSession; }): Promise<any>; getCursor(filterObjectArg: any, dbDocArg: typeof SmartDataDbDoc, opts?: { session?: plugins.mongodb.ClientSession; }): Promise<SmartdataDbCursor<any>>; /** * finds an object in the DbCollection */ findAll(filterObject: any, opts?: { session?: plugins.mongodb.ClientSession; }): Promise<any[]>; /** * Watches the collection, returning a SmartdataDbWatcher with RxJS and EventEmitter support. * @param filterObject match filter for change stream * @param opts optional MongoDB ChangeStreamOptions & { bufferTimeMs } to buffer events * @param smartdataDbDocArg document class for instance creation */ watch(filterObject: any, opts?: (plugins.mongodb.ChangeStreamOptions & { bufferTimeMs?: number; }), smartdataDbDocArg?: typeof SmartDataDbDoc): Promise<SmartdataDbWatcher>; /** * create an object in the database */ insert(dbDocArg: T & SmartDataDbDoc<T, unknown>, opts?: { session?: plugins.mongodb.ClientSession; }): Promise<any>; /** * inserts object into the DbCollection */ update(dbDocArg: T & SmartDataDbDoc<T, unknown>, opts?: { session?: plugins.mongodb.ClientSession; }): Promise<any>; delete(dbDocArg: T & SmartDataDbDoc<T, unknown>, opts?: { session?: plugins.mongodb.ClientSession; }): Promise<any>; getCount(filterObject: any, opts?: { session?: plugins.mongodb.ClientSession; }): Promise<number>; /** * Runs an integrity check on the collection. * Compares estimated vs actual document count and checks for duplicates on unique index fields. */ checkCollectionIntegrity(): Promise<{ ok: boolean; estimatedCount: number; actualCount: number; duplicateFields: Array<{ field: string; duplicateValues: number; }>; }>; /** * checks a Doc for constraints * if this.objectValidation is not set it passes. */ private checkDoc; }