@warlock.js/cascade
Version:
ORM for managing databases
122 lines • 5.01 kB
TypeScript
import { type SimpleFetchOptions } from "../query/types";
import { BaseModel } from "./base-model";
import type { ChildModel, ChunkCallback, Document, Filter, FindOrCreateOptions, PaginationListing, PrimaryIdType } from "./types";
export declare abstract class CrudModel extends BaseModel {
/**
* Create a new record in the database for the current model (child class of this one)
* and return a new instance of it with the created data and the new generated id
*/
static create<T>(this: ChildModel<T>, data: Document): Promise<T>;
/**
* Create many records in the database for the current model (child class of this one)
*/
static createMany<T>(this: ChildModel<T>, data: Document[]): Promise<T[]>;
/**
* Called before creating a new record
*/
protected beforeCreating<T>(this: ChildModel<T>, _data: Document): Promise<void>;
/**
* Called after creating a new record
*/
protected onCreate<T>(this: ChildModel<T>): Promise<void>;
/**
* Update model by the given id
*/
static update<T>(this: ChildModel<T>, id: PrimaryIdType, data: Document): Promise<T | null>;
/**
* Replace the entire document for the given document id with the given new data
*/
static replace<T>(this: ChildModel<T>, id: PrimaryIdType, data: Document): Promise<T | null>;
/**
* Restore the document from trash
*/
static restore<T>(this: ChildModel<T>, id: PrimaryIdType): Promise<T | null>;
/**
* Restore all documents from trash or by the given filter
*/
static restoreAll<T>(this: ChildModel<T>, filter?: Filter): Promise<any[]>;
/**
* Get deleted document by id
*/
static getDeleted<T>(this: ChildModel<T>, id: PrimaryIdType): Promise<T | null>;
/**
* Get all deleted documents
*/
static getAllDeleted<T>(this: ChildModel<T>, filter?: Filter): Promise<any[]>;
/**
* Find and update the document for the given filter with the given data or create a new document/record
* if filter has no matching
*/
static upsert<T>(this: ChildModel<T>, filter: Filter, data: Document): Promise<T>;
/**
* Find document by id
*/
static find<T>(this: ChildModel<T>, id: PrimaryIdType): Promise<T>;
/**
* Find document by the given column and value
*/
static findBy<T>(this: ChildModel<T>, column: string, value: any): Promise<T | null>;
/**
* Create an explain plan for the given filter
*/
static explain<T>(this: ChildModel<T>, filter?: Filter, options?: SimpleFetchOptions): Promise<any>;
/**
* List multiple documents based on the given filter
*/
static list<T extends Document = Document>(this: ChildModel<T>, filter?: Filter, options?: SimpleFetchOptions): Promise<T[]>;
/**
* Paginate records based on the given filter
*/
static paginate<T>(this: ChildModel<T>, filter: Filter, page?: number, limit?: number): Promise<PaginationListing<T>>;
/**
* Find or create a new document based on the given filter and data
* If the document is not found, it will be created
* otherwise, just return the found document
*/
static findOrCreate<T>(this: ChildModel<T>, filter: Filter, data: Document, { merge }?: FindOrCreateOptions): Promise<T>;
/**
* Update or create a new document based on the given filter and data
*/
static updateOrCreate<T>(this: ChildModel<T>, filter: Filter, data: Document): Promise<T>;
/**
* Count total documents based on the given filter
*/
static count(filter?: Filter): Promise<any>;
/**
* Get first model for the given filter
*/
static first<T>(this: ChildModel<T>, filter?: Filter): Promise<T | null>;
/**
* Get last model for the given filter
*/
static last<T>(this: ChildModel<T>, filter?: Filter): Promise<T | null>;
/**
* Get latest documents
*/
static latest<T>(this: ChildModel<T>, filter?: Filter): Promise<T[]>;
/**
* Delete single document if the given filter is an ObjectId of mongodb
* Otherwise, delete multiple documents based on the given filter object
*/
static delete<T>(this: ChildModel<T>, filter?: PrimaryIdType | Filter): Promise<number>;
/**
* Chunk the documents
*/
static chunk<T>(this: ChildModel<T>, limit: number, callback: ChunkCallback<T>): Promise<void>;
static chunk<T>(this: ChildModel<T>, filter: Filter & {
limit: number;
}, callback: ChunkCallback<T>): Promise<void>;
/**
* Check if document exists for the given filter
*/
static exists<T>(this: ChildModel<T>, filter?: Filter): Promise<boolean>;
/**
* Get distinct values for the given column
*/
static distinct<T>(this: ChildModel<T>, column: string, filter?: Filter): Promise<any[]>;
/**
* Prepare filters
*/
protected static prepareFilters(filters?: Filter): Promise<Filter>;
}
//# sourceMappingURL=crud-model.d.ts.map