@liorandb/core 1.0.12 → 1.0.14

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.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,26 @@
1
1
  import { ClassicLevel } from 'classic-level';
2
2
  import { ZodSchema } from 'zod';
3
3
 
4
+ interface IndexOptions {
5
+ unique?: boolean;
6
+ }
7
+ declare class Index {
8
+ readonly field: string;
9
+ readonly unique: boolean;
10
+ readonly dir: string;
11
+ readonly db: ClassicLevel<string, string>;
12
+ constructor(baseDir: string, field: string, options?: IndexOptions);
13
+ private normalizeKey;
14
+ private getRaw;
15
+ private setRaw;
16
+ private delRaw;
17
+ insert(doc: any): Promise<void>;
18
+ delete(doc: any): Promise<void>;
19
+ update(oldDoc: any, newDoc: any): Promise<void>;
20
+ find(value: any): Promise<string[]>;
21
+ close(): Promise<void>;
22
+ }
23
+
4
24
  interface UpdateOptions$1 {
5
25
  upsert?: boolean;
6
26
  }
@@ -9,7 +29,10 @@ declare class Collection<T = any> {
9
29
  db: ClassicLevel<string, string>;
10
30
  private queue;
11
31
  private schema?;
32
+ private indexes;
12
33
  constructor(dir: string, schema?: ZodSchema<T>);
34
+ registerIndex(index: Index): void;
35
+ getIndex(field: string): Index | undefined;
13
36
  setSchema(schema: ZodSchema<T>): void;
14
37
  private validate;
15
38
  private _enqueue;
@@ -28,6 +51,7 @@ declare class Collection<T = any> {
28
51
  deleteOne(filter: any): Promise<any>;
29
52
  deleteMany(filter: any): Promise<any>;
30
53
  countDocuments(filter?: any): Promise<any>;
54
+ private _updateIndexes;
31
55
  private _insertOne;
32
56
  private _insertMany;
33
57
  private _findOne;
@@ -49,7 +73,11 @@ type TXCommit = {
49
73
  tx: number;
50
74
  commit: true;
51
75
  };
52
- type WALEntry = TXOp | TXCommit;
76
+ type TXApplied = {
77
+ tx: number;
78
+ applied: true;
79
+ };
80
+ type WALEntry = TXOp | TXCommit | TXApplied;
53
81
  declare class DBTransactionContext {
54
82
  private db;
55
83
  readonly txId: number;
@@ -64,13 +92,18 @@ declare class LioranDB {
64
92
  manager: LioranManager;
65
93
  collections: Map<string, Collection>;
66
94
  private walPath;
95
+ private metaPath;
96
+ private meta;
67
97
  private static TX_SEQ;
68
98
  constructor(basePath: string, dbName: string, manager: LioranManager);
99
+ private loadMeta;
100
+ private saveMeta;
69
101
  writeWAL(entries: WALEntry[]): Promise<void>;
70
102
  clearWAL(): Promise<void>;
71
103
  private recoverFromWAL;
72
104
  applyTransaction(ops: TXOp[]): Promise<void>;
73
105
  collection<T = any>(name: string, schema?: ZodSchema<T>): Collection<T>;
106
+ createIndex(collection: string, field: string, options?: IndexOptions): Promise<void>;
74
107
  transaction<T>(fn: (tx: DBTransactionContext) => Promise<T>): Promise<T>;
75
108
  close(): Promise<void>;
76
109
  }
@@ -78,40 +111,23 @@ declare class LioranDB {
78
111
  interface LioranManagerOptions$1 {
79
112
  rootPath?: string;
80
113
  encryptionKey?: string | Buffer;
81
- ipc?: boolean;
82
114
  }
83
115
  declare class LioranManager {
84
116
  rootPath: string;
85
117
  openDBs: Map<string, LioranDB>;
86
118
  private closed;
87
- private ipc;
119
+ private mode;
120
+ private lockFd?;
121
+ private ipcServer?;
88
122
  constructor(options?: LioranManagerOptions$1);
123
+ private isProcessAlive;
124
+ private tryAcquireLock;
89
125
  db(name: string): Promise<LioranDB>;
90
- createDatabase(name: string): Promise<LioranDB>;
91
126
  openDatabase(name: string): Promise<LioranDB>;
92
- closeDatabase(name: string): Promise<void>;
93
- /**
94
- * Gracefully shuts down everything.
95
- * - Closes all databases
96
- * - Terminates IPC worker if running
97
- */
98
127
  closeAll(): Promise<void>;
99
- /**
100
- * Alias for closeAll() (clean public API)
101
- */
102
128
  close(): Promise<void>;
103
129
  private _registerShutdownHooks;
104
130
  private _assertOpen;
105
- renameDatabase(oldName: string, newName: string): Promise<boolean>;
106
- deleteDatabase(name: string): Promise<boolean>;
107
- dropDatabase(name: string): Promise<boolean>;
108
- listDatabases(): Promise<string[]>;
109
- getStats(): {
110
- rootPath: string;
111
- openDatabases: string[];
112
- ipc: boolean;
113
- closed: boolean;
114
- };
115
131
  }
116
132
 
117
133
  declare function getBaseDBFolder(): string;
@@ -127,5 +143,35 @@ interface UpdateOptions {
127
143
  type Query<T = any> = Partial<T> & {
128
144
  [key: string]: any;
129
145
  };
146
+ type IndexType = "hash" | "btree";
147
+ interface IndexDefinition<T = any> {
148
+ field: keyof T | string;
149
+ unique?: boolean;
150
+ sparse?: boolean;
151
+ type?: IndexType;
152
+ }
153
+ interface IndexMetadata {
154
+ field: string;
155
+ unique: boolean;
156
+ sparse: boolean;
157
+ type: IndexType;
158
+ createdAt: number;
159
+ }
160
+ interface QueryExplainPlan {
161
+ indexUsed?: string;
162
+ indexType?: IndexType;
163
+ scannedDocuments: number;
164
+ returnedDocuments: number;
165
+ executionTimeMs: number;
166
+ }
167
+ interface CollectionIndexAPI<T = any> {
168
+ createIndex(def: IndexDefinition<T>): Promise<void>;
169
+ dropIndex(field: keyof T | string): Promise<void>;
170
+ listIndexes(): Promise<IndexMetadata[]>;
171
+ rebuildIndexes(): Promise<void>;
172
+ }
173
+ interface DatabaseIndexAPI {
174
+ rebuildAllIndexes(): Promise<void>;
175
+ }
130
176
 
131
- export { LioranDB, LioranManager, type LioranManagerOptions, type Query, type UpdateOptions, getBaseDBFolder };
177
+ export { type CollectionIndexAPI, type DatabaseIndexAPI, type IndexDefinition, type IndexMetadata, type IndexType, LioranDB, LioranManager, type LioranManagerOptions, type Query, type QueryExplainPlan, type UpdateOptions, getBaseDBFolder };