Skip to content

Commit

Permalink
Merge pull request #644 from thomasconner/master
Browse files Browse the repository at this point in the history
Add option to disable meta property for documents added to a collection
  • Loading branch information
obeliskos committed Jan 20, 2018
2 parents 59aa3af + cc1cb93 commit d031e47
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 39 deletions.
66 changes: 41 additions & 25 deletions spec/generic/persistence.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ describe('testing unique index serialization', function () {
});
});

describe('testing disable meta serialization', function () {
var db;
beforeEach(function () {
db = new loki();
users = db.addCollection('users', { disableMeta: true });
});

it('should have meta disabled', function () {
var ser = db.serialize();
var reloaded = new loki();
var loaded = reloaded.loadJSON(ser);
var coll = reloaded.getCollection('users');
expect(coll.disableMeta).toEqual(true);
});
});

describe('testing destructured serialization/deserialization', function () {
it('verify default (D) destructuring works as expected', function() {
var ddb = new loki("test.db", { serializationMethod: "destructured" });
Expand Down Expand Up @@ -152,23 +168,23 @@ describe('testing destructured serialization/deserialization', function () {

// Verify db alone works correctly using NDAA format
result = ddb.serializeDestructured({
partitioned: true,
partitioned: true,
delimited : false,
partition: -1 // indicates to get serialized db container only
});

var cddb = new loki('test');
cddb.loadJSON(result);

expect(cddb.collections.length).toEqual(2);
expect(cddb.collections[0].data.length).toEqual(0);
expect(cddb.collections[1].data.length).toEqual(0);
expect(cddb.collections[0].data.length).toEqual(0);
expect(cddb.collections[1].data.length).toEqual(0);
expect(cddb.collections[0].name).toEqual(ddb.collections[0].name);
expect(cddb.collections[1].name).toEqual(ddb.collections[1].name);

// Verify collection alone works correctly using NDAA format
result = ddb.serializeDestructured({
partitioned: true,
partitioned: true,
delimited : false,
partition: 0 // collection [0] only
});
Expand All @@ -187,7 +203,7 @@ describe('testing destructured serialization/deserialization', function () {

// Verify collection alone works correctly using DA format (the other partitioned format)
result = ddb.serializeDestructured({
partitioned: true,
partitioned: true,
delimited : true,
partition: 0 // collection [0] only
});
Expand Down Expand Up @@ -233,7 +249,7 @@ describe('testing adapter functionality', function () {
expect(cdb.getCollection("testcoll").findOne({name:"test2"}).val).toEqual(101);
expect(cdb.collections[0].data.length).toEqual(3);
expect(cdb.collections[1].data.length).toEqual(1);

done();
});

Expand All @@ -248,11 +264,11 @@ describe('testing adapter functionality', function () {
coll.insert({ name : "test1", val: 100 });
coll.insert({ name : "test2", val: 101 });
coll.insert({ name : "test3", val: 102 });

ddb.saveDatabase(function(err) {
expect(memAdapter.hashStore.hasOwnProperty("test.db")).toEqual(true);
expect(memAdapter.hashStore["test.db"].savecount).toEqual(1);

ddb.deleteDatabase(function(err) {
expect(memAdapter.hashStore.hasOwnProperty("test.db")).toEqual(false);
done();
Expand Down Expand Up @@ -314,7 +330,7 @@ describe('testing adapter functionality', function () {
expect(db2.collections[1].data.length).toEqual(1);
expect(db2.getCollection("items").findOne({ name : 'gungnir'}).owner).toEqual("odin");
expect(db2.getCollection("another").findOne({ a: 1}).b).toEqual(3);

done();
});
});
Expand Down Expand Up @@ -415,7 +431,7 @@ describe('testing adapter functionality', function () {
expect(db2.collections[0].data.length).toEqual(4);
expect(db2.collections[1].data.length).toEqual(1);
expect(db2.collections[2].data.length).toEqual(0);

done();
});
});
Expand All @@ -424,24 +440,24 @@ describe('testing adapter functionality', function () {

it('verify reference adapters get db reference which is copy and serializable-safe', function(done) {
// Current loki functionality with regards to reference mode adapters:
// Since we don't use serializeReplacer on reference mode adapters, we make
// Since we don't use serializeReplacer on reference mode adapters, we make
// lightweight clone, cloning only db container and collection containers (object refs are same).

function MyFakeReferenceAdapter() { this.mode = "reference" }

MyFakeReferenceAdapter.prototype.loadDatabase = function(dbname, callback) {
expect(typeof(dbname)).toEqual("string");
expect(typeof(callback)).toEqual("function");

var result = new loki("new db");
var n1 = result.addCollection("n1");
var n2 = result.addCollection("n2");
n1.insert({m: 9, n: 8});
n2.insert({m:7, n:6});

callback(result);
};

MyFakeReferenceAdapter.prototype.exportDatabase = function(dbname, dbref, callback) {
expect(typeof(dbname)).toEqual("string");
expect(dbref.constructor.name).toEqual("Loki");
Expand All @@ -456,31 +472,31 @@ describe('testing adapter functionality', function () {
// (accidentally?) updating a document should...
dbref.collections[0].findOne({a:1}).b=3;
};

var adapter = new MyFakeReferenceAdapter();
var db = new loki("rma test", {adapter: adapter});
var c1 = db.addCollection("c1");
var c2 = db.addCollection("c2");
c1.insert({a:1, b:2});
c2.insert({a:3, b:4});

db.saveDatabase(function() {
expect(db.persistenceAdapter).toNotEqual(null);
expect(db.filename).toEqual("rma test");
expect(db.collections[0].name).toEqual("c1");
expect(db.getCollection("c1").findOne({a:1}).b).toEqual(3);
});

var db2 = new loki("other name", { adapter: adapter});
db2.loadDatabase({}, function() {
expect(db2.collections.length).toEqual(2);
expect(db2.collections[0].name).toEqual("n1");
expect(db2.collections[1].name).toEqual("n2");
expect(db2.getCollection("n1").findOne({m:9}).n).toEqual(8);

done();
});
});
});
});

describe('async adapter tests', function() {
Expand Down Expand Up @@ -568,7 +584,7 @@ describe('async adapter tests', function() {
expect(db.throttledSavePending).toEqual(true);

// we want this to fail so above they should be bootstrapping several
// saves which take about 400ms to complete.
// saves which take about 400ms to complete.
// The full drain can take one save/callback cycle longer than duration (~100ms).
db.throttledSaveDrain(function (success) {
expect(success).toEqual(false);
Expand Down Expand Up @@ -766,7 +782,7 @@ describe('async adapter tests', function() {
expect(db.throttledSavePending).toEqual(true);

// at this point, several rounds of saves should be triggered...
// a load at this scope (possibly simulating script run from different code path)
// a load at this scope (possibly simulating script run from different code path)
// should wait until any pending saves are complete, then freeze saves (queue them ) while loading,
// then re-enable saves
db.loadDatabase({}, function (success) {
Expand Down Expand Up @@ -813,7 +829,7 @@ describe('testing changesAPI', function() {
expect(result[4].name).toEqual("items");
expect(result[4].operation).toEqual("U");
expect(result[4].obj.name).toEqual("tyrfing");

done();
});
});
Expand Down
30 changes: 24 additions & 6 deletions spec/generic/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,24 @@ describe('loki', function () {
// verify batch inserts emit with objs which have meta set properly
coll.insert([ { a:2, b:3}, { a:3, b:4}]);
});

it('meta not set on returned objects', function() {
var tdb = new loki('test.db');
var coll = tdb.addCollection('tc', { disableMeta: true });

// verify single insert return objs do not have meta set
var 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
var objs = coll.insert([{ a: 2, b: 3 }, { a: 3, b: 4 }]);
expect(Array.isArray(objs));
objs.forEach(function (o) {
expect(o.hasOwnProperty('meta')).toEqual(false);
expect(o.hasOwnProperty('$loki')).toEqual(true);
});
});
});

describe('dot notation', function () {
Expand Down Expand Up @@ -415,13 +433,13 @@ describe('loki', function () {
"ids": [12, 379]
}
});

dna.insert({
"relations" : {
"ids": [111]
}
});

var results = dna.find({
'relations.ids' : { $contains: 379 }
});
Expand Down Expand Up @@ -1018,15 +1036,15 @@ describe('loki', function () {
items.insert({ name : 'gungnir', owner: 'odin', maker: 'elves' });
items.insert({ name : 'tyrfing', owner: 'svafrlami', maker: 'dwarves' });
items.insert({ name : 'draupnir', owner: 'odin', maker: 'elves' });

// simplesort without filters on prop with index should work
var results = items.chain().simplesort('name').data();
expect(results.length).toEqual(4);
expect(results[0].name).toEqual('draupnir');
expect(results[1].name).toEqual('gungnir');
expect(results[2].name).toEqual('mjolnir');
expect(results[3].name).toEqual('tyrfing');

// simplesort without filters on prop without index should work
results = items.chain().simplesort('owner').data();
expect(results.length).toEqual(4);
Expand Down Expand Up @@ -1078,7 +1096,7 @@ describe('loki', function () {
expect(result[0].maker).toEqual('elves');
expect(result[0].hasOwnProperty('$loki')).toEqual(false);
expect(result[0].hasOwnProperty('meta')).toEqual(false);
expect(result[1].maker).toEqual('elves');
expect(result[1].maker).toEqual('elves');
expect(result[1].hasOwnProperty('$loki')).toEqual(false);
expect(result[1].hasOwnProperty('meta')).toEqual(false);

Expand Down Expand Up @@ -1110,7 +1128,7 @@ describe('loki', function () {
expect(result[0].maker).toEqual('elves');
expect(result[0].hasOwnProperty('$loki')).toEqual(true);
expect(result[0].hasOwnProperty('meta')).toEqual(true);
expect(result[1].maker).toEqual('elves');
expect(result[1].maker).toEqual('elves');
expect(result[1].hasOwnProperty('$loki')).toEqual(true);
expect(result[1].hasOwnProperty('meta')).toEqual(true);
});
Expand Down
23 changes: 15 additions & 8 deletions src/lokijs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@
* @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.autoupdate=false] - use Object.observe to update objects automatically
* @param {boolean} [options.clone=false] - specify whether inserts and queries clone to/from user
Expand All @@ -1018,7 +1019,7 @@
return this.collections[i];
}
}

var collection = new Collection(name, options);
this.collections.push(collection);

Expand Down Expand Up @@ -1577,7 +1578,7 @@
for (i; i < len; i += 1) {
coll = dbObject.collections[i];

copyColl = this.addCollection(coll.name, { disableChangesApi: coll.disableChangesApi, disableDeltaChangesApi: coll.disableDeltaChangesApi });
copyColl = this.addCollection(coll.name, { disableChangesApi: coll.disableChangesApi, disableDeltaChangesApi: coll.disableDeltaChangesApi, disableMeta: coll.disableMeta });

copyColl.adaptiveBinaryIndices = coll.hasOwnProperty('adaptiveBinaryIndices')?(coll.adaptiveBinaryIndices === true): false;
copyColl.transactional = coll.transactional;
Expand Down Expand Up @@ -4484,6 +4485,9 @@
// option to make event listeners async, default is sync
this.asyncListeners = options.hasOwnProperty('asyncListeners') ? options.asyncListeners : false;

// if set to true we will not maintain a meta property for a document
this.disableMeta = options.hasOwnProperty('disableMeta') ? options.disableMeta : false;

// disable track changes
this.disableChangesApi = options.hasOwnProperty('disableChangesApi') ? options.disableChangesApi : true;

Expand Down Expand Up @@ -4640,7 +4644,7 @@
function insertMeta(obj) {
var len, idx;

if (!obj) {
if (self.disableMeta || !obj) {
return;
}

Expand Down Expand Up @@ -4670,7 +4674,7 @@
}

function updateMeta(obj) {
if (!obj) {
if (self.disableMeta || !obj) {
return;
}
obj.meta.updated = (new Date()).getTime();
Expand Down Expand Up @@ -5195,7 +5199,7 @@
// if configured to clone, do so now... otherwise just use same obj reference
var obj = this.cloneObjects ? clone(doc, this.cloneMethod) : doc;

if (typeof obj.meta === 'undefined') {
if (!this.disableMeta && typeof obj.meta === 'undefined') {
obj.meta = {
revision: 0,
created: 0
Expand Down Expand Up @@ -5283,9 +5287,9 @@
var k = 0,
len = doc.length;

// if not cloning, disable adaptive binary indices for the duration of the batch update,
// if not cloning, disable adaptive binary indices for the duration of the batch update,
// followed by lazy rebuild and re-enabling adaptive indices after batch update.
var adaptiveBatchOverride = !this.cloneObjects &&
var adaptiveBatchOverride = !this.cloneObjects &&
this.adaptiveBinaryIndices && Object.keys(this.binaryIndices).length > 0;

if (adaptiveBatchOverride) {
Expand Down Expand Up @@ -5400,7 +5404,10 @@
}

obj.$loki = this.maxId;
obj.meta.version = 0;

if (!this.disableMeta) {
obj.meta.version = 0;
}

var key, constrUnique = this.constraints.unique;
for (key in constrUnique) {
Expand Down

0 comments on commit d031e47

Please sign in to comment.