Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 93 additions & 38 deletions packages/loki/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,48 +307,73 @@ export class Collection<TData extends object = object, TNested extends object =
toJSON(): Collection.Serialized {
return {
name: this.name,
unindexedSortComparator: this._unindexedSortComparator,
defaultLokiOperatorPackage: this._defaultLokiOperatorPackage,
_unindexedSortComparator: this._unindexedSortComparator,
_defaultLokiOperatorPackage: this._defaultLokiOperatorPackage,
_dynamicViews: this._dynamicViews,
uniqueNames: Object.keys(this._constraints.unique),
transforms: this._transforms as any,
rangedIndexes: this._rangedIndexes as any,
_uniqueNames: Object.keys(this._constraints.unique),
_transforms: this._transforms as any,
_rangedIndexes: this._rangedIndexes as any,
_data: this._data,
idIndex: this._idIndex,
maxId: this._maxId,
_idIndex: this._idIndex,
_maxId: this._maxId,
_dirty: this._dirty,
_nestedProperties: this._nestedProperties,
transactional: this._transactional,
asyncListeners: this._asyncListeners,
disableMeta: this._disableMeta,
disableChangesApi: this._disableChangesApi,
disableDeltaChangesApi: this._disableDeltaChangesApi,
cloneObjects: this._cloneObjects,
cloneMethod: this._cloneMethod,
changes: this._changes,
_transactional: this._transactional,
_asyncListeners: this._asyncListeners,
_disableMeta: this._disableMeta,
_disableChangesApi: this._disableChangesApi,
_disableDeltaChangesApi: this._disableDeltaChangesApi,
_cloneObjects: this._cloneObjects,
_cloneMethod: this._cloneMethod,
_changes: this._changes,
_fullTextSearch: this._fullTextSearch
};
}

static fromJSONObject(obj: Collection.Serialized, options?: Collection.DeserializeOptions) {
static migrate(obj: Collection.Serialized /*| Collection */, migrateFromVersion: string) {
if (!migrateFromVersion) {
return;
}
if (migrateFromVersion === 1.5) {
let src = obj as any as Collection.Serialized_1_5;
obj._unindexedSortComparator = src.unindexedSortComparator;
obj._defaultLokiOperatorPackage = src.defaultLokiOperatorPackage;
obj._uniqueNames = src.uniqueNames;
obj._transforms = src.transforms;
obj._rangedIndexes = src.rangedIndexes;
obj._idIndex = src.idIndex;
obj._maxId = src.maxId;
obj._transactional = src.transactional;
obj._asyncListeners = src.asyncListeners;
obj._disableMeta = src.disableMeta;
obj._disableChangesApi = src.disableChangesApi;
obj._disableDeltaChangesApi = src.disableDeltaChangesApi;
obj._cloneObjects = src.cloneObjects;
obj._cloneMethod = src.cloneMethod;
obj._changes = src.changes;
}
}

static fromJSONObject(obj: Collection.Serialized /*| Collection */, options?: Collection.DeserializeOptions, migrateFromVersion?: string) {
Collection.migrate(obj, migrateFromVersion);

// instantiate collection with options needed by constructor
let coll = new Collection<any>(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) {
Expand Down Expand Up @@ -388,16 +413,16 @@ export class Collection<TData extends object = object, TNested extends object =
}
}

coll._maxId = (obj.maxId === undefined) ? 0 : obj.maxId;
coll._idIndex = obj.idIndex;
if (obj.transforms !== undefined) {
coll._transforms = obj.transforms;
coll._maxId = (obj._maxId === undefined) ? 0 : obj._maxId;
coll._idIndex = obj._idIndex;
if (obj._transforms !== undefined) {
coll._transforms = obj._transforms;
}

// inflate rangedindexes
for (let ri in obj.rangedIndexes) {
for (let ri in obj._rangedIndexes) {
// shortcut reference to serialized meta
let sri = obj.rangedIndexes[ri];
let sri = obj._rangedIndexes[ri];
// lookup index factory function in map based on index type name
let rif = RangedIndexFactoryMap[sri.indexTypeName];
// lookup comparator function in map based on comparator name
Expand All @@ -413,9 +438,15 @@ export class Collection<TData extends object = object, TNested extends object =
coll._ensureId();

// regenerate unique indexes
if (obj.uniqueNames !== undefined) {
for (let j = 0; j < obj.uniqueNames.length; j++) {
coll.ensureUniqueIndex(obj.uniqueNames[j]);
let uniqueNames = obj._uniqueNames;
// Check if type is collection.
let maybeColl = obj as any as Collection<any>;
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]);
}
}

Expand Down Expand Up @@ -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<Transform[]>;
_rangedIndexes: RangedIndexOptions;
_data: Doc<any>[];
_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;
Expand Down
13 changes: 6 additions & 7 deletions packages/loki/src/loki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ describe("partitioning adapter", () => {
expect(db2["_collections"][1].count()).toEqual(1);
expect(db2.getCollection<User>("items").findOne({name: "gungnir"}).owner).toEqual("odin");
expect(db2.getCollection<AB>("another").findOne({a: 1}).b).toEqual(3);
// Insert still works.
db2.getCollection<AB>("another").insert({a: 2, b: 4});
expect(db2.getCollection<AB>("another").findOne({a: 2}).b).toEqual(4);
}).then(done, done.fail);
});

Expand Down