Can now use with any nedb query

pull/2/head
Louis Chatriot 11 years ago
parent 04dd0d3a9c
commit af426c5141
  1. 8
      README.md
  2. 18
      lib/model.js
  3. 25
      test/model.test.js

@ -350,11 +350,15 @@ db.update({ _id: 'id6' }, { $addToSet: { fruits: 'apple' } }, {}, function () {
// If we had used a fruit not in the array, e.g. 'banana', it would have been added to the array
});
// $pull removes all instances of a value from an existing array
// Equality is deep-checked
// $pull removes all values matching a value or even any NeDB query from the array
db.update({ _id: 'id6' }, { $pull: { fruits: 'apple' } }, {}, function () {
// Now the fruits array is ['orange', 'pear']
});
db.update({ _id: 'id6' }, { $pull: { fruits: $in: ['apple', 'pear'] } }, {}, function () {
// Now the fruits array is ['orange']
});
// $each can be used to $push or $addToSet multiple values at once
// This example works the same way with $addToSet

@ -619,11 +619,17 @@ logicalOperators.$not = function (obj, query) {
* @param {Object} query
*/
function match (obj, query) {
var queryKeys = Object.keys(query)
, queryKey, queryValue
, i
;
var queryKeys, queryKey, queryValue, i;
// Primitive query against a primitive type
// This is a bit of a hack since we construct an object with an arbitrary key only to dereference it later
// But I don't have time for a cleaner implementation now
if (isPrimitiveType(obj) || isPrimitiveType(query)) {
return matchQueryPart({ needAKey: obj }, 'needAKey', query);
}
// Normal query
queryKeys = Object.keys(query);
for (i = 0; i < queryKeys.length; i += 1) {
queryKey = queryKeys[i];
queryValue = query[queryKey];
@ -645,8 +651,7 @@ function match (obj, query) {
*/
function matchQueryPart (obj, queryKey, queryValue) {
var objValue = getDotValue(obj, queryKey)
, i, keys, firstChars, dollarFirstChars
;
, i, keys, firstChars, dollarFirstChars;
// Check if the object value is an array treat it as an array of { obj, query }
// Where there needs to be at least one match
@ -695,6 +700,7 @@ module.exports.serialize = serialize;
module.exports.deserialize = deserialize;
module.exports.deepCopy = deepCopy;
module.exports.checkObject = checkObject;
module.exports.isPrimitiveType = isPrimitiveType;
module.exports.modify = modify;
module.exports.getDotValue = getDotValue;
module.exports.match = match;

@ -166,8 +166,19 @@ describe('Model', function () {
model.checkObject(obj);
});
it.only('Can check if an object is a primitive or not', function () {
it('Can check if an object is a primitive or not', function () {
model.isPrimitiveType(5).should.equal(true);
model.isPrimitiveType('sdsfdfs').should.equal(true);
model.isPrimitiveType(0).should.equal(true);
model.isPrimitiveType(true).should.equal(true);
model.isPrimitiveType(false).should.equal(true);
model.isPrimitiveType(new Date()).should.equal(true);
model.isPrimitiveType([]).should.equal(true);
model.isPrimitiveType([3, 'try']).should.equal(true);
model.isPrimitiveType(null).should.equal(true);
model.isPrimitiveType({}).should.equal(false);
model.isPrimitiveType({ a: 42 }).should.equal(false);
});
}); // ==== End of 'Object checking' ==== //
@ -545,7 +556,7 @@ describe('Model', function () {
}); // End of '$pop modifier'
describe.skip('$pull modifier', function () {
describe('$pull modifier', function () {
it('Can remove an element from a set', function () {
var obj = { arr: ['hello', 'world'] }
@ -593,12 +604,12 @@ describe('Model', function () {
, modified
;
// modified = model.modify(obj, { $pull: { arr: { $gte: 5 } } });
// assert.deepEqual(modified, { arr: [4, 2], other: 'yup' });
modified = model.modify(obj, { $pull: { arr: { $gte: 5 } } });
assert.deepEqual(modified, { arr: [4, 2], other: 'yup' });
// obj = { arr: [{ b: 4 }, { b: 7 }, { b: 1 }], other: 'yeah' };
// modified = model.modify(obj, { $pull: { arr: { b: { $gte: 5} } } });
// assert.deepEqual(modified, { arr: [{ b: 4 }, { b: 1 }], other: 'yeah' });
obj = { arr: [{ b: 4 }, { b: 7 }, { b: 1 }], other: 'yeah' };
modified = model.modify(obj, { $pull: { arr: { b: { $gte: 5} } } });
assert.deepEqual(modified, { arr: [{ b: 4 }, { b: 1 }], other: 'yeah' });
});
}); // End of '$pull modifier'

Loading…
Cancel
Save