Skip to content

Commit a1e4a2d

Browse files
committed
migrate databases from 1.5 to 1.6
1 parent 54611d8 commit a1e4a2d

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

packages/loki/src/collection.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,33 @@ export class Collection<TData extends object = object, TNested extends object =
330330
};
331331
}
332332

333-
static fromJSONObject(obj: Collection.Serialized /*| Collection */, options?: Collection.DeserializeOptions) {
333+
static migrate(obj: Collection.Serialized /*| Collection */, migrateFromVersion: string) {
334+
if (!migrateFromVersion) {
335+
return;
336+
}
337+
if (migrateFromVersion === 1.5) {
338+
let src = obj as any as Collection.Serialized_1_5;
339+
obj._unindexedSortComparator = src.unindexedSortComparator;
340+
obj._defaultLokiOperatorPackage = src.defaultLokiOperatorPackage;
341+
obj._uniqueNames = src.uniqueNames;
342+
obj._transforms = src.transforms;
343+
obj._rangedIndexes = src.rangedIndexes;
344+
obj._idIndex = src.idIndex;
345+
obj._maxId = src.maxId;
346+
obj._transactional = src.transactional;
347+
obj._asyncListeners = src.asyncListeners;
348+
obj._disableMeta = src.disableMeta;
349+
obj._disableChangesApi = src.disableChangesApi;
350+
obj._disableDeltaChangesApi = src.disableDeltaChangesApi;
351+
obj._cloneObjects = src.cloneObjects;
352+
obj._cloneMethod = src.cloneMethod;
353+
obj._changes = src.changes;
354+
}
355+
}
356+
357+
static fromJSONObject(obj: Collection.Serialized /*| Collection */, options?: Collection.DeserializeOptions, migrateFromVersion?: string) {
358+
Collection.migrate(obj, migrateFromVersion);
359+
334360
// instantiate collection with options needed by constructor
335361
let coll = new Collection<any>(obj.name, {
336362
disableChangesApi: obj._disableChangesApi,
@@ -1737,6 +1763,30 @@ export namespace Collection {
17371763
_fullTextSearch: FullTextSearch;
17381764
}
17391765

1766+
export interface Serialized_1_5 {
1767+
name: string;
1768+
unindexedSortComparator: string;
1769+
defaultLokiOperatorPackage: string;
1770+
_dynamicViews: DynamicView[];
1771+
_nestedProperties: { name: string, path: string[] }[];
1772+
uniqueNames: string[];
1773+
transforms: Dict<Transform[]>;
1774+
rangedIndexes: RangedIndexOptions;
1775+
_data: Doc<any>[];
1776+
idIndex: number[];
1777+
maxId: number;
1778+
_dirty: boolean;
1779+
transactional: boolean;
1780+
asyncListeners: boolean;
1781+
disableMeta: boolean;
1782+
disableChangesApi: boolean;
1783+
disableDeltaChangesApi: boolean;
1784+
cloneObjects: boolean;
1785+
cloneMethod: CloneMethod;
1786+
changes: any;
1787+
_fullTextSearch: FullTextSearch;
1788+
}
1789+
17401790
export interface CheckIndexOptions {
17411791
randomSampling?: boolean;
17421792
randomSamplingFactor?: number;

packages/loki/src/loki.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ export class Loki extends LokiEventEmitter {
714714
this._collections = [];
715715

716716
for (let i = 0; i < len; ++i) {
717-
this._collections.push(Collection.fromJSONObject(dbObject._collections[i] as any as Collection.Serialized, options));
717+
const migrateFromVersion = (dbObject as any).databaseVersion == this.databaseVersion ? "" : (dbObject as any).databaseVersion;
718+
this._collections.push(Collection.fromJSONObject(dbObject._collections[i] as any, options, migrateFromVersion));
718719
}
719720
}
720721

0 commit comments

Comments
 (0)