nedb
Advanced tools
Comparing version 1.7.1 to 1.7.2
@@ -267,2 +267,5 @@ /** | ||
* Push an element to the end of an array field | ||
* Optional modifier $each instead of value to push several values | ||
* Optional modifier $slice to slice the resulting array, see https://docs.mongodb.org/manual/reference/operator/update/slice/ | ||
* Différeence with MongoDB: if $slice is specified and not $each, we act as if value is an empty array | ||
*/ | ||
@@ -275,4 +278,8 @@ lastStepModifierFunctions.$push = function (obj, field, value) { | ||
if (value !== null && typeof value === 'object' && value.$slice && value.$each === undefined) { | ||
value.$each = []; | ||
} | ||
if (value !== null && typeof value === 'object' && value.$each) { | ||
if (Object.keys(value).length > 1) { throw new Error("Can't use another field in conjunction with $each"); } | ||
if (Object.keys(value).length >= 3 || (Object.keys(value).length === 2 && value.$slice === undefined)) { throw new Error("Can only use $slice in cunjunction with $each when $push to array"); } | ||
if (!util.isArray(value.$each)) { throw new Error("$each requires an array value"); } | ||
@@ -283,2 +290,18 @@ | ||
}); | ||
if (value.$slice === undefined || typeof value.$slice !== 'number') { return; } | ||
if (value.$slice === 0) { | ||
obj[field] = []; | ||
} else { | ||
var start, end, n = obj[field].length; | ||
if (value.$slice < 0) { | ||
start = Math.max(0, n + value.$slice); | ||
end = n; | ||
} else if (value.$slice > 0) { | ||
start = 0; | ||
end = Math.min(n, value.$slice); | ||
} | ||
obj[field] = obj[field].slice(start, end); | ||
} | ||
} else { | ||
@@ -285,0 +308,0 @@ obj[field].push(value); |
{ | ||
"name": "nedb", | ||
"version": "1.7.1", | ||
"version": "1.7.2", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Louis Chatriot", |
@@ -418,3 +418,3 @@ <img src="http://i.imgur.com/9O1xHFb.png" style="width: 25%; height: 25%; float: left;"> | ||
* A new document will replace the matched docs | ||
* 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, `$unset` to delete a field and `$inc` to increment a field's value. To work on arrays, you have `$push`, `$pop`, `$addToSet`, `$pull`, and the special `$each`. 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, `$unset` to delete a field and `$inc` to increment a field's value. To work on arrays, you have `$push`, `$pop`, `$addToSet`, `$pull`, and the special `$each` and `$slice`. See examples below for the syntax. | ||
* `options` is an object with two possible parameters | ||
@@ -514,9 +514,15 @@ * `multi` (defaults to `false`) which allows the modification of several documents if set to true | ||
// $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: {$each: ['banana', 'orange'] } } }, {}, function () { | ||
db.update({ _id: 'id6' }, { $push: { fruits: { $each: ['banana', 'orange'] } } }, {}, function () { | ||
// Now the fruits array is ['apple', 'orange', 'pear', 'banana', 'orange'] | ||
}); | ||
// $slice can be used in cunjunction with $push and $each to limit the size of the resulting array. | ||
// A value of 0 will update the array to an empty array. A positive value n will keep only the n first elements | ||
// A negative value -n will keep only the last n elements. | ||
// If $slice is specified but not $each, $each is set to [] | ||
db.update({ _id: 'id6' }, { $push: { fruits: { $each: ['banana'], $slice: 2 } } }, {}, function () { | ||
// Now the fruits array is ['apple', 'orange'] | ||
}); | ||
``` | ||
@@ -523,0 +529,0 @@ |
@@ -211,3 +211,3 @@ var model = require('../lib/model') | ||
}); | ||
it('Can check if an object is a primitive or not', function () { | ||
@@ -524,2 +524,43 @@ model.isPrimitiveType(5).should.equal(true); | ||
it('Can use the $slice modifier to limit the number of array elements', function () { | ||
var obj = { arr: ['hello'] } | ||
, modified; | ||
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: 1 } } }); | ||
assert.deepEqual(modified, { arr: ['hello'] }); | ||
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: -1 } } }); | ||
assert.deepEqual(modified, { arr: ['everything'] }); | ||
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: 0 } } }); | ||
assert.deepEqual(modified, { arr: [] }); | ||
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: 2 } } }); | ||
assert.deepEqual(modified, { arr: ['hello', 'world'] }); | ||
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: -2 } } }); | ||
assert.deepEqual(modified, { arr: ['earth', 'everything'] }); | ||
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: -20 } } }); | ||
assert.deepEqual(modified, { arr: ['hello', 'world', 'earth', 'everything'] }); | ||
modified = model.modify(obj, { $push: { arr: { $each: ['world', 'earth', 'everything'], $slice: 20 } } }); | ||
assert.deepEqual(modified, { arr: ['hello', 'world', 'earth', 'everything'] }); | ||
modified = model.modify(obj, { $push: { arr: { $each: [], $slice: 1 } } }); | ||
assert.deepEqual(modified, { arr: ['hello'] }); | ||
// $each not specified, but $slice is | ||
modified = model.modify(obj, { $push: { arr: { $slice: 1 } } }); | ||
assert.deepEqual(modified, { arr: ['hello'] }); | ||
(function () { | ||
modified = model.modify(obj, { $push: { arr: { $slice: 1, unauthorized: true } } }); | ||
}).should.throw(); | ||
(function () { | ||
modified = model.modify(obj, { $push: { arr: { $each: [], unauthorized: true } } }); | ||
}).should.throw(); | ||
}); | ||
}); // End of '$push modifier' | ||
@@ -526,0 +567,0 @@ |
1280875
28769
682