Comparing version 0.7.9 to 0.7.10
@@ -592,2 +592,4 @@ var fs = require('fs') | ||
module.exports = Datastore; |
@@ -242,3 +242,13 @@ /** | ||
if (!util.isArray(obj[field])) { throw "Can't $push an element on non-array values"; } | ||
obj[field].push(value); | ||
if (value !== null && typeof value === 'object' && value.$each) { | ||
if (Object.keys(value).length > 1) { throw "Can't use another field in conjunction with $each"; } | ||
if (!util.isArray(value.$each)) { throw "$each requires an array value"; } | ||
value.$each.forEach(function (v) { | ||
obj[field].push(v); | ||
}); | ||
} else { | ||
obj[field].push(value); | ||
} | ||
}; | ||
@@ -253,2 +263,4 @@ | ||
lastStepModifierFunctions.$addToSet = function (obj, field, value) { | ||
var addToSet = true; | ||
// Create the array if it doesn't exist | ||
@@ -258,3 +270,16 @@ if (!obj.hasOwnProperty(field)) { obj[field] = []; } | ||
if (!util.isArray(obj[field])) { throw "Can't $addToSet an element on non-array values"; } | ||
if (obj[field].indexOf(value) === -1) { obj[field].push(value); } | ||
if (value !== null && typeof value === 'object' && value.$each) { | ||
if (Object.keys(value).length > 1) { throw "Can't use another field in conjunction with $each"; } | ||
if (!util.isArray(value.$each)) { throw "$each requires an array value"; } | ||
value.$each.forEach(function (v) { | ||
lastStepModifierFunctions.$addToSet(obj, field, v); | ||
}); | ||
} else { | ||
obj[field].forEach(function (v) { | ||
if (compareThings(v, value) === 0) { addToSet = false; } | ||
}); | ||
if (addToSet) { obj[field].push(value); } | ||
} | ||
}; | ||
@@ -261,0 +286,0 @@ |
{ | ||
"name": "nedb", | ||
"version": "0.7.9", | ||
"version": "0.7.10", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "tldr.io", |
@@ -11,3 +11,3 @@ # NeDB (Node embedded database) | ||
I recently benchmarked NeDB against the popular client-side database <a href="http://www.taffydb.com/" target="_blank">TaffyDB</a> and <a href="https://github.com/louischatriot/taffydb-benchmark" target="_blank">NeDB is much, much faster</a>, so I'm considering porting it to browsers. Please comment on <a href="https://github.com/louischatriot/nedb/issues/23">this issue</a> if you want it. | ||
I recently benchmarked NeDB against the popular client-side database <a href="http://www.taffydb.com/" target="_blank">TaffyDB</a> and <a href="https://github.com/louischatriot/taffydb-benchmark" target="_blank">NeDB is much, much faster</a>, so I will port it to browsers. Please comment on <a href="https://github.com/louischatriot/nedb/issues/23">this issue</a> if you have any ideas/requirements. | ||
@@ -213,3 +213,3 @@ | ||
* A new document will replace the matched docs | ||
* The available modifiers are `$set` to change a field's value, `$inc` to increment a field's value and `$push`, `$pop`, `$addToSet` to work on arrays. The modifiers create the fields they need to modify if they don't exist, and you can apply them to subdocs. See examples below for the syntax | ||
* The modifiers create the fields they need to modify if they don't exist, and you can apply them to subdocs. Available field modifiers are `$set` to change a field's value and `$inc` to increment a field's value. To work on arrays, you have `$push`, `$pop`, `$addToSet`, and the special `$each`. See examples below for the syntax. | ||
* `options` is an object with two possible parameters | ||
@@ -276,3 +276,3 @@ * `multi` (defaults to `false`) which allows the modification of several documents if set to true | ||
// $push inserts new elements at the end of the array | ||
db.update({ _id: 'id6' }, { $push: { fruits: ['banana'] } }, {}, function () { | ||
db.update({ _id: 'id6' }, { $push: { fruits: 'banana' } }, {}, function () { | ||
// Now the fruits array is ['apple', 'orange', 'pear', 'banana'] | ||
@@ -288,7 +288,14 @@ }); | ||
// $addToSet adds an element to an array only if it isn't already in it | ||
// Equality is deep-checked (i.e. $addToSet will not insert an object in an array already containing the same object) | ||
// Note that it doesn't check whether the array contained duplicates before or not | ||
db.update({ _id: 'id6' }, { $addToSet: { fruits: ['apple'] } }, {}, function () { | ||
db.update({ _id: 'id6' }, { $addToSet: { fruits: 'apple' } }, {}, function () { | ||
// The fruits array didn't change | ||
// If we had used a fruit not in the array, e.g. 'banana', it would have been added to the array | ||
}); | ||
// $each can be used to $push or $addToSet multiple values at once | ||
// This example works the same way with $addToSet | ||
db.update({ _id: 'id6' }, { $push: { fruits: ['banana', 'orange'] } }, {}, function () { | ||
// Now the fruits array is ['apple', 'orange', 'pear', 'banana', 'orange'] | ||
}); | ||
``` | ||
@@ -295,0 +302,0 @@ |
@@ -347,3 +347,3 @@ var model = require('../lib/model') | ||
it('Can push an element to a non-existent field and will create the array', function () { | ||
var obj = { arr: [] } | ||
var obj = {} | ||
, modified; | ||
@@ -381,2 +381,18 @@ | ||
it('Can use the $each modifier to add multiple values to an array at once', function () { | ||
var obj = { arr: ['hello'] } | ||
, modified; | ||
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'] } } }); | ||
assert.deepEqual(modified, { arr: ['hello', 'world', 'earth', 'everything'] }); | ||
(function () { | ||
modified = model.modify(obj, { $push: { arr: { $each: 45 } } }); | ||
}).should.throw(); | ||
(function () { | ||
modified = model.modify(obj, { $push: { arr: { $each: ['world'], unauthorized: true } } }); | ||
}).should.throw(); | ||
}); | ||
}); // End of '$push modifier' | ||
@@ -415,2 +431,30 @@ | ||
it('Use deep-equality to check whether we can add a value to a set', function () { | ||
var obj = { arr: [ { b: 2 } ] } | ||
, modified; | ||
modified = model.modify(obj, { $addToSet: { arr: { b: 3 } } }); | ||
assert.deepEqual(modified, { arr: [{ b: 2 }, { b: 3 }] }); | ||
obj = { arr: [ { b: 2 } ] } | ||
modified = model.modify(obj, { $addToSet: { arr: { b: 2 } } }); | ||
assert.deepEqual(modified, { arr: [{ b: 2 }] }); | ||
}); | ||
it('Can use the $each modifier to add multiple values to a set at once', function () { | ||
var obj = { arr: ['hello'] } | ||
, modified; | ||
modified = model.modify(obj, { $addToSet: { arr: { $each: ['world', 'earth', 'hello', 'earth'] } } }); | ||
assert.deepEqual(modified, { arr: ['hello', 'world', 'earth'] }); | ||
(function () { | ||
modified = model.modify(obj, { $addToSet: { arr: { $each: 45 } } }); | ||
}).should.throw(); | ||
(function () { | ||
modified = model.modify(obj, { $addToSet: { arr: { $each: ['world'], unauthorized: true } } }); | ||
}).should.throw(); | ||
}); | ||
}); // End of '$addToSet modifier' | ||
@@ -417,0 +461,0 @@ |
222977
4744
414