Skip to content

Commit 54611d8

Browse files
committed
change serialization of collection to be consistent with a collection object in fromJSONObject
1 parent ccc4552 commit 54611d8

File tree

3 files changed

+65
-59
lines changed

3 files changed

+65
-59
lines changed

packages/loki/src/collection.ts

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -307,48 +307,47 @@ export class Collection<TData extends object = object, TNested extends object =
307307
toJSON(): Collection.Serialized {
308308
return {
309309
name: this.name,
310-
unindexedSortComparator: this._unindexedSortComparator,
311-
defaultLokiOperatorPackage: this._defaultLokiOperatorPackage,
310+
_unindexedSortComparator: this._unindexedSortComparator,
311+
_defaultLokiOperatorPackage: this._defaultLokiOperatorPackage,
312312
_dynamicViews: this._dynamicViews,
313-
uniqueNames: Object.keys(this._constraints.unique),
314-
transforms: this._transforms as any,
315-
rangedIndexes: this._rangedIndexes as any,
313+
_uniqueNames: Object.keys(this._constraints.unique),
314+
_transforms: this._transforms as any,
315+
_rangedIndexes: this._rangedIndexes as any,
316316
_data: this._data,
317-
idIndex: this._idIndex,
318-
maxId: this._maxId,
317+
_idIndex: this._idIndex,
318+
_maxId: this._maxId,
319319
_dirty: this._dirty,
320320
_nestedProperties: this._nestedProperties,
321-
transactional: this._transactional,
322-
asyncListeners: this._asyncListeners,
323-
disableMeta: this._disableMeta,
324-
disableChangesApi: this._disableChangesApi,
325-
disableDeltaChangesApi: this._disableDeltaChangesApi,
326-
cloneObjects: this._cloneObjects,
327-
cloneMethod: this._cloneMethod,
328-
changes: this._changes,
321+
_transactional: this._transactional,
322+
_asyncListeners: this._asyncListeners,
323+
_disableMeta: this._disableMeta,
324+
_disableChangesApi: this._disableChangesApi,
325+
_disableDeltaChangesApi: this._disableDeltaChangesApi,
326+
_cloneObjects: this._cloneObjects,
327+
_cloneMethod: this._cloneMethod,
328+
_changes: this._changes,
329329
_fullTextSearch: this._fullTextSearch
330330
};
331331
}
332332

333-
static fromJSONObject(obj: Collection.Serialized, options?: Collection.DeserializeOptions) {
333+
static fromJSONObject(obj: Collection.Serialized /*| Collection */, options?: Collection.DeserializeOptions) {
334334
// instantiate collection with options needed by constructor
335335
let coll = new Collection<any>(obj.name, {
336-
disableChangesApi: obj.disableChangesApi,
337-
disableDeltaChangesApi: obj.disableDeltaChangesApi,
338-
unindexedSortComparator: obj.unindexedSortComparator,
339-
defaultLokiOperatorPackage: obj.defaultLokiOperatorPackage
336+
disableChangesApi: obj._disableChangesApi,
337+
disableDeltaChangesApi: obj._disableDeltaChangesApi,
338+
unindexedSortComparator: obj._unindexedSortComparator,
339+
defaultLokiOperatorPackage: obj._defaultLokiOperatorPackage
340340
});
341341

342-
coll._transactional = obj.transactional;
343-
coll._asyncListeners = obj.asyncListeners;
344-
coll._disableMeta = obj.disableMeta;
345-
coll._disableChangesApi = obj.disableChangesApi;
346-
coll._cloneObjects = obj.cloneObjects;
347-
coll._cloneMethod = obj.cloneMethod || "deep";
348-
coll._changes = obj.changes;
342+
coll._transactional = obj._transactional;
343+
coll._asyncListeners = obj._asyncListeners;
344+
coll._disableMeta = obj._disableMeta;
345+
coll._disableChangesApi = obj._disableChangesApi;
346+
coll._cloneObjects = obj._cloneObjects;
347+
coll._cloneMethod = obj._cloneMethod || "deep";
348+
coll._changes = obj._changes;
349349
coll._nestedProperties = obj._nestedProperties as any[];
350-
coll._rangedIndexes = obj.rangedIndexes || {};
351-
350+
coll._rangedIndexes = obj._rangedIndexes || {};
352351
coll._dirty = (options && options.retainDirtyFlags === true) ? obj._dirty : false;
353352

354353
function makeLoader(coll: Collection.Serialized) {
@@ -388,16 +387,16 @@ export class Collection<TData extends object = object, TNested extends object =
388387
}
389388
}
390389

391-
coll._maxId = (obj.maxId === undefined) ? 0 : obj.maxId;
392-
coll._idIndex = obj.idIndex;
393-
if (obj.transforms !== undefined) {
394-
coll._transforms = obj.transforms;
390+
coll._maxId = (obj._maxId === undefined) ? 0 : obj._maxId;
391+
coll._idIndex = obj._idIndex;
392+
if (obj._transforms !== undefined) {
393+
coll._transforms = obj._transforms;
395394
}
396395

397396
// inflate rangedindexes
398-
for (let ri in obj.rangedIndexes) {
397+
for (let ri in obj._rangedIndexes) {
399398
// shortcut reference to serialized meta
400-
let sri = obj.rangedIndexes[ri];
399+
let sri = obj._rangedIndexes[ri];
401400
// lookup index factory function in map based on index type name
402401
let rif = RangedIndexFactoryMap[sri.indexTypeName];
403402
// lookup comparator function in map based on comparator name
@@ -413,9 +412,15 @@ export class Collection<TData extends object = object, TNested extends object =
413412
coll._ensureId();
414413

415414
// regenerate unique indexes
416-
if (obj.uniqueNames !== undefined) {
417-
for (let j = 0; j < obj.uniqueNames.length; j++) {
418-
coll.ensureUniqueIndex(obj.uniqueNames[j]);
415+
let uniqueNames = obj._uniqueNames;
416+
// Check if type is collection.
417+
let maybeColl = obj as any as Collection<any>;
418+
if (maybeColl._constraints && maybeColl._constraints.unique !== undefined) {
419+
uniqueNames = Object.keys(maybeColl._constraints.unique);
420+
}
421+
if (uniqueNames) {
422+
for (let j = 0; j < uniqueNames.length; j++) {
423+
coll.ensureUniqueIndex(uniqueNames[j]);
419424
}
420425
}
421426

@@ -1710,25 +1715,25 @@ export namespace Collection {
17101715

17111716
export interface Serialized {
17121717
name: string;
1713-
unindexedSortComparator: string;
1714-
defaultLokiOperatorPackage: string;
1718+
_unindexedSortComparator: string;
1719+
_defaultLokiOperatorPackage: string;
17151720
_dynamicViews: DynamicView[];
17161721
_nestedProperties: { name: string, path: string[] }[];
1717-
uniqueNames: string[];
1718-
transforms: Dict<Transform[]>;
1719-
rangedIndexes: RangedIndexOptions;
1722+
_uniqueNames: string[];
1723+
_transforms: Dict<Transform[]>;
1724+
_rangedIndexes: RangedIndexOptions;
17201725
_data: Doc<any>[];
1721-
idIndex: number[];
1722-
maxId: number;
1726+
_idIndex: number[];
1727+
_maxId: number;
17231728
_dirty: boolean;
1724-
transactional: boolean;
1725-
asyncListeners: boolean;
1726-
disableMeta: boolean;
1727-
disableChangesApi: boolean;
1728-
disableDeltaChangesApi: boolean;
1729-
cloneObjects: boolean;
1730-
cloneMethod: CloneMethod;
1731-
changes: any;
1729+
_transactional: boolean;
1730+
_asyncListeners: boolean;
1731+
_disableMeta: boolean;
1732+
_disableChangesApi: boolean;
1733+
_disableDeltaChangesApi: boolean;
1734+
_cloneObjects: boolean;
1735+
_cloneMethod: CloneMethod;
1736+
_changes: any;
17321737
_fullTextSearch: FullTextSearch;
17331738
}
17341739

packages/loki/src/loki.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export class Loki extends LokiEventEmitter {
3939

4040
// persist version of code which created the database to the database.
4141
// could use for upgrade scenarios
42-
private databaseVersion: number = 1.5; // TODO
43-
private engineVersion: number = 1.5;
42+
private databaseVersion: number = 1.6;
43+
private engineVersion: number = 1.6;
4444

4545
public _collections: Collection[];
4646

@@ -707,16 +707,14 @@ export class Loki extends LokiEventEmitter {
707707
* @param {object} options - apply or override collection level settings
708708
* @param {boolean} options.retainDirtyFlags - whether collection dirty flags will be preserved
709709
*/
710-
public loadJSONObject(dbObject: Loki, options?: Collection.DeserializeOptions): void;
711-
public loadJSONObject(dbObject: Loki.Serialized, options?: Collection.DeserializeOptions): void;
712-
public loadJSONObject(dbObject: any, options: Collection.DeserializeOptions = {}): void {
710+
public loadJSONObject(dbObject: Loki | Loki.Serialized, options?: Collection.DeserializeOptions): void {
713711
const len = dbObject._collections ? dbObject._collections.length : 0;
714712

715713
this.filename = dbObject.filename;
716714
this._collections = [];
717715

718716
for (let i = 0; i < len; ++i) {
719-
this._collections.push(Collection.fromJSONObject(dbObject._collections[i], options));
717+
this._collections.push(Collection.fromJSONObject(dbObject._collections[i] as any as Collection.Serialized, options));
720718
}
721719
}
722720

packages/partitioning-adapter/spec/generic/partitioning.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ describe("partitioning adapter", () => {
7474
expect(db2["_collections"][1].count()).toEqual(1);
7575
expect(db2.getCollection<User>("items").findOne({name: "gungnir"}).owner).toEqual("odin");
7676
expect(db2.getCollection<AB>("another").findOne({a: 1}).b).toEqual(3);
77+
// Insert still works.
78+
db2.getCollection<AB>("another").insert({a: 2, b: 4});
79+
expect(db2.getCollection<AB>("another").findOne({a: 2}).b).toEqual(4);
7780
}).then(done, done.fail);
7881
});
7982

0 commit comments

Comments
 (0)