UNPKG

docudb

Version:

Document-based NoSQL database for NodeJS

252 lines 8.82 kB
/** * Main database module * Integrates all components and provides the CRUD interface */ import FileStorage from '../storage/fileStorage.js'; import IndexManager from '../index/indexManager.js'; import { DatabaseOptions, StorageOptions, CollectionOptions, CollectionMetadata, DocumentStructure, UpdateOperations, Document, Schema, IndexOptions, Index, QueryCriteria } from '../types/index.js'; declare class Database { /** Database name */ name: string; /** Directory to store data */ dataDir: string; /** Collections in this database */ collections: Record<string, any>; /** Storage options */ storageOptions: StorageOptions; /** ID generation type */ idType: 'mongo' | 'uuid'; /** File storage instance */ storage: FileStorage; /** Index manager instance */ indexManager: IndexManager; /** Database Initialized */ private _initialized; /** * Creates a new database instance * @param options - Configuration options */ constructor(options?: DatabaseOptions); /** * Initializes the database * @returns {Promise<void>} */ initialize(): Promise<void>; /** * Gets a collection * @param {string} collectionName - Collection name * @param {CollectionOptions} options - Collection options * @returns {Collection} - Collection instance */ collection(collectionName: string, options?: CollectionOptions): Collection; /** * Drops a collection * @param {string} collectionName - Name of collection to drop * @returns {Promise<boolean>} - true if successfully dropped */ dropCollection(collectionName: string): Promise<boolean>; /** * Lists all collections * @returns {Promise<string[]>} - Collection names */ listCollections(): Promise<string[]>; /** * Loads existing collections * @private */ private _loadCollections; } export declare class Collection { name: string; storage: FileStorage; indexManager: IndexManager; options: CollectionOptions; schema: Schema | null; documents: Record<string, DocumentStructure>; metadataPath: string; metadata: CollectionMetadata; /** * @param {string} name - Collection name * @param {FileStorage} storage - Storage instance * @param {IndexManager} indexManager - Index manager instance * @param {Object} options - Additional options */ constructor(name: string, storage: FileStorage, indexManager: IndexManager, options?: CollectionOptions); /** * Initializes the collection * @returns {Promise<void>} */ initialize(): Promise<void>; /** * Inserts a document into the collection * @param {DocumentStructure} doc - Document to insert * @returns {Promise<Document>} - Inserted document with ID */ insertOne(doc: DocumentStructure): Promise<Document>; /** * Inserts multiple documents into the collection * @param {Object[]} docs - Documents to insert * @returns {Promise<Object[]>} - Inserted documents with IDs */ insertMany(docs: DocumentStructure[]): Promise<Document[]>; /** * Finds a document by its ID * @param {string} id - Document ID * @returns {Promise<Object|null>} - Found document or null */ findById(id: string): Promise<Document | null>; /** * Finds documents matching criteria * @param {QueryCriteria} criteria - Search criteria * @returns {Promise<DocumentStructure[]>} - Found documents */ find(criteria?: QueryCriteria): Promise<Document[]>; /** * Finds one document matching criteria * @param {QueryCriteria} criteria - Search criteria * @returns {Promise<Object|null>} - Found document or null */ findOne(criteria?: QueryCriteria): Promise<Document | null>; /** * Updates a document by its ID * @param {string} id - Document ID * @param {Object} update - Changes to apply * @returns {Promise<Document|null>} - Updated document or null */ updateById(id: string, update: UpdateOperations): Promise<Document | null>; /** * Updates documents matching criteria * @param {QueryCriteria} criteria - Search criteria * @param {Object} update - Changes to apply * @returns {Promise<number>} - Number of documents updated */ updateMany(criteria: QueryCriteria, update: UpdateOperations): Promise<number>; /** * Deletes a document by its ID * @param {string} id - Document ID * @returns {Promise<boolean>} - true if successfully deleted */ deleteById(id: string): Promise<boolean>; /** * Deletes the first document found with the query criteria * @param {QueryCriteria} criteria - Search criteria * @returns {Promise<boolean>} - true if successfully deleted */ deleteOne(criteria: QueryCriteria): Promise<boolean>; /** * Deletes documents matching criteria * @param {QueryCriteria} criteria - Search criteria * @returns {Promise<number>} - Number of documents deleted */ deleteMany(criteria: QueryCriteria): Promise<number>; /** * Counts documents matching criteria * @param {QueryCriteria} criteria - Search criteria * @returns {Promise<number>} - Number of documents */ count(criteria?: QueryCriteria): Promise<number>; /** * Creates an index for a field * @param {string} field - Field to index * @param {Object} options - Index options * @returns {Promise<boolean>} - true if successfully created */ createIndex(field: string | string[], options?: IndexOptions): Promise<boolean>; /** * List all the indices in the collection * @returns {Promise<Index[]>} - List of indices */ listIndexes(): Promise<Index[]>; /** * Drops an index * @param {string} field - Indexed field * @returns {Promise<boolean>} - true if successfully dropped */ dropIndex(field: string): Promise<boolean>; /** * Gets the index of a document by its ID in the collection * @param {string} id - Document ID * @returns {Promise<number>} - Index of the document or -1 if not found */ getPosition(id: string): Promise<number>; /** * Finds a document by its index in the collection * @param {number} position - Index of the document in the collection * @returns {Promise<Object|null>} - Document at the specified index or null if not found */ findByPosition(position: number): Promise<Document | null>; /** * Updates the position of a document in the collection * @param {string} id - Document ID * @param {number} newIndex - New index position for the document * @returns {Promise<boolean>} - true if successfully updated, false if document not found */ updatePosition(id: string, newIndex: number): Promise<boolean>; /** * Removes the collection * @returns {Promise<void>} */ drop(): Promise<void>; /** * Loads the collection metadata * @private */ private _loadMetadata; /** * Stores the collection's metadata * @private */ private _saveMetadata; /** * Generates a unique ID * @returns {string} - Generated ID * @private */ private _generateId; /** * Applies an update to a document * @param {Object} doc - Original document * @param {Object} update - Changes to apply * @returns {Object} - Updated document * @private */ private _applyUpdate; /** * Sets a nested value in an object * @param {Object} obj - Object to modify * @param {string} path - Path to the value using dot notation * @param {*} value - Value to set * @private */ private _setNestedValue; /** * Removes a nested value from an object * @param {Object} obj - Object to modify * @param {string} path - Path to the value using dot notation * @private */ private _unsetNestedValue; /** * Gets a nested value from an object * @param {Object} obj - Object to get the value from * @param {string} path - Path to the value using dot notation * @returns {*} - Found or undefined value * @private */ private _getNestedValue; /** * Loads all documents in the collection * @returns {Promise<Document[]>} - All documents * @private */ private _loadAllDocuments; /** * Attempts to optimize a query using indexes * @param {Query} query - Query to optimize * @returns {Promise<Document[]|null>} - Results or null if the optimization failed * @private */ private _findWithOptimization; } export default Database; //# sourceMappingURL=database.d.ts.map