diff --git a/packages/loki/spec/generic/persistence.spec.ts b/packages/loki/spec/generic/persistence.spec.ts index e4077cc7..540aa9f1 100644 --- a/packages/loki/spec/generic/persistence.spec.ts +++ b/packages/loki/spec/generic/persistence.spec.ts @@ -59,6 +59,19 @@ describe("testing unique index serialization", () => { }); }); +describe("testing disable meta serialization", function () { + it("should have meta disabled", function () { + const db = new Loki(); + db.addCollection("users", { disableMeta: true }); + + const ser = db.serialize(); + const reloaded = new Loki(); + reloaded.loadJSON(ser); + const coll = reloaded.getCollection("users"); + expect(coll["disableMeta"]).toEqual(true); + }); +}); + describe("testing destructured serialization/deserialization", () => { it("verify default (D) destructuring works as expected", () => { const ddb = new Loki("test.db", {serializationMethod: "destructured"}); diff --git a/packages/loki/spec/generic/test.spec.ts b/packages/loki/spec/generic/test.spec.ts index d612c25c..a7178b5c 100644 --- a/packages/loki/spec/generic/test.spec.ts +++ b/packages/loki/spec/generic/test.spec.ts @@ -202,6 +202,24 @@ describe("loki", () => { users.remove(testObject as any); expect(userCount1).toEqual(users.count()); }); + + it("meta not set on returned objects", function() { + const tdb = new Loki("test.db"); + const coll = tdb.addCollection<{a: number, b: number}>("tc", { disableMeta: true }); + + // verify single insert return objs do not have meta set + const obj = coll.insert({ a: 1, b: 2 }); + expect(obj.hasOwnProperty("meta")).toEqual(false); + expect(obj.hasOwnProperty("$loki")).toEqual(true); + + // verify batch insert return objs do not have meta set + const objs = coll.insert([{ a: 2, b: 3 }, { a: 3, b: 4 }]); + expect(Array.isArray(objs)); + objs.forEach((o) => { + expect(o.hasOwnProperty("meta")).toEqual(false); + expect(o.hasOwnProperty("$loki")).toEqual(true); + }); + }); }); describe("dot notation", () => { diff --git a/packages/loki/src/collection.ts b/packages/loki/src/collection.ts index 975d9d02..0b1f56e3 100644 --- a/packages/loki/src/collection.ts +++ b/packages/loki/src/collection.ts @@ -110,6 +110,10 @@ export class Collection = {}) { super(); + + // Consistency checks. + if (options && options.disableMeta === true) { + if (options.disableChangesApi === false) { + throw new Error("disableMeta option cannot be passed as true when disableChangesApi is passed as false"); + } + if (options.disableDeltaChangesApi === false) { + throw new Error("disableMeta option cannot be passed as true when disableDeltaChangesApi is passed as false"); + } + if (typeof options.ttl === "number" && options.ttl > 0) { + throw new Error("disableMeta option cannot be passed as true when ttl is enabled"); + } + } + // the name of the collection this.name = name; // the data held by the collection @@ -233,6 +252,9 @@ export class Collectionobj).meta === undefined) { - (obj).meta = { + if (!this.disableMeta && (obj as Doc).meta === undefined) { + (obj as Doc).meta = { revision: 0, created: 0 }; @@ -944,7 +968,9 @@ export class Collection; newDoc.$loki = this.maxId; - newDoc.meta.version = 0; + if (!this.disableMeta) { + newDoc.meta.version = 0; + } const constrUnique = this.constraints.unique; for (const key in constrUnique) { @@ -1194,7 +1220,7 @@ export class Collection) { - if (!obj) { + if (this.disableMeta || !obj) { return; } obj.meta.updated = (new Date()).getTime(); @@ -2053,6 +2079,7 @@ export namespace Collection { indices?: (keyof TData)[]; adaptiveBinaryIndices?: boolean; asyncListeners?: boolean; + disableMeta?: boolean; disableChangesApi?: boolean; disableDeltaChangesApi?: boolean; clone?: boolean; @@ -2095,6 +2122,7 @@ export namespace Collection { adaptiveBinaryIndices: boolean; transactional: boolean; asyncListeners: boolean; + disableMeta: boolean; disableChangesApi: boolean; disableDeltaChangesApi: boolean; cloneObjects: boolean; diff --git a/packages/loki/src/loki.ts b/packages/loki/src/loki.ts index 7afd155f..8442e4cd 100644 --- a/packages/loki/src/loki.ts +++ b/packages/loki/src/loki.ts @@ -242,10 +242,13 @@ export class Loki extends LokiEventEmitter { * @param {array} [options.exact=[]] - array of property names to define exact constraints for * @param {array} [options.indices=[]] - array property names to define binary indexes for * @param {boolean} [options.asyncListeners=false] - whether listeners are called asynchronously + * @param {boolean} [options.disableMeta=false] - set to true to disable meta property on documents * @param {boolean} [options.disableChangesApi=true] - set to false to enable Changes Api + * @param {boolean} [options.disableDeltaChangesApi=true] - set to false to enable Delta Changes API (requires Changes API, forces cloning) * @param {boolean} [options.clone=false] - specify whether inserts and queries clone to/from user * @param {string} [options.cloneMethod=CloneMethod.DEEP] - the clone method - * @param {int} options.ttlInterval - time interval for clearing out 'aged' documents; not set by default. + * @param {number} [options.ttl=] - age of document (in ms.) before document is considered aged/stale + * @param {number} [options.ttlInterval=] - time interval for clearing out 'aged' documents; not set by default * @returns {Collection} a reference to the collection which was just added */ public addCollection(name: string, options: Collection.Options = {}): Collection {