diff --git a/packages/loki/src/collection.ts b/packages/loki/src/collection.ts index 016b7ce4..fd187e4e 100644 --- a/packages/loki/src/collection.ts +++ b/packages/loki/src/collection.ts @@ -307,48 +307,73 @@ export class Collection(obj.name, { - disableChangesApi: obj.disableChangesApi, - disableDeltaChangesApi: obj.disableDeltaChangesApi, - unindexedSortComparator: obj.unindexedSortComparator, - defaultLokiOperatorPackage: obj.defaultLokiOperatorPackage + disableChangesApi: obj._disableChangesApi, + disableDeltaChangesApi: obj._disableDeltaChangesApi, + unindexedSortComparator: obj._unindexedSortComparator, + defaultLokiOperatorPackage: obj._defaultLokiOperatorPackage }); - coll._transactional = obj.transactional; - coll._asyncListeners = obj.asyncListeners; - coll._disableMeta = obj.disableMeta; - coll._disableChangesApi = obj.disableChangesApi; - coll._cloneObjects = obj.cloneObjects; - coll._cloneMethod = obj.cloneMethod || "deep"; - coll._changes = obj.changes; + coll._transactional = obj._transactional; + coll._asyncListeners = obj._asyncListeners; + coll._disableMeta = obj._disableMeta; + coll._disableChangesApi = obj._disableChangesApi; + coll._cloneObjects = obj._cloneObjects; + coll._cloneMethod = obj._cloneMethod || "deep"; + coll._changes = obj._changes; coll._nestedProperties = obj._nestedProperties as any[]; - coll._rangedIndexes = obj.rangedIndexes || {}; - + coll._rangedIndexes = obj._rangedIndexes || {}; coll._dirty = (options && options.retainDirtyFlags === true) ? obj._dirty : false; function makeLoader(coll: Collection.Serialized) { @@ -388,16 +413,16 @@ export class Collection; + if (maybeColl._constraints && maybeColl._constraints.unique !== undefined) { + uniqueNames = Object.keys(maybeColl._constraints.unique); + } + if (uniqueNames) { + for (let j = 0; j < uniqueNames.length; j++) { + coll.ensureUniqueIndex(uniqueNames[j]); } } @@ -1709,6 +1740,30 @@ export namespace Collection { } export interface Serialized { + name: string; + _unindexedSortComparator: string; + _defaultLokiOperatorPackage: string; + _dynamicViews: DynamicView[]; + _nestedProperties: { name: string, path: string[] }[]; + _uniqueNames: string[]; + _transforms: Dict; + _rangedIndexes: RangedIndexOptions; + _data: Doc[]; + _idIndex: number[]; + _maxId: number; + _dirty: boolean; + _transactional: boolean; + _asyncListeners: boolean; + _disableMeta: boolean; + _disableChangesApi: boolean; + _disableDeltaChangesApi: boolean; + _cloneObjects: boolean; + _cloneMethod: CloneMethod; + _changes: any; + _fullTextSearch: FullTextSearch; + } + + export interface Serialized_1_5 { name: string; unindexedSortComparator: string; defaultLokiOperatorPackage: string; diff --git a/packages/loki/src/loki.ts b/packages/loki/src/loki.ts index 8b636c7e..349498f4 100644 --- a/packages/loki/src/loki.ts +++ b/packages/loki/src/loki.ts @@ -39,8 +39,8 @@ export class Loki extends LokiEventEmitter { // persist version of code which created the database to the database. // could use for upgrade scenarios - private databaseVersion: number = 1.5; // TODO - private engineVersion: number = 1.5; + private databaseVersion: number = 1.6; + private engineVersion: number = 1.6; public _collections: Collection[]; @@ -171,7 +171,7 @@ export class Loki extends LokiEventEmitter { }; // process the options - if (this._persistenceMethod !== undefined) { + if (options.persistenceMethod && options.persistenceMethod !== "adapter") { // check if the specified persistence method is known if (typeof(PERSISTENCE_METHODS[this._persistenceMethod]) === "function") { this._persistenceAdapter = new (PERSISTENCE_METHODS[this._persistenceMethod]); @@ -707,16 +707,15 @@ export class Loki extends LokiEventEmitter { * @param {object} options - apply or override collection level settings * @param {boolean} options.retainDirtyFlags - whether collection dirty flags will be preserved */ - public loadJSONObject(dbObject: Loki, options?: Collection.DeserializeOptions): void; - public loadJSONObject(dbObject: Loki.Serialized, options?: Collection.DeserializeOptions): void; - public loadJSONObject(dbObject: any, options: Collection.DeserializeOptions = {}): void { + public loadJSONObject(dbObject: Loki | Loki.Serialized, options?: Collection.DeserializeOptions): void { const len = dbObject._collections ? dbObject._collections.length : 0; this.filename = dbObject.filename; this._collections = []; for (let i = 0; i < len; ++i) { - this._collections.push(Collection.fromJSONObject(dbObject._collections[i], options)); + const migrateFromVersion = (dbObject as any).databaseVersion == this.databaseVersion ? "" : (dbObject as any).databaseVersion; + this._collections.push(Collection.fromJSONObject(dbObject._collections[i] as any, options, migrateFromVersion)); } } diff --git a/packages/partitioning-adapter/spec/generic/partitioning.spec.ts b/packages/partitioning-adapter/spec/generic/partitioning.spec.ts index 5b9333dd..3820ff02 100644 --- a/packages/partitioning-adapter/spec/generic/partitioning.spec.ts +++ b/packages/partitioning-adapter/spec/generic/partitioning.spec.ts @@ -74,6 +74,9 @@ describe("partitioning adapter", () => { expect(db2["_collections"][1].count()).toEqual(1); expect(db2.getCollection("items").findOne({name: "gungnir"}).owner).toEqual("odin"); expect(db2.getCollection("another").findOne({a: 1}).b).toEqual(3); + // Insert still works. + db2.getCollection("another").insert({a: 2, b: 4}); + expect(db2.getCollection("another").findOne({a: 2}).b).toEqual(4); }).then(done, done.fail); });