Can find by date and objects

pull/2/head
Louis Chatriot 12 years ago
parent 973996fb8c
commit 8d0b4b03f6
  1. 11
      lib/model.js
  2. 41
      test/db.test.js
  3. 2
      test/model.test.js

@ -265,15 +265,6 @@ function areThingsEqual (a, b) {
} }
/**
* Compare two things. That can be any serialized data: strings, booleans, numbers, dates, null, undefined, objects
* Returns -1 if a is less than b
* 0 if equal
* 1 if greater
*/
/** /**
* Test for field equality * Test for field equality
* @param {Object} obj The model to check * @param {Object} obj The model to check
@ -286,7 +277,7 @@ matcherFunctions.$eq = function (obj, field, value) {
if (!obj) { return false; } // field cannot be empty here so that means there is no match if (!obj) { return false; } // field cannot be empty here so that means there is no match
if (fieldParts.length === 1) { if (fieldParts.length === 1) {
return obj[fieldParts[0]] === value; // TODO: check for real equality, incl. deep object equality return areThingsEqual(obj[fieldParts[0]], value);
} else { } else {
return matcherFunctions.$eq(obj[fieldParts[0]], fieldParts.slice(1), value); return matcherFunctions.$eq(obj[fieldParts[0]], fieldParts.slice(1), value);
} }

@ -233,28 +233,57 @@ describe('Database', function () {
], done); ], done);
}); });
it('Can find dates and objects', function (done) { it('Can find dates and objects (non JS-native types)', function (done) {
var now = new Date(); var date1 = new Date(1234543)
, date2 = new Date(9999)
;
d.insert({ now: date1, sth: { name: 'nedb' } }, function () {
d.findOne({ now: date1 }, function (err, doc) {
assert.isNull(err);
doc.sth.name.should.equal('nedb');
d.findOne({ now: date2 }, function (err, doc) {
assert.isNull(err);
assert.isNull(doc);
d.findOne({ sth: { name: 'nedb' } }, function (err, doc) {
assert.isNull(err);
doc.sth.name.should.equal('nedb');
d.findOne({ sth: { name: 'other' } }, function (err, doc) {
assert.isNull(err);
assert.isNull(doc);
d.insert({ now: now, sth: { name: 'nedb' } }, function () {
d.findOne({ now: now }, function (err, doc) {
//console.log(doc);
// TODO
done(); done();
}); });
}); });
}); });
});
});
});
it('Can use dot-notation to query subfields', function (done) { it('Can use dot-notation to query subfields', function (done) {
d.insert({ greeting: { english: 'hello' } }, function () { d.insert({ greeting: { english: 'hello' } }, function () {
d.findOne({ "greeting.english": 'hello' }, function (err, doc) { d.findOne({ "greeting.english": 'hello' }, function (err, doc) {
assert.isNull(err);
doc.greeting.english.should.equal('hello'); doc.greeting.english.should.equal('hello');
d.findOne({ "greeting.english": 'hellooo' }, function (err, doc) {
assert.isNull(err);
assert.isNull(doc);
d.findOne({ "greeting.englis": 'hello' }, function (err, doc) {
assert.isNull(err);
assert.isNull(doc);
done(); done();
}); });
}); });
}); });
});
});
}); // ==== End of 'Find' ==== // }); // ==== End of 'Find' ==== //

@ -330,7 +330,7 @@ describe('Model', function () {
describe('Finding documents', function () { describe('Finding documents', function () {
describe.only('Comparing things', function () { describe('Comparing things', function () {
it('Two things of different types cannot be equal, two identical native things are equal', function () { it('Two things of different types cannot be equal, two identical native things are equal', function () {
var toTest = [null, undefined, 'somestring', 42, true, new Date(72998322), { hello: 'world' }] var toTest = [null, undefined, 'somestring', 42, true, new Date(72998322), { hello: 'world' }]

Loading…
Cancel
Save