@liorandb/core 1.0.16 → 1.0.18
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 +87 -19
- package/dist/index.js +335 -180
- package/package.json +1 -1
- package/src/core/collection.ts +150 -107
- package/src/core/database.ts +64 -56
- package/src/core/migration.store.ts +40 -0
- package/src/core/migration.ts +162 -0
- package/src/core/migration.types.ts +22 -0
- package/src/types/index.ts +107 -34
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
|
-
|
|
35
|
-
|
|
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
|
-
|
|
41
|
-
|
|
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,15 @@ declare class Collection<T = any> {
|
|
|
50
60
|
private _updateMany;
|
|
51
61
|
private _deleteOne;
|
|
52
62
|
private _deleteMany;
|
|
53
|
-
insertOne(doc: any): Promise<
|
|
54
|
-
insertMany(docs: any[]): Promise<
|
|
55
|
-
find(query?: any): Promise<
|
|
56
|
-
findOne(query?: any): Promise<
|
|
57
|
-
updateOne(filter: any, update: any, options?: UpdateOptions$1): Promise<
|
|
58
|
-
updateMany(filter: any, update: any): Promise<
|
|
59
|
-
deleteOne(filter: any): Promise<
|
|
60
|
-
deleteMany(filter: any): Promise<
|
|
61
|
-
countDocuments(filter?: any): Promise<
|
|
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>;
|
|
62
72
|
}
|
|
63
73
|
|
|
64
74
|
type TXOp = {
|
|
@@ -92,15 +102,20 @@ declare class LioranDB {
|
|
|
92
102
|
private walPath;
|
|
93
103
|
private metaPath;
|
|
94
104
|
private meta;
|
|
105
|
+
private migrator;
|
|
95
106
|
private static TX_SEQ;
|
|
96
107
|
constructor(basePath: string, dbName: string, manager: LioranManager);
|
|
97
108
|
private loadMeta;
|
|
98
109
|
private saveMeta;
|
|
110
|
+
getSchemaVersion(): string;
|
|
111
|
+
setSchemaVersion(v: string): void;
|
|
112
|
+
migrate(from: string, to: string, fn: (db: LioranDB) => Promise<void>): void;
|
|
113
|
+
applyMigrations(targetVersion: string): Promise<void>;
|
|
99
114
|
writeWAL(entries: WALEntry[]): Promise<void>;
|
|
100
115
|
clearWAL(): Promise<void>;
|
|
101
116
|
private recoverFromWAL;
|
|
102
117
|
applyTransaction(ops: TXOp[]): Promise<void>;
|
|
103
|
-
collection<T = any>(name: string, schema?: ZodSchema<T
|
|
118
|
+
collection<T = any>(name: string, schema?: ZodSchema<T>, schemaVersion?: number): Collection<T>;
|
|
104
119
|
createIndex(collection: string, field: string, options?: IndexOptions): Promise<void>;
|
|
105
120
|
compactCollection(name: string): Promise<void>;
|
|
106
121
|
compactAll(): Promise<void>;
|
|
@@ -144,13 +159,23 @@ interface LioranManagerOptions {
|
|
|
144
159
|
rootPath?: string;
|
|
145
160
|
encryptionKey?: string | Buffer;
|
|
146
161
|
ipc?: boolean;
|
|
162
|
+
/**
|
|
163
|
+
* If true, database auto-applies pending migrations on startup.
|
|
164
|
+
*/
|
|
165
|
+
autoMigrate?: boolean;
|
|
147
166
|
}
|
|
148
167
|
interface UpdateOptions {
|
|
149
168
|
upsert?: boolean;
|
|
169
|
+
/**
|
|
170
|
+
* If true, returns the modified document instead of the original.
|
|
171
|
+
*/
|
|
172
|
+
returnNew?: boolean;
|
|
150
173
|
}
|
|
151
|
-
type Query<T = any> = Partial<T>
|
|
174
|
+
type Query<T = any> = Partial<T> | ({
|
|
175
|
+
[K in keyof T]?: any;
|
|
176
|
+
} & {
|
|
152
177
|
[key: string]: any;
|
|
153
|
-
};
|
|
178
|
+
});
|
|
154
179
|
type IndexType = "hash" | "btree";
|
|
155
180
|
interface IndexDefinition<T = any> {
|
|
156
181
|
field: keyof T | string;
|
|
@@ -172,6 +197,40 @@ interface QueryExplainPlan {
|
|
|
172
197
|
returnedDocuments: number;
|
|
173
198
|
executionTimeMs: number;
|
|
174
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Per-collection document schema version
|
|
202
|
+
*/
|
|
203
|
+
type SchemaVersion = number;
|
|
204
|
+
/**
|
|
205
|
+
* Collection-level migration definition
|
|
206
|
+
*/
|
|
207
|
+
interface CollectionMigration<T = any> {
|
|
208
|
+
from: SchemaVersion;
|
|
209
|
+
to: SchemaVersion;
|
|
210
|
+
migrate: (doc: any) => T;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Database-level migration definition
|
|
214
|
+
*/
|
|
215
|
+
interface DatabaseMigration {
|
|
216
|
+
from: string;
|
|
217
|
+
to: string;
|
|
218
|
+
migrate: () => Promise<void>;
|
|
219
|
+
}
|
|
220
|
+
interface CollectionOptions<T = any> {
|
|
221
|
+
/**
|
|
222
|
+
* Zod schema used for validation
|
|
223
|
+
*/
|
|
224
|
+
schema?: any;
|
|
225
|
+
/**
|
|
226
|
+
* Current document schema version
|
|
227
|
+
*/
|
|
228
|
+
schemaVersion?: SchemaVersion;
|
|
229
|
+
/**
|
|
230
|
+
* Optional migrations for automatic document upgrading
|
|
231
|
+
*/
|
|
232
|
+
migrations?: CollectionMigration<T>[];
|
|
233
|
+
}
|
|
175
234
|
interface CollectionIndexAPI<T = any> {
|
|
176
235
|
createIndex(def: IndexDefinition<T>): Promise<void>;
|
|
177
236
|
dropIndex(field: keyof T | string): Promise<void>;
|
|
@@ -181,5 +240,14 @@ interface CollectionIndexAPI<T = any> {
|
|
|
181
240
|
interface DatabaseIndexAPI {
|
|
182
241
|
rebuildAllIndexes(): Promise<void>;
|
|
183
242
|
}
|
|
243
|
+
/**
|
|
244
|
+
* Database migration coordination API
|
|
245
|
+
*/
|
|
246
|
+
interface DatabaseMigrationAPI {
|
|
247
|
+
migrate(from: string, to: string, fn: () => Promise<void>): void;
|
|
248
|
+
applyMigrations(targetVersion: string): Promise<void>;
|
|
249
|
+
getSchemaVersion(): string;
|
|
250
|
+
setSchemaVersion(version: string): void;
|
|
251
|
+
}
|
|
184
252
|
|
|
185
|
-
export { type CollectionIndexAPI, type DatabaseIndexAPI, type IndexDefinition, type IndexMetadata, type IndexType, LioranDB, LioranManager, type LioranManagerOptions, type Query, type QueryExplainPlan, type UpdateOptions, getBaseDBFolder };
|
|
253
|
+
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 };
|