@liorandb/core 1.0.17 → 1.0.19

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
@@ -21,6 +21,11 @@ declare class Index {
21
21
  close(): Promise<void>;
22
22
  }
23
23
 
24
+ interface Migration<T = any> {
25
+ from: number;
26
+ to: number;
27
+ migrate: (doc: any) => T;
28
+ }
24
29
  interface UpdateOptions$1 {
25
30
  upsert?: boolean;
26
31
  }
@@ -29,20 +34,25 @@ declare class Collection<T = any> {
29
34
  db: ClassicLevel<string, string>;
30
35
  private queue;
31
36
  private schema?;
37
+ private schemaVersion;
38
+ private migrations;
32
39
  private indexes;
33
- constructor(dir: string, schema?: ZodSchema<T>);
34
- registerIndex(index: Index): void;
35
- getIndex(field: string): Index | undefined;
36
- setSchema(schema: ZodSchema<T>): void;
40
+ constructor(dir: string, schema?: ZodSchema<T>, schemaVersion?: number);
41
+ setSchema(schema: ZodSchema<T>, version: number): void;
42
+ addMigration(migration: Migration<T>): void;
37
43
  private validate;
44
+ private migrateIfNeeded;
38
45
  private _enqueue;
39
46
  close(): Promise<void>;
40
- compact(): Promise<void>;
41
- _exec(op: string, args: any[]): Promise<any>;
47
+ registerIndex(index: Index): void;
48
+ getIndex(field: string): Index | undefined;
42
49
  private _updateIndexes;
50
+ compact(): Promise<void>;
51
+ _exec(op: string, args: any[]): Promise<number | boolean | T | T[] | null>;
43
52
  private _insertOne;
44
53
  private _insertMany;
45
54
  private _getCandidateIds;
55
+ private _readAndMigrate;
46
56
  private _find;
47
57
  private _findOne;
48
58
  private _countDocuments;
@@ -50,15 +60,46 @@ declare class Collection<T = any> {
50
60
  private _updateMany;
51
61
  private _deleteOne;
52
62
  private _deleteMany;
53
- insertOne(doc: any): Promise<any>;
54
- insertMany(docs: any[]): Promise<any>;
55
- find(query?: any): Promise<any>;
56
- findOne(query?: any): Promise<any>;
57
- updateOne(filter: any, update: any, options?: UpdateOptions$1): Promise<any>;
58
- updateMany(filter: any, update: any): Promise<any>;
59
- deleteOne(filter: any): Promise<any>;
60
- deleteMany(filter: any): Promise<any>;
61
- countDocuments(filter?: any): Promise<any>;
63
+ insertOne(doc: any): Promise<number | boolean | T | T[] | null>;
64
+ insertMany(docs: any[]): Promise<number | boolean | T | T[] | null>;
65
+ find(query?: any): Promise<number | boolean | T | T[] | null>;
66
+ findOne(query?: any): Promise<number | boolean | T | T[] | null>;
67
+ updateOne(filter: any, update: any, options?: UpdateOptions$1): Promise<number | boolean | T | T[] | null>;
68
+ updateMany(filter: any, update: any): Promise<number | boolean | T | T[] | null>;
69
+ deleteOne(filter: any): Promise<number | boolean | T | T[] | null>;
70
+ deleteMany(filter: any): Promise<number | boolean | T | T[] | null>;
71
+ countDocuments(filter?: any): Promise<number | boolean | T | T[] | null>;
72
+ }
73
+
74
+ type WALRecord = {
75
+ lsn: number;
76
+ tx: number;
77
+ type: "op";
78
+ payload: any;
79
+ } | {
80
+ lsn: number;
81
+ tx: number;
82
+ type: "commit";
83
+ } | {
84
+ lsn: number;
85
+ tx: number;
86
+ type: "applied";
87
+ };
88
+ declare class WALManager {
89
+ private walDir;
90
+ private currentGen;
91
+ private lsn;
92
+ private fd;
93
+ constructor(baseDir: string);
94
+ private walPath;
95
+ private detectLastGeneration;
96
+ private open;
97
+ private rotate;
98
+ append(record: Omit<WALRecord, "lsn">): Promise<number>;
99
+ replay(fromLSN: number, apply: (r: WALRecord) => Promise<void>): Promise<void>;
100
+ cleanup(beforeGen: number): Promise<void>;
101
+ getCurrentLSN(): number;
102
+ getCurrentGen(): number;
62
103
  }
63
104
 
64
105
  type TXOp = {
@@ -67,15 +108,6 @@ type TXOp = {
67
108
  op: string;
68
109
  args: any[];
69
110
  };
70
- type TXCommit = {
71
- tx: number;
72
- commit: true;
73
- };
74
- type TXApplied = {
75
- tx: number;
76
- applied: true;
77
- };
78
- type WALEntry = TXOp | TXCommit | TXApplied;
79
111
  declare class DBTransactionContext {
80
112
  private db;
81
113
  readonly txId: number;
@@ -89,27 +121,28 @@ declare class LioranDB {
89
121
  dbName: string;
90
122
  manager: LioranManager;
91
123
  collections: Map<string, Collection>;
92
- private walPath;
93
124
  private metaPath;
94
125
  private meta;
95
126
  private migrator;
96
127
  private static TX_SEQ;
128
+ wal: WALManager;
129
+ private checkpoint;
97
130
  constructor(basePath: string, dbName: string, manager: LioranManager);
131
+ private initialize;
132
+ private recoverFromWAL;
98
133
  private loadMeta;
99
134
  private saveMeta;
100
135
  getSchemaVersion(): string;
101
136
  setSchemaVersion(v: string): void;
102
137
  migrate(from: string, to: string, fn: (db: LioranDB) => Promise<void>): void;
103
138
  applyMigrations(targetVersion: string): Promise<void>;
104
- writeWAL(entries: WALEntry[]): Promise<void>;
105
- clearWAL(): Promise<void>;
106
- private recoverFromWAL;
107
139
  applyTransaction(ops: TXOp[]): Promise<void>;
108
- collection<T = any>(name: string, schema?: ZodSchema<T>): Collection<T>;
140
+ collection<T = any>(name: string, schema?: ZodSchema<T>, schemaVersion?: number): Collection<T>;
109
141
  createIndex(collection: string, field: string, options?: IndexOptions): Promise<void>;
110
142
  compactCollection(name: string): Promise<void>;
111
143
  compactAll(): Promise<void>;
112
144
  transaction<T>(fn: (tx: DBTransactionContext) => Promise<T>): Promise<T>;
145
+ postCommitMaintenance(): Promise<void>;
113
146
  close(): Promise<void>;
114
147
  }
115
148
 
@@ -149,13 +182,23 @@ interface LioranManagerOptions {
149
182
  rootPath?: string;
150
183
  encryptionKey?: string | Buffer;
151
184
  ipc?: boolean;
185
+ /**
186
+ * If true, database auto-applies pending migrations on startup.
187
+ */
188
+ autoMigrate?: boolean;
152
189
  }
153
190
  interface UpdateOptions {
154
191
  upsert?: boolean;
192
+ /**
193
+ * If true, returns the modified document instead of the original.
194
+ */
195
+ returnNew?: boolean;
155
196
  }
156
- type Query<T = any> = Partial<T> & {
197
+ type Query<T = any> = Partial<T> | ({
198
+ [K in keyof T]?: any;
199
+ } & {
157
200
  [key: string]: any;
158
- };
201
+ });
159
202
  type IndexType = "hash" | "btree";
160
203
  interface IndexDefinition<T = any> {
161
204
  field: keyof T | string;
@@ -177,6 +220,40 @@ interface QueryExplainPlan {
177
220
  returnedDocuments: number;
178
221
  executionTimeMs: number;
179
222
  }
223
+ /**
224
+ * Per-collection document schema version
225
+ */
226
+ type SchemaVersion = number;
227
+ /**
228
+ * Collection-level migration definition
229
+ */
230
+ interface CollectionMigration<T = any> {
231
+ from: SchemaVersion;
232
+ to: SchemaVersion;
233
+ migrate: (doc: any) => T;
234
+ }
235
+ /**
236
+ * Database-level migration definition
237
+ */
238
+ interface DatabaseMigration {
239
+ from: string;
240
+ to: string;
241
+ migrate: () => Promise<void>;
242
+ }
243
+ interface CollectionOptions<T = any> {
244
+ /**
245
+ * Zod schema used for validation
246
+ */
247
+ schema?: any;
248
+ /**
249
+ * Current document schema version
250
+ */
251
+ schemaVersion?: SchemaVersion;
252
+ /**
253
+ * Optional migrations for automatic document upgrading
254
+ */
255
+ migrations?: CollectionMigration<T>[];
256
+ }
180
257
  interface CollectionIndexAPI<T = any> {
181
258
  createIndex(def: IndexDefinition<T>): Promise<void>;
182
259
  dropIndex(field: keyof T | string): Promise<void>;
@@ -186,5 +263,14 @@ interface CollectionIndexAPI<T = any> {
186
263
  interface DatabaseIndexAPI {
187
264
  rebuildAllIndexes(): Promise<void>;
188
265
  }
266
+ /**
267
+ * Database migration coordination API
268
+ */
269
+ interface DatabaseMigrationAPI {
270
+ migrate(from: string, to: string, fn: () => Promise<void>): void;
271
+ applyMigrations(targetVersion: string): Promise<void>;
272
+ getSchemaVersion(): string;
273
+ setSchemaVersion(version: string): void;
274
+ }
189
275
 
190
- export { type CollectionIndexAPI, type DatabaseIndexAPI, type IndexDefinition, type IndexMetadata, type IndexType, LioranDB, LioranManager, type LioranManagerOptions, type Query, type QueryExplainPlan, type UpdateOptions, getBaseDBFolder };
276
+ export { type CollectionIndexAPI, type CollectionMigration, type CollectionOptions, type DatabaseIndexAPI, type DatabaseMigration, type DatabaseMigrationAPI, type IndexDefinition, type IndexMetadata, type IndexType, LioranDB, LioranManager, type LioranManagerOptions, type Query, type QueryExplainPlan, type SchemaVersion, type UpdateOptions, getBaseDBFolder };