UNPKG

evelodb

Version:

A high-performance native B-tree database for Node.js. Made by Evelocore.

223 lines (222 loc) 7 kB
import { ObjectId } from 'bson'; export interface CollectionSchema { fields?: Record<string, any>; indexes?: string[]; uniqueKeys?: string[]; objectIdKey?: string; noRepeat?: boolean; } export interface EveloDBConfig { directory?: string; maxHandles?: number; compactThreshold?: number; schema?: Record<string, CollectionSchema>; } export interface ReadImageConfig { returnBase64?: boolean; quality?: number; pixels?: number; blackAndWhite?: boolean; mirror?: boolean; upToDown?: boolean; invert?: boolean; brightness?: number; contrast?: number; maxWidth?: number | null; maxHeight?: number | null; } export interface WriteResult { success?: boolean; err?: string; code?: string | number; _id?: string | ObjectId; [key: string]: any; } export interface BackupResult { success: boolean; err?: string; backupPath?: string; } export interface DeleteResult { success?: boolean; err?: string; code?: number | string; deletedCount?: number; } export interface EditResult { success?: boolean; err?: boolean | string; code?: string | number; modifiedCount?: number; skippedDuplicates?: number; } export interface CountResult { success: boolean; count?: number; err?: string; } export interface DropResult { success?: boolean; err?: string | number; code?: string | number; deletedCount?: number; message?: string; } export interface FileResult { success?: boolean; err?: string; code?: string | number; data?: Buffer; } export interface ReadImageResult { success?: boolean; err?: string; code?: string | number; data?: unknown; metadata?: { filename: string; extension: string; originalSize: number; processingApplied: { resized: boolean; qualityReduced: boolean; blackAndWhite: boolean; mirrored: boolean; flippedVertical: boolean; inverted: boolean; brightnessAdjusted: boolean; contrastAdjusted: boolean; }; }; } export interface ObjectResult<T = any> { success: boolean; data?: T; err?: string; code?: string | number; } export interface Condition { $eq?: unknown; $ne?: unknown; $gt?: unknown; $gte?: unknown; $lt?: unknown; $lte?: unknown; $in?: unknown[]; $nin?: unknown[]; $regex?: string; $options?: string; } export type Conditions = Record<string, unknown | Condition>; declare class ObjectStore { private dbDir; private name?; private baseDir; constructor(dbDir: string, name?: string | undefined); private getPath; read<T = any>(): T | null; write(data: Record<string, any>): ObjectResult; update(data: Record<string, any>): ObjectResult; delete(): ObjectResult; rename(newName: string): ObjectResult; list(): string[]; } export declare class QueryResult<T = unknown> { data: T[]; err?: string; constructor(data: T[] | null | undefined, err?: string); getList(offset?: number, limit?: number): T[]; count(): number; sort(compareFn: (a: T, b: T) => number): QueryResult<T>; all(): T[]; } export declare class eveloDB { config: Required<EveloDBConfig>; private handles; private locks; private backupManager; constructor(config?: EveloDBConfig); private getBsonPaths; private getHandle; private evictLRU; private flushHandle; private closeHandle; closeAll(): void; /** * Lists all collection names currently known to the database directory * by scanning for `.db` files on disk. */ listCollections(): string[]; /** * Re-initializes the database instance by closing all open handles, * clearing in-memory state, and re-syncing with the physical files on disk. * Call this after external modifications to the database files. */ reInit(): void; private generateUniqueId; private getObjectIdKey; private mapInput; private mapOutput; private matchesConditions; private isDuplicateInArray; private maybeCompact; private buildFingerprintSet; private fingerprintOf; private validateSchema; create(collection: string, data: Record<string, unknown>): WriteResult; delete(collection: string, conditions: Conditions): DeleteResult; find<T = Record<string, unknown>>(collection: string, conditions: Conditions, raw?: boolean): QueryResult<T>; findOne<T = Record<string, unknown>>(collection: string, conditions: Conditions): T | null; get<T = Record<string, unknown>>(collection: string): QueryResult<T>; update(collection: string, conditions: Conditions, newData: Record<string, unknown>): EditResult; inject(collection: string, data: Record<string, unknown>[], options?: { method?: 'overwrite' | 'merge'; }): any; private applyUpdateOperators; edit(collection: string, conditions: Conditions, newData: Record<string, unknown>): EditResult; /** Returns all records with original _id (unmapped) */ allInternal(collection: string): Record<string, unknown>[]; count(collection: string): CountResult; check(collection: string, data: Conditions): boolean; search<T = Record<string, unknown>>(collection: string, conditions: Record<string, unknown>, raw?: boolean): QueryResult<T>; drop(collection: string): DropResult; reset(collection: string): DropResult; compact(collection: string): { success: boolean; err?: string; }; writeFile(name: string, data: Buffer): FileResult; allFiles(): string[]; readFile(name: string): FileResult; readImage(name: string, config?: ReadImageConfig): Promise<ReadImageResult>; deleteFile(name: string): FileResult; createBackup(collection: string, config: { type?: 'json' | 'binary'; path: string; password?: string; title?: string; }): BackupResult; restoreBackup(collection: string, config: { type?: 'json' | 'binary'; file: string; password?: string; }): { success: boolean; err?: string; }; readBackupFile(filePath: string, password?: string): any; rebuildIndexes(collection: string): void; /** * Performs an asynchronous transaction on a collection. * Ensures that only one transaction runs at a time for the specified collection. */ transaction<T>(collection: string, callback: () => Promise<T> | T): Promise<T>; /** * Performs an atomic operation. * If a collection name is provided, it locks only that collection. * If no collection is provided, it acts as a global database lock. */ atomic<T>(collectionOrCallback: string | ((tx: eveloDB) => Promise<T> | T), callback?: (tx: eveloDB) => Promise<T> | T): Promise<T>; object(name?: string): ObjectStore; } export default eveloDB;