@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.
130 lines (129 loc) • 4.14 kB
TypeScript
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>): <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[];
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[]): void;
/**
* creates regular indexes for the collection
*/
createRegularIndexes(indexesArg?: Array<{
field: string;
options: IIndexOptions;
}>): 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, 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 while applying a filter
*/
watch(filterObject: any, 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>;
/**
* checks a Doc for constraints
* if this.objectValidation is not set it passes.
*/
private checkDoc;
}