UNPKG

@warlock.js/cascade

Version:

ORM for managing databases

218 lines 8.8 kB
import type { AggregateOptions, ClientSession, CountDocumentsOptions, DeleteOptions, DistinctOptions, ExplainVerbosityLike, FindOneAndReplaceOptions, FindOneAndUpdateOptions, FindOptions, UpdateFilter, UpdateOptions, WithId } from "mongodb"; import { type Database } from "../database"; import type { Document, Filter, ModelDocument } from "../model/types"; import type { CountedEventPayload, CountingEventPayload, CreatedEventPayload, CreatingEventPayload, DeletedEventPayload, DeletingEventPayload, ExplainedEventPayload, ExplainingEventPayload, FetchedEventPayload, FetchingEventPayload, ReplacedEventPayload, ReplacingEventPayload, SavedEventPayload, SavingEventPayload, SimpleFetchOptions, UpdatedEventPayload, UpdatingEventPayload, UpsertedEventPayload, UpsertingEventPayload } from "./types"; export declare class Query { /** * Connection instance */ protected database: Database; /** * class event name */ eventName: string; /** * Set the database instance */ setDatabase(database: Database): this; /** * Get collection query for the given collection name */ query<TSchema extends Document = Document>(collection: string): Collection<TSchema_1>; /** * Get current active session from database object */ getCurrentSession(): ClientSession; /** * Create a new document in the given collection */ create(collection: string, data: Document, { session }?: { session?: ClientSession; }): Promise<WithId<{ [key: string]: any; id?: number; }>>; /** * Create many documents in the given collection */ createMany(collection: string, data: Document[], { session }?: { session?: ClientSession; }): Promise<ModelDocument[]>; /** * Update model by the given id */ update(collection: string, filter: Filter, data: Document, options?: FindOneAndUpdateOptions): Promise<Partial<ModelDocument> | null>; /** * Update a single document in the given collection */ updateOne(collection: string, filter: Filter, update: UpdateFilter<Document>, options?: UpdateOptions): Promise<any>; /** * Update many documents */ updateMany(collection: string, filter: Filter, updateOptions: UpdateFilter<Document>, options?: UpdateOptions): Promise<any>; /** * Increment the value of the given field by the given amount */ increment(collection: string, filter: Filter, field: string, amount?: number): Promise<any>; /** * Decrement the value of the given field by the given amount */ decrement(collection: string, filter: Filter, field: string, amount?: number): Promise<any>; /** * Find and increment the value of the given field by the given amount */ findAndIncrement(collection: string, filter: Filter, field: string, amount?: number): Promise<any>; /** * Find and decrement the value of the given field by the given amount */ findAndDecrement(collection: string, filter: Filter, field: string, amount?: number): Promise<any>; /** * Replace the entire document for the given document id with the given new data */ replace(collection: string, filter: Filter, data: Document, options?: FindOneAndReplaceOptions): Promise<Partial<ModelDocument> | null>; /** * Find and update the document for the given filter with the given data or create a new document/record * if filter has no matching */ upsert(collection: string, filter: Filter, data: Document, options?: FindOneAndUpdateOptions): Promise<Partial<ModelDocument> | null>; /** * Perform a single delete operation for the given collection */ deleteOne(collection: string, filter?: Filter, options?: DeleteOptions): Promise<boolean>; /** * Delete multiple documents from the given collection */ delete(collection: string, filter?: Filter, options?: DeleteOptions): Promise<number>; /** * Alias to delete * * @alias delete */ deleteMany(collection: string, filter?: Filter, options?: DeleteOptions): Promise<number>; /** * Check if document exists for the given collection with the given filter */ exists(collection: string, filter?: Filter, options?: FindOptions): Promise<boolean>; /** * Find a single document for the given collection with the given filter */ first<T extends Document = Document>(collection: string, filter?: Filter, findOptions?: Omit<SimpleFetchOptions, "limit">): Promise<WithId<T_1>>; /** * Find last document for the given collection with the given filter */ last<T extends Document = Document>(collection: string, filter?: Filter, findOptions?: Omit<SimpleFetchOptions, "limit">): Promise<WithId<T_1>>; /** * Find multiple document for the given collection with the given filter */ list<T extends Document = Document>(collection: string, filter?: Filter, options?: SimpleFetchOptions): Promise<WithId<T>[]>; /** * Find latest documents for the given collection with the given filter */ latest<T extends Document = Document>(collection: string, filter?: Filter, findOptions?: SimpleFetchOptions): Promise<WithId<T>[]>; /** * Find oldest documents for the given collection with the given filter */ oldest(collection: string, filter?: Filter, options?: FindOptions): Promise<any>; /** * Get distinct values for the given collection with the given filter */ distinct(collection: string, field: string, filter?: Filter, options?: DistinctOptions): Promise<any>; /** * Count documents for the given collection with the given filter */ count(collection: string, filter?: Filter, options?: CountDocumentsOptions): Promise<any>; /** * Create an explain fetch query */ explain(collection: string, filter?: Filter, options?: FindOptions, verbosity?: ExplainVerbosityLike): Promise<any>; /** * Create aggregate query */ aggregate(collection: string, pipeline: Document[], options?: AggregateOptions): Promise<any>; /** * Trigger event */ trigger(eventName: string, payload?: Record<string, any>): Promise<import("@mongez/events").EventTriggerResponse[]>; /** * Listen on creating event */ onCreating(callback: (payload: CreatingEventPayload) => void): this; /** * Listen on created event */ onCreated(callback: (payload: CreatedEventPayload) => void): this; /** * Listen on updating event */ onUpdating(callback: (payload: UpdatingEventPayload) => void): this; /** * Listen on updated event */ onUpdated(callback: (payload: UpdatedEventPayload) => void): this; /** * Listen on upserting event */ onUpserting(callback: (payload: UpsertingEventPayload) => void): this; /** * Listen on upserted event */ onUpserted(callback: (payload: UpsertedEventPayload) => void): this; /** * Listen on replacing event */ onReplacing(callback: (payload: ReplacingEventPayload) => void): this; /** * Listen on replaced event */ onReplaced(callback: (payload: ReplacedEventPayload) => void): this; /** * Listen on saving event */ onSaving(callback: (payload: SavingEventPayload) => void): this; /** * Listen on saved event */ onSaved(callback: (payload: SavedEventPayload) => void): this; /** * Listen on fetching event */ onFetching(callback: (payload: FetchingEventPayload) => void): this; /** * Listen on fetched event */ onFetched(callback: (payload: FetchedEventPayload) => void): this; /** * Listen on counting event */ onCounting(callback: (payload: CountingEventPayload) => void): this; /** * Listen on counted event */ onCounted(callback: (payload: CountedEventPayload) => void): this; /** * Listen on explaining event */ onExplaining(callback: (payload: ExplainingEventPayload) => void): this; /** * Listen on explained event */ onExplained(callback: (payload: ExplainedEventPayload) => void): this; /** * Listen on deleting event */ onDeleting(callback: (payload: DeletingEventPayload) => void): this; /** * Listen on deleted event */ onDeleted(callback: (payload: DeletedEventPayload) => void): this; /** * Listen to the given event */ on(event: string, callback: (payload: any) => void): void; /** * Prepare query options and add session if not exists */ protected prepareQueryOptions(options?: Record<string, any>): Record<string, any>; } export declare const query: Query; //# sourceMappingURL=query.d.ts.map