UNPKG

axiodb

Version:

The Pure JavaScript Alternative to SQLite. Embedded NoSQL database for Node.js with MongoDB-style queries, zero native dependencies, built-in InMemoryCache, and web GUI. Perfect for desktop apps, CLI tools, and embedded systems. No compilation, no platfor

119 lines (118 loc) 5.55 kB
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"; import { CryptoHelper } from "../../Helper/Crypto.helper"; /** * Represents a collection inside a database. */ export default class Collection { private readonly name; private readonly path; private readonly updatedAt; private readonly isEncrypted; private readonly cryptoInstance?; private Converter; private Insertion; private readonly encryptionKey; private readonly IndexManager; private readonly indexCache; constructor(name: string, path: string, isEncrypted?: boolean, cryptoInstance?: CryptoHelper, encryptionKey?: string); /** * Get Numbers of Documents in the Collection * @returns {Promise<number>} - 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<SuccessInterface | ErrorInterface>; /** * 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<SuccessInterface | undefined>; /** * 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<SuccessInterface | ErrorInterface>; /** * Inserts a document into the collection. * @param {object} data - The data to be inserted. * @returns {Promise<any>} - A promise that resolves with the response of the insertion operation. */ insert(data: object | any): Promise<SuccessInterface | ErrorInterface | undefined>; /** * Inserts one or multiple documents into the collection. * * @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. */ insertMany(data: object[] | object): Promise<SuccessInterface | ErrorInterface>; /** * 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; }