@valkyriestudios/mongo
Version:
MongoDB Adapter Library
101 lines (100 loc) • 5.38 kB
TypeScript
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 } from 'mongodb';
type BulkOperator = OrderedBulkOperation | UnorderedBulkOperation;
type BulkOperatorFunction = (operator: BulkOperator) => void;
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>;
/**
* 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 extends Document>(pipeline: Document[], options?: AggregateOptions): Promise<T[]>;
/**
* 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 extends 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 };