nodejs-json-db 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1052 @@
1
+ /**
2
+ * Base document type - all documents must have an _id field
3
+ */
4
+ interface Document {
5
+ _id: string;
6
+ [key: string]: unknown;
7
+ }
8
+ /**
9
+ * Query comparison operators
10
+ */
11
+ interface ComparisonOperators<T> {
12
+ $eq?: T;
13
+ $ne?: T;
14
+ $gt?: T;
15
+ $gte?: T;
16
+ $lt?: T;
17
+ $lte?: T;
18
+ $in?: T[];
19
+ $nin?: T[];
20
+ $regex?: RegExp | string;
21
+ $exists?: boolean;
22
+ $startsWith?: string;
23
+ $endsWith?: string;
24
+ $contains?: T extends unknown[] ? T[number] : never;
25
+ }
26
+ /**
27
+ * Field-level query type
28
+ */
29
+ type QueryField<T> = T | ComparisonOperators<T>;
30
+ /**
31
+ * Query type for finding documents
32
+ */
33
+ type Query<T> = {
34
+ [K in keyof T]?: QueryField<T[K]>;
35
+ } & {
36
+ $and?: Query<T>[];
37
+ $or?: Query<T>[];
38
+ $not?: Query<T>;
39
+ };
40
+ /**
41
+ * Update operators for modifying documents
42
+ */
43
+ interface UpdateOperators<T> {
44
+ $set?: Partial<T>;
45
+ $unset?: {
46
+ [K in keyof T]?: true;
47
+ };
48
+ $inc?: {
49
+ [K in keyof T]?: T[K] extends number ? number : never;
50
+ };
51
+ $push?: {
52
+ [K in keyof T]?: T[K] extends unknown[] ? T[K][number] : never;
53
+ };
54
+ $pull?: {
55
+ [K in keyof T]?: T[K] extends unknown[] ? T[K][number] : never;
56
+ };
57
+ $addToSet?: {
58
+ [K in keyof T]?: T[K] extends unknown[] ? T[K][number] : never;
59
+ };
60
+ }
61
+ /**
62
+ * Sort order for query results
63
+ */
64
+ type SortOrder = 1 | -1 | 'asc' | 'desc';
65
+ /**
66
+ * Sort specification
67
+ */
68
+ type Sort<T> = {
69
+ [K in keyof T]?: SortOrder;
70
+ };
71
+ /**
72
+ * Options for find operations
73
+ */
74
+ interface FindOptions<T> {
75
+ sort?: Sort<T>;
76
+ limit?: number;
77
+ skip?: number;
78
+ }
79
+ /**
80
+ * Collection configuration options
81
+ */
82
+ interface CollectionOptions {
83
+ /** Name of the collection */
84
+ name: string;
85
+ /** Custom ID generator function */
86
+ idGenerator?: () => string;
87
+ }
88
+ /**
89
+ * High-concurrency mode options
90
+ */
91
+ interface HighConcurrencyOptions {
92
+ /** Enable high-concurrency mode (default: false) */
93
+ enabled: boolean;
94
+ /** Number of partitions for data sharding (default: 16) */
95
+ partitions?: number;
96
+ /** Maximum writes before automatic flush (default: 1000) */
97
+ batchSize?: number;
98
+ /** Maximum time in ms before automatic flush (default: 100) */
99
+ flushInterval?: number;
100
+ /** Maximum concurrent I/O operations (default: 4) */
101
+ maxConcurrentIO?: number;
102
+ /** Whether to coalesce duplicate writes (default: true) */
103
+ coalesceWrites?: boolean;
104
+ }
105
+ /**
106
+ * JsonDB configuration options
107
+ */
108
+ interface JsonDBOptions {
109
+ /** Path to the database directory */
110
+ dataDir: string;
111
+ /** Whether to auto-save after each write operation (default: true) */
112
+ autoSave?: boolean;
113
+ /** Debounce time in ms for auto-save (default: 0) */
114
+ saveDebounce?: number;
115
+ /** Whether to pretty print JSON files (default: true) */
116
+ prettyPrint?: boolean;
117
+ /** Custom file extension (default: .json) */
118
+ fileExtension?: string;
119
+ /** High-concurrency mode options (opt-in) */
120
+ highConcurrency?: HighConcurrencyOptions;
121
+ }
122
+ /**
123
+ * Internal collection data structure
124
+ */
125
+ interface CollectionData<T extends Document = Document> {
126
+ name: string;
127
+ documents: T[];
128
+ createdAt: string;
129
+ updatedAt: string;
130
+ }
131
+ /**
132
+ * Storage adapter interface
133
+ */
134
+ interface StorageAdapter {
135
+ read<T extends Document>(collectionName: string): Promise<CollectionData<T> | null>;
136
+ write<T extends Document>(collectionName: string, data: CollectionData<T>): Promise<void>;
137
+ exists(collectionName: string): Promise<boolean>;
138
+ delete(collectionName: string): Promise<void>;
139
+ list(): Promise<string[]>;
140
+ }
141
+
142
+ /**
143
+ * File-based storage adapter for JSON data
144
+ */
145
+ declare class Storage implements StorageAdapter {
146
+ private readonly dataDir;
147
+ private readonly fileExtension;
148
+ private readonly prettyPrint;
149
+ private readonly cache;
150
+ private readonly locks;
151
+ constructor(options: JsonDBOptions);
152
+ /**
153
+ * Ensure the data directory exists
154
+ */
155
+ private ensureDirectory;
156
+ /**
157
+ * Get the file path for a collection
158
+ */
159
+ private getFilePath;
160
+ /**
161
+ * Acquire a lock for a collection
162
+ */
163
+ private acquireLock;
164
+ /**
165
+ * Read collection data from file
166
+ */
167
+ read<T extends Document>(collectionName: string): Promise<CollectionData<T> | null>;
168
+ /**
169
+ * Write collection data to file atomically
170
+ */
171
+ write<T extends Document>(collectionName: string, data: CollectionData<T>): Promise<void>;
172
+ /**
173
+ * Check if a collection file exists
174
+ */
175
+ exists(collectionName: string): Promise<boolean>;
176
+ /**
177
+ * Delete a collection file
178
+ */
179
+ delete(collectionName: string): Promise<void>;
180
+ /**
181
+ * List all collection names
182
+ */
183
+ list(): Promise<string[]>;
184
+ /**
185
+ * Clear the cache for a specific collection or all collections
186
+ */
187
+ clearCache(collectionName?: string): void;
188
+ /**
189
+ * Get the data directory path
190
+ */
191
+ getDataDir(): string;
192
+ }
193
+
194
+ /**
195
+ * Schema validator interface (compatible with Zod)
196
+ */
197
+ interface SchemaValidator$1<T> {
198
+ safeParse(data: unknown): {
199
+ success: true;
200
+ data: T;
201
+ } | {
202
+ success: false;
203
+ error: {
204
+ issues: Array<{
205
+ path: PropertyKey[];
206
+ message: string;
207
+ code?: string;
208
+ }>;
209
+ };
210
+ };
211
+ }
212
+ /**
213
+ * Collection class for managing documents
214
+ */
215
+ declare class Collection<T extends Document> {
216
+ private readonly name;
217
+ private readonly storage;
218
+ private readonly queryEngine;
219
+ private readonly autoSave;
220
+ private readonly saveDebounce;
221
+ private readonly idGenerator;
222
+ private readonly schema?;
223
+ private saveTimeout;
224
+ private pendingSave;
225
+ constructor(name: string, storage: Storage, options?: {
226
+ autoSave?: boolean;
227
+ saveDebounce?: number;
228
+ idGenerator?: () => string;
229
+ schema?: SchemaValidator$1<T>;
230
+ });
231
+ /**
232
+ * Validate a document against the schema (if defined)
233
+ */
234
+ private validate;
235
+ /**
236
+ * Get or create collection data
237
+ */
238
+ private getData;
239
+ /**
240
+ * Save collection data with debouncing
241
+ */
242
+ private save;
243
+ /**
244
+ * Force save any pending writes
245
+ */
246
+ flush(): Promise<void>;
247
+ /**
248
+ * Insert a single document
249
+ */
250
+ insert(doc: Omit<T, '_id'> & {
251
+ _id?: string;
252
+ }): Promise<T>;
253
+ /**
254
+ * Insert a single document without duplicate check (faster)
255
+ * In standard mode, this is equivalent to insert() but skips the duplicate check.
256
+ * Use this when you're certain the ID is unique.
257
+ */
258
+ insertFast(doc: Omit<T, '_id'> & {
259
+ _id?: string;
260
+ }): Promise<T>;
261
+ /**
262
+ * Insert multiple documents
263
+ */
264
+ insertMany(docs: (Omit<T, '_id'> & {
265
+ _id?: string;
266
+ })[]): Promise<T[]>;
267
+ /**
268
+ * Find documents matching a query
269
+ */
270
+ find(query?: Query<T>, options?: FindOptions<T>): Promise<T[]>;
271
+ /**
272
+ * Find a single document matching a query
273
+ */
274
+ findOne(query: Query<T>): Promise<T | null>;
275
+ /**
276
+ * Find a document by ID
277
+ */
278
+ findById(id: string): Promise<T | null>;
279
+ /**
280
+ * Count documents matching a query
281
+ */
282
+ count(query?: Query<T>): Promise<number>;
283
+ /**
284
+ * Update documents matching a query
285
+ */
286
+ update(query: Query<T>, update: UpdateOperators<T> | Partial<T>): Promise<number>;
287
+ /**
288
+ * Update a single document matching a query
289
+ */
290
+ updateOne(query: Query<T>, update: UpdateOperators<T> | Partial<T>): Promise<T | null>;
291
+ /**
292
+ * Update a document by ID
293
+ */
294
+ updateById(id: string, update: UpdateOperators<T> | Partial<T>): Promise<T | null>;
295
+ /**
296
+ * Delete documents matching a query
297
+ */
298
+ delete(query: Query<T>): Promise<number>;
299
+ /**
300
+ * Delete a single document matching a query
301
+ */
302
+ deleteOne(query: Query<T>): Promise<T | null>;
303
+ /**
304
+ * Delete a document by ID
305
+ */
306
+ deleteById(id: string): Promise<T | null>;
307
+ /**
308
+ * Get all documents in the collection
309
+ */
310
+ getAll(): Promise<T[]>;
311
+ /**
312
+ * Clear all documents in the collection
313
+ */
314
+ clear(): Promise<void>;
315
+ /**
316
+ * Drop the collection (delete the file)
317
+ */
318
+ drop(): Promise<void>;
319
+ /**
320
+ * Get the collection name
321
+ */
322
+ getName(): string;
323
+ /**
324
+ * Apply update operators to a document
325
+ */
326
+ private applyUpdate;
327
+ /**
328
+ * Sort documents by the specified sort options
329
+ */
330
+ private sortDocuments;
331
+ }
332
+
333
+ /**
334
+ * Task function type for worker pool
335
+ */
336
+ type TaskFunction<T = void> = () => Promise<T>;
337
+ /**
338
+ * Worker pool options
339
+ */
340
+ interface WorkerPoolOptions {
341
+ /** Maximum concurrent operations (default: 4) */
342
+ maxConcurrent?: number;
343
+ /** Maximum queue size before rejecting new tasks (default: 10000) */
344
+ maxQueueSize?: number;
345
+ }
346
+ /**
347
+ * Worker pool statistics
348
+ */
349
+ interface WorkerPoolStats {
350
+ activeWorkers: number;
351
+ queuedTasks: number;
352
+ completedTasks: number;
353
+ failedTasks: number;
354
+ }
355
+ /**
356
+ * WorkerPool - Manages parallel I/O operations with concurrency limits
357
+ *
358
+ * Features:
359
+ * - Configurable concurrency limit
360
+ * - Priority queue for task ordering
361
+ * - Backpressure handling (rejects when queue full)
362
+ * - Statistics tracking
363
+ */
364
+ declare class WorkerPool {
365
+ private readonly maxConcurrent;
366
+ private readonly maxQueueSize;
367
+ private readonly queue;
368
+ private activeCount;
369
+ private completedCount;
370
+ private failedCount;
371
+ private isShuttingDown;
372
+ constructor(options?: WorkerPoolOptions);
373
+ /**
374
+ * Submit a task to the worker pool
375
+ * @param task The async function to execute
376
+ * @param priority Higher priority tasks are executed first (default: 0)
377
+ */
378
+ submit<T>(task: TaskFunction<T>, priority?: number): Promise<T>;
379
+ /**
380
+ * Submit multiple tasks and wait for all to complete
381
+ */
382
+ submitAll<T>(tasks: TaskFunction<T>[], priority?: number): Promise<T[]>;
383
+ /**
384
+ * Submit multiple tasks and process results as they complete
385
+ */
386
+ submitStream<T>(tasks: TaskFunction<T>[], priority?: number): AsyncGenerator<T, void, unknown>;
387
+ /**
388
+ * Process the next task in the queue
389
+ */
390
+ private processNext;
391
+ /**
392
+ * Wait for all currently queued tasks to complete
393
+ */
394
+ drain(): Promise<void>;
395
+ /**
396
+ * Gracefully shutdown the worker pool
397
+ * Stops accepting new tasks and waits for existing ones to complete
398
+ */
399
+ shutdown(): Promise<void>;
400
+ /**
401
+ * Get current pool statistics
402
+ */
403
+ getStats(): WorkerPoolStats;
404
+ /**
405
+ * Check if pool is idle (no active or queued tasks)
406
+ */
407
+ isIdle(): boolean;
408
+ /**
409
+ * Check if pool is shutting down
410
+ */
411
+ isClosing(): boolean;
412
+ /**
413
+ * Get the current queue size
414
+ */
415
+ queueSize(): number;
416
+ /**
417
+ * Get the number of active workers
418
+ */
419
+ activeWorkers(): number;
420
+ }
421
+ /**
422
+ * Helper to run tasks with limited concurrency
423
+ * Simpler alternative to WorkerPool for one-off batch operations
424
+ */
425
+ declare function parallelLimit<T, R>(items: T[], limit: number, fn: (item: T, index: number) => Promise<R>): Promise<R[]>;
426
+
427
+ /**
428
+ * HighConcurrencyStorage - Storage adapter optimized for high-throughput scenarios
429
+ *
430
+ * Features:
431
+ * - Write buffering with automatic batch flushing
432
+ * - Data sharding across multiple partitions
433
+ * - Parallel I/O with concurrency limits
434
+ * - Operation coalescing for duplicate writes
435
+ */
436
+ declare class HighConcurrencyStorage {
437
+ private readonly partitionManager;
438
+ private readonly writeQueue;
439
+ private readonly workerPool;
440
+ private readonly options;
441
+ constructor(dbOptions: JsonDBOptions);
442
+ /**
443
+ * Process a batch of write operations
444
+ */
445
+ private processBatch;
446
+ /**
447
+ * Process operations for a single collection
448
+ */
449
+ private processCollectionOperations;
450
+ /**
451
+ * Process operations for a single partition
452
+ */
453
+ private processPartitionOperations;
454
+ /**
455
+ * Enqueue an insert operation
456
+ */
457
+ insert<T extends Document>(collectionName: string, document: T): Promise<void>;
458
+ /**
459
+ * Enqueue a bulk insert operation
460
+ */
461
+ insertMany<T extends Document>(collectionName: string, documents: T[]): Promise<void>;
462
+ /**
463
+ * Enqueue an update operation
464
+ */
465
+ update<T extends Document>(collectionName: string, documentId: string, changes: Partial<T>): Promise<void>;
466
+ /**
467
+ * Enqueue a delete operation
468
+ */
469
+ delete(collectionName: string, documentId: string): Promise<void>;
470
+ /**
471
+ * Enqueue a clear operation
472
+ */
473
+ clear(collectionName: string): Promise<void>;
474
+ /**
475
+ * Read a document by ID (bypasses queue, reads directly)
476
+ */
477
+ findById<T extends Document>(collectionName: string, documentId: string): Promise<T | null>;
478
+ /**
479
+ * Read all documents from a collection
480
+ */
481
+ readAll<T extends Document>(collectionName: string): Promise<T[]>;
482
+ /**
483
+ * Check if a collection exists
484
+ */
485
+ exists(collectionName: string): Promise<boolean>;
486
+ /**
487
+ * Initialize a collection with empty partitions
488
+ */
489
+ initializeCollection(collectionName: string): Promise<void>;
490
+ /**
491
+ * Delete a collection and all its partitions
492
+ */
493
+ deleteCollection(collectionName: string): Promise<void>;
494
+ /**
495
+ * List all collections
496
+ */
497
+ listCollections(): Promise<string[]>;
498
+ /**
499
+ * Flush all pending writes
500
+ */
501
+ flush(): Promise<void>;
502
+ /**
503
+ * Gracefully shutdown the storage
504
+ */
505
+ shutdown(): Promise<void>;
506
+ /**
507
+ * Get pending write count
508
+ */
509
+ pendingWrites(): number;
510
+ /**
511
+ * Get worker pool statistics
512
+ */
513
+ getStats(): {
514
+ pendingWrites: number;
515
+ workerPool: WorkerPoolStats;
516
+ partitions: number;
517
+ };
518
+ /**
519
+ * Clear cache for a collection
520
+ */
521
+ clearCache(collectionName?: string): void;
522
+ }
523
+
524
+ /**
525
+ * Schema validator interface (compatible with Zod)
526
+ */
527
+ interface SchemaValidator<T> {
528
+ safeParse(data: unknown): {
529
+ success: true;
530
+ data: T;
531
+ } | {
532
+ success: false;
533
+ error: {
534
+ issues: Array<{
535
+ path: PropertyKey[];
536
+ message: string;
537
+ code?: string;
538
+ }>;
539
+ };
540
+ };
541
+ }
542
+ /**
543
+ * HighConcurrencyCollection - Collection class optimized for high-throughput scenarios
544
+ *
545
+ * Features:
546
+ * - Uses HighConcurrencyStorage for batched, partitioned writes
547
+ * - Same API as regular Collection for compatibility
548
+ * - Optimized for write-heavy workloads
549
+ */
550
+ declare class HighConcurrencyCollection<T extends Document> {
551
+ private readonly name;
552
+ private readonly storage;
553
+ private readonly queryEngine;
554
+ private readonly idGenerator;
555
+ private readonly schema?;
556
+ constructor(name: string, storage: HighConcurrencyStorage, options?: {
557
+ idGenerator?: () => string;
558
+ schema?: SchemaValidator<T>;
559
+ });
560
+ /**
561
+ * Validate a document against the schema (if defined)
562
+ */
563
+ private validate;
564
+ /**
565
+ * Insert a single document
566
+ */
567
+ insert(doc: Omit<T, '_id'> & {
568
+ _id?: string;
569
+ }): Promise<T>;
570
+ /**
571
+ * Insert a single document without duplicate check (faster)
572
+ * Use this when you're certain the ID is unique
573
+ */
574
+ insertFast(doc: Omit<T, '_id'> & {
575
+ _id?: string;
576
+ }): Promise<T>;
577
+ /**
578
+ * Insert multiple documents
579
+ */
580
+ insertMany(docs: (Omit<T, '_id'> & {
581
+ _id?: string;
582
+ })[]): Promise<T[]>;
583
+ /**
584
+ * Find documents matching a query
585
+ */
586
+ find(query?: Query<T>, options?: FindOptions<T>): Promise<T[]>;
587
+ /**
588
+ * Find a single document matching a query
589
+ */
590
+ findOne(query: Query<T>): Promise<T | null>;
591
+ /**
592
+ * Find a document by ID
593
+ */
594
+ findById(id: string): Promise<T | null>;
595
+ /**
596
+ * Count documents matching a query
597
+ */
598
+ count(query?: Query<T>): Promise<number>;
599
+ /**
600
+ * Update documents matching a query
601
+ */
602
+ update(query: Query<T>, update: UpdateOperators<T> | Partial<T>): Promise<number>;
603
+ /**
604
+ * Update a single document matching a query
605
+ */
606
+ updateOne(query: Query<T>, update: UpdateOperators<T> | Partial<T>): Promise<T | null>;
607
+ /**
608
+ * Update a document by ID
609
+ */
610
+ updateById(id: string, update: UpdateOperators<T> | Partial<T>): Promise<T | null>;
611
+ /**
612
+ * Delete documents matching a query
613
+ */
614
+ delete(query: Query<T>): Promise<number>;
615
+ /**
616
+ * Delete a single document matching a query
617
+ */
618
+ deleteOne(query: Query<T>): Promise<T | null>;
619
+ /**
620
+ * Delete a document by ID
621
+ */
622
+ deleteById(id: string): Promise<T | null>;
623
+ /**
624
+ * Get all documents in the collection
625
+ */
626
+ getAll(): Promise<T[]>;
627
+ /**
628
+ * Clear all documents in the collection
629
+ */
630
+ clear(): Promise<void>;
631
+ /**
632
+ * Drop the collection
633
+ */
634
+ drop(): Promise<void>;
635
+ /**
636
+ * Flush pending writes
637
+ */
638
+ flush(): Promise<void>;
639
+ /**
640
+ * Get the collection name
641
+ */
642
+ getName(): string;
643
+ /**
644
+ * Get update changes from update operators
645
+ */
646
+ private getUpdateChanges;
647
+ /**
648
+ * Sort documents by the specified sort options
649
+ */
650
+ private sortDocuments;
651
+ }
652
+
653
+ /**
654
+ * Collection type union for standard and high-concurrency modes
655
+ */
656
+ type AnyCollection<T extends Document> = Collection<T> | HighConcurrencyCollection<T>;
657
+ /**
658
+ * Stats returned by getStats() in high-concurrency mode
659
+ */
660
+ interface HighConcurrencyStats {
661
+ pendingWrites: number;
662
+ workerPool: WorkerPoolStats;
663
+ partitions: number;
664
+ }
665
+ /**
666
+ * JsonDB - A lightweight JSON-based database for Node.js and Electron
667
+ *
668
+ * Supports two modes:
669
+ * - Standard mode: Single-file storage per collection (default)
670
+ * - High-concurrency mode: Partitioned storage with write batching (opt-in)
671
+ */
672
+ declare class JsonDB {
673
+ private readonly options;
674
+ private readonly storage;
675
+ private readonly hcStorage;
676
+ private readonly collections;
677
+ private readonly isHighConcurrency;
678
+ private connected;
679
+ /**
680
+ * Create a new JsonDB instance
681
+ * @param options Database configuration options
682
+ */
683
+ constructor(options: JsonDBOptions);
684
+ /**
685
+ * Get the standard storage (throws if in HC mode)
686
+ */
687
+ private getStorage;
688
+ /**
689
+ * Get the high-concurrency storage (throws if in standard mode)
690
+ */
691
+ private getHCStorage;
692
+ /**
693
+ * Connect to the database (initialize storage)
694
+ */
695
+ connect(): Promise<void>;
696
+ /**
697
+ * Close the database connection
698
+ */
699
+ close(): Promise<void>;
700
+ /**
701
+ * Get or create a collection
702
+ * @param name Collection name
703
+ * @param options Collection options (including optional Zod schema)
704
+ */
705
+ collection<T extends Document>(name: string, options?: {
706
+ schema?: {
707
+ safeParse(data: unknown): {
708
+ success: true;
709
+ data: T;
710
+ } | {
711
+ success: false;
712
+ error: {
713
+ issues: Array<{
714
+ path: PropertyKey[];
715
+ message: string;
716
+ code?: string;
717
+ }>;
718
+ };
719
+ };
720
+ };
721
+ }): AnyCollection<T>;
722
+ /**
723
+ * Get or create a collection internally
724
+ */
725
+ private getOrCreateCollection;
726
+ /**
727
+ * Check if a collection exists
728
+ * @param name Collection name
729
+ */
730
+ hasCollection(name: string): Promise<boolean>;
731
+ /**
732
+ * Get list of all collection names
733
+ */
734
+ listCollections(): Promise<string[]>;
735
+ /**
736
+ * Drop a collection
737
+ * @param name Collection name
738
+ */
739
+ dropCollection(name: string): Promise<void>;
740
+ /**
741
+ * Drop the entire database (delete all collections)
742
+ */
743
+ drop(): Promise<void>;
744
+ /**
745
+ * Get the data directory path
746
+ */
747
+ getDataDir(): string;
748
+ /**
749
+ * Check if database is connected
750
+ */
751
+ isConnected(): boolean;
752
+ /**
753
+ * Check if high-concurrency mode is enabled
754
+ */
755
+ isHighConcurrencyMode(): boolean;
756
+ /**
757
+ * Get high-concurrency storage statistics
758
+ * Only available in high-concurrency mode
759
+ */
760
+ getStats(): HighConcurrencyStats | null;
761
+ /**
762
+ * Flush all pending writes
763
+ * Useful in high-concurrency mode to ensure all writes are persisted
764
+ */
765
+ flush(): Promise<void>;
766
+ }
767
+
768
+ /**
769
+ * Write operation types
770
+ */
771
+ type WriteOperation<T extends Document = Document> = {
772
+ type: 'insert';
773
+ collectionName: string;
774
+ document: T;
775
+ } | {
776
+ type: 'update';
777
+ collectionName: string;
778
+ documentId: string;
779
+ changes: Partial<T>;
780
+ } | {
781
+ type: 'delete';
782
+ collectionName: string;
783
+ documentId: string;
784
+ } | {
785
+ type: 'bulkInsert';
786
+ collectionName: string;
787
+ documents: T[];
788
+ } | {
789
+ type: 'bulkUpdate';
790
+ collectionName: string;
791
+ documentIds: string[];
792
+ changes: Partial<T>;
793
+ } | {
794
+ type: 'bulkDelete';
795
+ collectionName: string;
796
+ documentIds: string[];
797
+ } | {
798
+ type: 'clear';
799
+ collectionName: string;
800
+ } | {
801
+ type: 'fullWrite';
802
+ collectionName: string;
803
+ data: CollectionData<T>;
804
+ };
805
+ /**
806
+ * Write queue options
807
+ */
808
+ interface WriteQueueOptions {
809
+ /** Maximum writes before automatic flush (default: 1000) */
810
+ batchSize?: number;
811
+ /** Maximum time in ms before automatic flush (default: 100) */
812
+ flushInterval?: number;
813
+ /** Whether to coalesce duplicate operations (default: true) */
814
+ coalesceWrites?: boolean;
815
+ }
816
+ /**
817
+ * Batch processor callback type
818
+ */
819
+ type BatchProcessor = (operations: Map<string, WriteOperation[]>) => Promise<void>;
820
+ /**
821
+ * WriteQueue - Buffers and batches write operations for high throughput
822
+ *
823
+ * Features:
824
+ * - Batches writes to reduce I/O operations
825
+ * - Coalesces duplicate operations (e.g., multiple updates to same doc)
826
+ * - Automatic flush by count or time interval
827
+ * - Promise-based API for tracking write completion
828
+ */
829
+ declare class WriteQueue {
830
+ private readonly batchSize;
831
+ private readonly flushInterval;
832
+ private readonly coalesceWrites;
833
+ private readonly batchProcessor;
834
+ private queue;
835
+ private flushTimer;
836
+ private pendingFlush;
837
+ private totalQueued;
838
+ private isShuttingDown;
839
+ constructor(batchProcessor: BatchProcessor, options?: WriteQueueOptions);
840
+ /**
841
+ * Enqueue a write operation
842
+ * Returns a promise that resolves when the write is persisted
843
+ */
844
+ enqueue<T extends Document>(operation: WriteOperation<T>): Promise<void>;
845
+ /**
846
+ * Try to coalesce a write with existing queued operations
847
+ */
848
+ private tryCoalesce;
849
+ /**
850
+ * Schedule a flush if not already scheduled
851
+ */
852
+ private scheduleFlush;
853
+ /**
854
+ * Flush all pending writes
855
+ */
856
+ flush(): Promise<void>;
857
+ /**
858
+ * Process a batch of operations
859
+ */
860
+ private processBatch;
861
+ /**
862
+ * Gracefully shutdown the queue
863
+ * Flushes all pending writes and stops accepting new ones
864
+ */
865
+ shutdown(): Promise<void>;
866
+ /**
867
+ * Get the number of pending operations
868
+ */
869
+ pending(): number;
870
+ /**
871
+ * Check if the queue is empty
872
+ */
873
+ isEmpty(): boolean;
874
+ /**
875
+ * Check if the queue is shutting down
876
+ */
877
+ isClosing(): boolean;
878
+ }
879
+
880
+ /**
881
+ * Partition options
882
+ */
883
+ interface PartitionOptions {
884
+ /** Number of partitions (default: 16) */
885
+ partitionCount?: number;
886
+ /** Whether to pretty print JSON (default: true) */
887
+ prettyPrint?: boolean;
888
+ /** File extension (default: .json) */
889
+ fileExtension?: string;
890
+ }
891
+ /**
892
+ * Partition info for a collection
893
+ */
894
+ interface PartitionInfo {
895
+ name: string;
896
+ partitionIndex: number;
897
+ documentCount: number;
898
+ }
899
+ /**
900
+ * PartitionManager - Distributes documents across multiple files (shards)
901
+ *
902
+ * Features:
903
+ * - Consistent hashing for document distribution
904
+ * - Parallel I/O across partitions
905
+ * - Automatic partition discovery and initialization
906
+ * - Maintains per-partition caching
907
+ */
908
+ declare class PartitionManager {
909
+ private readonly dataDir;
910
+ private readonly partitionCount;
911
+ private readonly prettyPrint;
912
+ private readonly fileExtension;
913
+ private readonly cache;
914
+ private readonly locks;
915
+ constructor(dataDir: string, options?: PartitionOptions);
916
+ /**
917
+ * Ensure the data directory exists
918
+ */
919
+ private ensureDirectory;
920
+ /**
921
+ * Get partition index for a document ID using consistent hashing
922
+ */
923
+ getPartitionIndex(documentId: string): number;
924
+ /**
925
+ * Get partition file name
926
+ */
927
+ getPartitionFileName(collectionName: string, partitionIndex: number): string;
928
+ /**
929
+ * Get partition file path
930
+ */
931
+ getPartitionFilePath(collectionName: string, partitionIndex: number): string;
932
+ /**
933
+ * Get cache key for a partition
934
+ */
935
+ private getCacheKey;
936
+ /**
937
+ * Acquire lock for a partition
938
+ */
939
+ private acquireLock;
940
+ /**
941
+ * Read a single partition
942
+ */
943
+ readPartition<T extends Document>(collectionName: string, partitionIndex: number): Promise<CollectionData<T> | null>;
944
+ /**
945
+ * Write a single partition atomically
946
+ */
947
+ writePartition<T extends Document>(collectionName: string, partitionIndex: number, data: CollectionData<T>): Promise<void>;
948
+ /**
949
+ * Read all partitions for a collection in parallel
950
+ */
951
+ readAllPartitions<T extends Document>(collectionName: string): Promise<Map<number, CollectionData<T>>>;
952
+ /**
953
+ * Write multiple partitions in parallel
954
+ */
955
+ writePartitions<T extends Document>(collectionName: string, partitions: Map<number, CollectionData<T>>): Promise<void>;
956
+ /**
957
+ * Initialize empty partitions for a collection
958
+ */
959
+ initializePartitions(collectionName: string): Promise<void>;
960
+ /**
961
+ * Get partition info for a collection
962
+ */
963
+ getPartitionInfo(collectionName: string): Promise<PartitionInfo[]>;
964
+ /**
965
+ * Get all documents from all partitions
966
+ */
967
+ getAllDocuments<T extends Document>(collectionName: string): Promise<T[]>;
968
+ /**
969
+ * Find document by ID (searches correct partition)
970
+ */
971
+ findById<T extends Document>(collectionName: string, documentId: string): Promise<T | null>;
972
+ /**
973
+ * Delete all partitions for a collection
974
+ */
975
+ deleteCollection(collectionName: string): Promise<void>;
976
+ /**
977
+ * Clear cache for a collection or all collections
978
+ */
979
+ clearCache(collectionName?: string): void;
980
+ /**
981
+ * Get the number of partitions
982
+ */
983
+ getPartitionCount(): number;
984
+ /**
985
+ * List all partitioned collections
986
+ */
987
+ listCollections(): Promise<string[]>;
988
+ }
989
+
990
+ /**
991
+ * Base error class for JsonDB errors
992
+ */
993
+ declare class JsonDBError extends Error {
994
+ constructor(message: string);
995
+ }
996
+ /**
997
+ * Error thrown when a document is not found
998
+ */
999
+ declare class DocumentNotFoundError extends JsonDBError {
1000
+ constructor(collectionName: string, id?: string);
1001
+ }
1002
+ /**
1003
+ * Error thrown when a duplicate key is detected
1004
+ */
1005
+ declare class DuplicateKeyError extends JsonDBError {
1006
+ constructor(collectionName: string, id: string);
1007
+ }
1008
+ /**
1009
+ * Zod error issue type (subset of ZodIssue for type safety without importing zod)
1010
+ */
1011
+ interface SchemaIssue {
1012
+ path: PropertyKey[];
1013
+ message: string;
1014
+ code?: string;
1015
+ }
1016
+ /**
1017
+ * Error thrown when validation fails
1018
+ */
1019
+ declare class ValidationError extends JsonDBError {
1020
+ readonly collectionName: string;
1021
+ readonly issues: SchemaIssue[];
1022
+ readonly field?: string;
1023
+ readonly value?: unknown;
1024
+ constructor(collectionName: string, issues: SchemaIssue[], field?: string, value?: unknown);
1025
+ }
1026
+ /**
1027
+ * Error thrown when storage operations fail
1028
+ */
1029
+ declare class StorageError extends JsonDBError {
1030
+ readonly cause?: Error;
1031
+ constructor(message: string, cause?: Error);
1032
+ }
1033
+ /**
1034
+ * Error thrown when collection operations fail
1035
+ */
1036
+ declare class CollectionError extends JsonDBError {
1037
+ constructor(message: string);
1038
+ }
1039
+
1040
+ /**
1041
+ * Generate a unique ID using bson ObjectId
1042
+ * @param _length - Ignored, kept for backward compatibility
1043
+ * @returns A 24-character hexadecimal string
1044
+ */
1045
+ declare function generateId(_length?: number): string;
1046
+ /**
1047
+ * Check if a value is a valid ID
1048
+ * Supports both ObjectId (24 hex chars) and custom IDs (1-64 chars)
1049
+ */
1050
+ declare function isValidId(id: unknown): id is string;
1051
+
1052
+ export { Collection, type CollectionData, CollectionError, type CollectionOptions, type ComparisonOperators, type Document, DocumentNotFoundError, DuplicateKeyError, type FindOptions, HighConcurrencyCollection, type HighConcurrencyOptions, HighConcurrencyStorage, JsonDB, JsonDBError, type JsonDBOptions, PartitionManager, type Query, type QueryField, type SchemaIssue, type SchemaValidator$1 as SchemaValidator, type Sort, type SortOrder, type StorageAdapter, StorageError, type UpdateOperators, ValidationError, WorkerPool, WriteQueue, generateId, isValidId, parallelLimit };