UNPKG

@valkyriestudios/mongo

Version:
116 lines (115 loc) 6.08 kB
import { Mongo } from './index'; import { type InsertOneOptions, type InsertOneResult, type AggregateOptions, type BulkWriteResult, type CountOptions, type DeleteOptions, type Document, type Filter, type OrderedBulkOperation, type UnorderedBulkOperation, type UpdateFilter, type UpdateOptions, type DistinctOptions } from 'mongodb'; type BulkOperator = OrderedBulkOperation | UnorderedBulkOperation; type BulkOperatorFunction = (operator: BulkOperator) => void; type Flatten<T> = T extends ReadonlyArray<infer U> ? U : T; declare class Query<TModel extends Document = Document> { #private; constructor(instance: Mongo, col: string); /** * Counts the number of records. Pass pipeline to run a count with filters. * * Take Note: This will automatically add a $count stage to the pipeline when running an aggregation pipeline * * @param {Filter<Document>|Document[]?} filter - Optional filter object or aggregation pipeline to run * @param {CountOptions|AggregateOptions?} options - Options to use when running a filter/pipeline */ count(filter?: Filter<TModel> | Document[], options?: CountOptions | AggregateOptions): Promise<number>; /** * Quickly checks if a document exists matching the filter * * @param {Filter<TModel>} filter - Filter to match */ exists(filter?: Filter<TModel>): Promise<boolean>; /** * Run an aggregation pipeline against the collection and return its results * * @param {Document[]} pipeline - Pipeline array to run * @param {AggregateOptions} options - (default={}) Aggregation options * @returns {Promise<Document[]>} Array of documents - Null is returned when aggregation fails */ aggregate<T = Document>(pipeline: Document[], options?: AggregateOptions): Promise<T[]>; /** * Find unique values for a specified field across the collection * * @param {string} key - Field name to get distinct values for * @param {Filter<TModel>} filter - Optional filter to narrow scope * @returns {Promise<any[]>} Array of unique values */ distinct<Key extends keyof TModel>(key: Key & string, filter?: Filter<TModel>, options?: DistinctOptions): Promise<Flatten<TModel[Key]>[]>; /** * Find the first document matching the provided query * Take Note: when passing no query it will simply return the first document it finds * * @param {Filter<Document>?} query - Optional Query that matches the document to find * @param {Document?} projection - Optional projection to use, if not passed will return entire object * @returns {Promise<Document|null>} The found document or null * @throws {Error} when provided query or connection fails */ findOne<T = TModel>(query?: Filter<TModel>, projection?: Document): Promise<T | null>; /** * Remove the first document matching the provided query * * @param {Filter<Document>} query - Query that matches the document to be removed * @param {DeleteOptions} options - (default={}) Remove options * @returns {Promise<boolean>} Result of the query * @throws {Error} When provided options are invalid or connection fails */ removeOne(query: Filter<TModel>, options?: DeleteOptions): Promise<boolean>; /** * Remove all documents matching the provided query * * @param {Filter<Document>} query - Query that matches the documents to be removed * @param {DeleteOptions} options - (default={}) Remove options * @returns {Promise<boolean>} Result of the query * @throws {Error} When provided options are invalid or connection fails */ removeMany(query: Filter<TModel>, options?: DeleteOptions): Promise<boolean>; /** * Update the first document matching the provided query * * @param {Filter<Document>} query - Query that matches the documents to be updated * @param {UpdateFilter<Document>} data - Update to run * @param {UpdateOptions} options - Update Options * @returns {Promise<boolean>} Result of the query * @throws {Error} When provided options are invalid or connection fails */ updateOne(query: Filter<TModel>, data: UpdateFilter<TModel>, options?: UpdateOptions): Promise<boolean>; /** * Update all documents matching the provided query * * @param {Filter<Document>} query - Query that matches the documents to be updated * @param {UpdateFilter<Document>} data - Update to run * @param {UpdateOptions} options - Update Options * @returns {Promise<boolean>} Result of the query * @throws {Error} When provided options are invalid or connection fails */ updateMany(query: Filter<TModel>, data: UpdateFilter<TModel>, options?: UpdateOptions): Promise<boolean>; /** * Insert a document into a specific collection * * @param {Document} document - Document to insert * @param {InsertOneOptions} options - Update Options * @returns {Promise<InsertOneResult>} Result of the query * @throws {Error} When provided options are invalid or connection fails */ insertOne(document: TModel, options?: InsertOneOptions): Promise<InsertOneResult['insertedId'] | null>; /** * Insert multiple documents into a specific collection * * @param {Document[]} documents - Array of documents to insert * @returns {Promise<boolean>} Result of the query * @throws {Error} When provided options are invalid or connection fails */ insertMany(documents: TModel[]): Promise<boolean>; /** * Run bulk operations * * @param {BulkOperatorFunction} fn - Bulk operations callback function * @param {boolean} sorted - Whether or not an unordered (false) or ordered (true) bulk operation should be used * @returns {Promise<BulkWriteResult|null>} Result of the query * @throws {Error} When provided options are invalid or connection fails */ bulkOps(fn: BulkOperatorFunction, sorted?: boolean): Promise<BulkWriteResult | null>; } export { Query, Query as default };