import { ErrorInterface, SuccessInterface } from "../../config/Interfaces/Helper/response.helper.interface"; import { SessionOptions } from "../../config/Interfaces/Transaction/transaction.interface"; import Reader from "../CRUD Operation/Reader.operation"; import DeleteOperation from "../CRUD Operation/Delete.operation"; import UpdateOperation from "../CRUD Operation/Update.operation"; import Aggregation from "../Aggregation/Aggregation.Operation"; import Transaction from "../Transaction/Transaction.service"; import Session from "../Transaction/Session.service"; /** * Represents a collection inside a database. */ export default class Collection { private readonly name; private readonly path; private readonly updatedAt; private Converter; private readonly IndexManager; private readonly indexCache; constructor(name: string, path: string); /** * Get Numbers of Documents in the Collection * @returns {Promise} - A promise that resolves with the number of documents in the collection. * @throws {Error} - Throws an error if the collection is empty or if there is an issue with the query. */ totalDocuments(): Promise; /** * Creates a new index on the specified fields by delegating to the IndexManager. * * @remarks * This method accepts one or more field names and forwards them to IndexManager.createIndex. * Index creation behavior (such as uniqueness, ordering, or persistence) is determined by the IndexManager implementation. * * @param fieldNames - One or more field names that should be included in the new index. * @returns A promise that resolves with the result returned by IndexManager.createIndex. * @throws Any errors thrown by IndexManager.createIndex (for example, validation or persistence errors) are propagated. */ newIndex(...fieldNames: string[]): Promise; /** * Drops an index by name from the collection. * * @param indexName - The name of the index to drop. * @returns A promise that resolves with the result returned by IndexManager.dropIndex. * @throws Will throw an error if the drop operation fails (for example, if the index does not exist * or if the caller lacks necessary permissions). */ dropIndex(indexName: string): Promise; /** * Lists all indexes currently registered for this collection. * * @returns A promise that resolves with the result returned by IndexManager.listIndexes. */ getIndexes(): Promise; /** * Inserts a document into the collection. * * Routed through a single-operation Transaction so the insert is WAL-backed: if the * process crashes between writing the document and updating its index, Transaction.recoverTransactions() * (run at collection startup) redoes or undoes it from the WAL instead of leaving a * half-written document. * * @param {object} data - The data to be inserted. * @returns {Promise} - A promise that resolves with the response of the insertion operation. */ insert(data: object | any): Promise; /** * Inserts one or multiple documents into the collection. * * Routed through a single Transaction covering every document, so the whole batch * is staged and committed as one atomic unit: each indexed field is read, updated * in memory for all documents, and written back to disk exactly once - instead of * once per document (the previous per-document Transaction approach turned every * indexed insert into an O(N) full index-file rewrite for N documents). * * @param data - A single document object or an array of document objects to be inserted. * @returns A promise that resolves to a `SuccessInterface` containing the total number of documents inserted and their IDs, * or an `ErrorInterface` if the operation fails or is rolled back. */ insertMany(data: object[] | object): Promise; /** * Reads a document from the collection. * @param {object} query - The query to be executed * @returns {Reader} - An instance of the Reader class. */ query(query: object | any): Reader; /** * Initiates an aggregation operation on the collection with the provided pipeline steps. * @param {object[]} PipelineQuerySteps - The pipeline steps to be executed. * @returns {Aggregation} - An instance of the Aggregation class. * @throws {Error} Throws an error if the pipeline steps are empty. * @example * ```typescript * // Aggregate the collection to get the total count of documents * collection.aggregate([{$match: {}}, ${group: {_id: null, count: {$sum: 1}}}]).exec(); * ``` */ aggregate(PipelineQuerySteps: object[]): Aggregation; /** * Initiates a delete operation on the collection with the provided query. * * @param query - The query object that specifies which documents to delete. * @returns A DeleteOperation instance that can be executed to perform the deletion. * @throws {Error} Throws an error if the query is empty. * * @example * ```typescript * // Delete all documents where age is greater than 30 * collection.delete({ age: { $gt: 30 } }); * ``` */ delete(query: object | any): DeleteOperation; update(query: object | any): UpdateOperation; beginTransaction(): Transaction; /** * Starts a new session for MongoDB-like transaction management. * Sessions support automatic retry, timeouts, and the withTransaction pattern. * * @param options - Optional session configuration * @returns A new Session instance * * @example * ```typescript * const session = collection.startSession(); * await session.withTransaction(async (txn) => { * txn.insert({ name: 'Alice' }); * txn.update({ name: 'Bob' }, { age: 30 }); * }); * await session.endSession(); * ``` */ startSession(options?: SessionOptions): Session; }