@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.
100 lines (99 loc) • 3.4 kB
TypeScript
import * as plugins from './smartdata.plugins.js';
import { SmartdataDb } from './smartdata.classes.db.js';
import { SmartdataDbCursor } from './smartdata.classes.cursor.js';
import { SmartDataDbDoc } from './smartdata.classes.doc.js';
import { SmartdataDbWatcher } from './smartdata.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>): <T extends new (...args: any[]) => {}>(constructor: T) => {
new (...args: any[]): {
readonly collection: SmartdataCollection<any>;
};
className: string;
readonly collection: SmartdataCollection<any>;
} & T;
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>): <T extends new (...args: any[]) => any>(constructor: T) => {
new (...args: any[]): {
[x: string]: any;
readonly collection: SmartdataCollection<any>;
readonly manager: TManager;
};
className: string;
readonly collection: SmartdataCollection<any>;
readonly manager: TManager;
} & T;
/**
* @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>;
collectionName: string;
smartdataDb: SmartdataDb;
uniqueIndexes: string[];
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[]): void;
/**
* 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): Promise<any>;
getCursor(filterObjectArg: any, dbDocArg: typeof SmartDataDbDoc): Promise<SmartdataDbCursor<any>>;
/**
* finds an object in the DbCollection
*/
findAll(filterObject: any): Promise<any[]>;
/**
* watches the collection while applying a filter
*/
watch(filterObject: any, smartdataDbDocArg: typeof SmartDataDbDoc): Promise<SmartdataDbWatcher>;
/**
* create an object in the database
*/
insert(dbDocArg: T & SmartDataDbDoc<T, unknown>): Promise<any>;
/**
* inserts object into the DbCollection
*/
update(dbDocArg: T & SmartDataDbDoc<T, unknown>): Promise<any>;
delete(dbDocArg: T & SmartDataDbDoc<T, unknown>): Promise<any>;
getCount(filterObject: any): Promise<number>;
/**
* checks a Doc for constraints
* if this.objectValidation is not set it passes.
*/
private checkDoc;
}