osmos-odm
Advanced tools
Comparing version 1.4.9 to 1.4.10
@@ -9,3 +9,3 @@ # Models | ||
var Schema = Osmos.Schema; | ||
Osmos.registerDriverInstance('db', new Osmos.drivers.memory()); | ||
@@ -16,15 +16,15 @@ | ||
}); | ||
var model = new Model('UserName', schema, 'users', 'db'); | ||
function callback(err, document) { | ||
// Do something with the document | ||
} | ||
model.get('123123', callback); // By primary key | ||
model.findOne({ name : 'marco' }, callback); // Find one document | ||
model.find({ name : /^m.+/ }, callback); // Find all documents that match | ||
model.create(callback); // Create a new document | ||
## Creating a model | ||
@@ -75,3 +75,3 @@ | ||
Model.delete(spec, function callback(err, count)); | ||
On a successful return, `count` should contain the number of rows affected by the deletion operation. | ||
@@ -92,2 +92,8 @@ | ||
## Counting number of documents | ||
To get a count of documents, you can use the `count` method: | ||
Model.count(spec, function(err, count) {}); | ||
## Class methods | ||
@@ -131,3 +137,3 @@ | ||
}, | ||
get: function(value) { | ||
@@ -155,3 +161,3 @@ return new Date(value); | ||
doc.__raw__.expires = Number.MAX_VALUE; | ||
cb(null); | ||
@@ -158,0 +164,0 @@ }); |
@@ -55,3 +55,3 @@ 'use strict'; | ||
this.client.index( | ||
payload, | ||
payload, | ||
@@ -89,3 +89,3 @@ function(err, result) { | ||
var unset = {}; | ||
Object.keys(changes.value).forEach(function(key) { | ||
@@ -104,5 +104,5 @@ var val = data[key]; | ||
}); | ||
var primaryKey; | ||
if (diff[document.schema.primaryKey]) { | ||
@@ -113,3 +113,3 @@ primaryKey = document.__originalData__[document.schema.primaryKey]; | ||
} | ||
var payload = { | ||
@@ -174,2 +174,12 @@ index: self.index, | ||
count: function count(model, spec, callback) { | ||
return this.find(model, spec, function(err, records) { | ||
if(err) { | ||
callback(err); | ||
} else { | ||
callback(null, records.length); | ||
} | ||
}); | ||
}, | ||
find : function find(model, spec, callback) { | ||
@@ -219,3 +229,3 @@ spec.index = this.index; | ||
callback( | ||
null, | ||
null, | ||
{ | ||
@@ -233,2 +243,2 @@ docs: records, | ||
module.exports = Driver; | ||
module.exports = Driver; |
@@ -12,3 +12,3 @@ 'use strict'; | ||
var value = spec[key]; | ||
if (typeof value === 'number') { | ||
@@ -23,7 +23,7 @@ return Number(target[key]) === spec[key]; | ||
Driver.prototype = { | ||
create : function create(model, callback) { | ||
callback(null, {}); | ||
}, | ||
get : function get(model, primaryKey, callback) { | ||
@@ -36,11 +36,11 @@ if (this.data[primaryKey]) { | ||
}, | ||
post : function post(document, data, callback) { | ||
if (document.primaryKey) return callback(new OsmosError('You cannot call post on an object that already has a primary key. Clone it first.')); | ||
var primaryKey = crypto.randomBytes(20).toString('hex'); | ||
document.primaryKey = primaryKey; | ||
data._primaryKey = primaryKey; | ||
this.data[primaryKey] = data; | ||
@@ -50,31 +50,31 @@ | ||
}, | ||
put : function put(document, data, callback) { | ||
var primaryKey = document.primaryKey; | ||
if (!this.data[primaryKey]) return callback(null, undefined); | ||
this.data[primaryKey] = data; | ||
callback(null); | ||
}, | ||
del : function deleteRecord(model, spec, callback) { | ||
var _this = this; | ||
this.find(model.bucket, spec, function(err, results) { | ||
if (err) return callback(err); | ||
results.forEach(function(result) { | ||
delete _this.data[result._primaryKey]; | ||
}, _this); | ||
callback(null, results.length); | ||
}); | ||
}, | ||
findOne : function findOne(model, spec, callback) { | ||
var result = null; | ||
var _this = this; | ||
Object.keys(this.data).every(function(key) { | ||
@@ -87,15 +87,15 @@ var record = _this.data[key]; | ||
} | ||
return true; | ||
}); | ||
callback(null, result); | ||
}, | ||
find : function find(model, spec, callback) { | ||
var result = []; | ||
Object.keys(this.data).forEach(function(key) { | ||
var record = this.data[key]; | ||
if (matchSpec(record, spec)) { | ||
@@ -105,8 +105,18 @@ result.push(record); | ||
}, this); | ||
callback(null, result); | ||
}, | ||
count: function count(model, spec, callback) { | ||
var count; | ||
this.find(this, spec, function(err, docs) { | ||
if(err) { | ||
callback(err); | ||
} else { | ||
callback(null, docs.length); | ||
} | ||
}); | ||
} | ||
}; | ||
module.exports = Driver; | ||
module.exports = Driver; |
@@ -83,3 +83,3 @@ 'use strict'; | ||
var unset = {}; | ||
Object.keys(changes.value).forEach(function(key) { | ||
@@ -98,5 +98,5 @@ var val = data[key]; | ||
}); | ||
var primaryKey; | ||
if (diff[document.schema.primaryKey]) { | ||
@@ -107,3 +107,3 @@ primaryKey = document.__originalData__[document.schema.primaryKey]; | ||
} | ||
var payload = {}; | ||
@@ -149,2 +149,8 @@ | ||
count: function countRecords(model, spec, cb) { | ||
return this.database.collection(model.bucket).find(spec).count(function(err, count) { | ||
cb(err, count); | ||
}); | ||
}, | ||
findOne : function findOne(model, spec, cb) { | ||
@@ -182,3 +188,3 @@ this.database.collection(model.bucket).findOne(spec, cb); | ||
if (err) return cb(err); | ||
cb(err, result); | ||
@@ -191,2 +197,2 @@ } | ||
module.exports = Driver; | ||
module.exports = Driver; |
@@ -270,2 +270,45 @@ 'use strict'; | ||
Driver.prototype.count = function countRecords(model, spec, cb) { | ||
var self = this; | ||
async.waterfall([ | ||
function(next) { | ||
self.pool.getConnection(function(err, db) { | ||
next(err, db); | ||
}); | ||
}, | ||
function(db, next) { | ||
var q = 'SELECT COUNT(*) AS count FROM ?? WHERE '; | ||
var data = []; | ||
data.push(model.bucket); | ||
var searchVals = []; | ||
Object.keys(spec).forEach(function(key) { | ||
var q = {}; | ||
q[key] = spec[key]; | ||
searchVals.push('?'); | ||
data.push(q) | ||
}); | ||
q += searchVals.join(' AND ') + ';'; | ||
db.query(q, data, function(err, result) { | ||
db.release(); | ||
next(err, result); | ||
}); | ||
} | ||
], function(err, result) { | ||
if(err) { | ||
cb(err); | ||
} else { | ||
var count = 0; | ||
if(result.length) { | ||
count = result[0].count; | ||
} | ||
cb(null, count); | ||
} | ||
}); | ||
}; | ||
Driver.prototype.find = function fineMySQLRecords(model, spec, cb) { | ||
@@ -272,0 +315,0 @@ var self = this; |
@@ -51,5 +51,5 @@ 'use strict'; | ||
if (changes.changed === 'equal') return callback(null); // Nothing to update. | ||
var diff = {}; | ||
Object.keys(changes.value).forEach(function(key) { | ||
@@ -60,5 +60,5 @@ if (changes.value[key].changed !== 'equal') { | ||
}); | ||
var primaryKey; | ||
if (diff[document.schema.primaryKey]) { | ||
@@ -69,3 +69,3 @@ primaryKey = document.__originalData__[document.schema.primaryKey]; | ||
} | ||
r | ||
@@ -106,3 +106,3 @@ .table(document.model.bucket, this.options) | ||
var table = r.table(model.bucket, this.options); | ||
if (typeof spec === 'function') { | ||
@@ -123,3 +123,3 @@ spec(this.connection, table, callback); | ||
var table = r.table(model.bucket, this.options); | ||
if (typeof spec === 'function') { | ||
@@ -138,4 +138,13 @@ spec(this.connection, table, callback); | ||
count: function count(model, spec, callback) { | ||
return this.find(model, spec, function(err, docs) { | ||
if(err) { | ||
callback(err); | ||
} else { | ||
callback(null, docs.length); | ||
} | ||
}); | ||
} | ||
}; | ||
module.exports = Driver; | ||
module.exports = Driver; |
@@ -33,2 +33,4 @@ 'use strict'; | ||
Model.prototype.hooks = [ | ||
'willCount', | ||
'didCount', | ||
'didCreate', | ||
@@ -314,2 +316,29 @@ 'willFind', | ||
Model.prototype.count = function(spec, done) { | ||
var args = { | ||
spec: spec, | ||
stop: false | ||
}; | ||
var self = this; | ||
this.performHookCycle( | ||
'Count', | ||
args, | ||
function(next) { | ||
if(args.stop) { | ||
next(null); | ||
} else { | ||
self.db.count(self, args.spec, function(err, count) { | ||
args.count = count; | ||
next(null); | ||
}); | ||
} | ||
}, | ||
function(err) { | ||
done(err, args.count); | ||
} | ||
); | ||
}; | ||
Model.prototype.findOne = function (spec, cb) { | ||
@@ -316,0 +345,0 @@ var self = this; |
{ | ||
"name": "osmos-odm", | ||
"version": "1.4.9", | ||
"version": "1.4.10", | ||
"author": "Marco Tabini <marcot@tabini.ca>", | ||
@@ -5,0 +5,0 @@ "description": "A general purpose, strict, simple ORM for Node.js apps", |
@@ -39,3 +39,3 @@ /*jshint expr:true*/ | ||
describe('The ElasticSearch driver', function() { | ||
before(function(done) { | ||
@@ -61,7 +61,7 @@ var driver = new ElasticSearch( | ||
model = new Model('ESPerson', schema, 'person', 'elasticsearch'); | ||
model.transformers._id = { | ||
get: function(value) { | ||
if (!value) return value; | ||
return value.toHexString ? value.toHexString() : value; | ||
@@ -75,14 +75,14 @@ } | ||
}); | ||
it('should allow creating new documents', function(done) { | ||
model.create(function(err, doc) { | ||
expect(err).not.to.be.ok; | ||
expect(doc).to.be.an('object'); | ||
expect(doc.constructor.name).to.equal('OsmosDataStoreDocument'); | ||
done(); | ||
}); | ||
}); | ||
it('should allow posting documents and reading their key', function(done) { | ||
@@ -92,10 +92,10 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
expect(doc.primaryKey).to.equal(undefined); | ||
doc.save(function(err) { | ||
expect(err).to.be.undefined; | ||
expect(doc.primaryKey).not.to.equal(undefined); | ||
done(); | ||
@@ -105,3 +105,3 @@ }); | ||
}); | ||
it('should allow putting documents and reading their key', function(done) { | ||
@@ -111,12 +111,12 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
var key = 'Test Key' + Math.random(); | ||
doc.primaryKey = key; | ||
doc.save(function(err) { | ||
expect(err).to.not.be.ok; | ||
expect(doc.primaryKey).to.equal(key); | ||
done(); | ||
@@ -126,13 +126,13 @@ }); | ||
}); | ||
it('should allow updating individual fields independently', function(done) { | ||
model.create(function(err, doc) { | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Manu'; | ||
doc.email = 'manu@example.org'; | ||
doc.save(function(err) { | ||
expect(err).to.not.be.ok; | ||
model.get(doc.primaryKey, function(err, doc2) { | ||
@@ -145,3 +145,3 @@ async.parallel( | ||
}, | ||
function(cb) { | ||
@@ -152,13 +152,13 @@ doc.email = 'joe@example.org'; | ||
], | ||
function(err) { | ||
expect(err).not.to.be.ok; | ||
model.get(doc.primaryKey, function(err, doc3) { | ||
expect(err).not.to.be.ok; | ||
expect(doc3).to.be.an('object'); | ||
expect(doc3.name).to.equal('Joe'); | ||
expect(doc3.email).to.equal('joe@example.org'); | ||
done(); | ||
@@ -168,3 +168,3 @@ }); | ||
); | ||
}); | ||
@@ -174,3 +174,3 @@ }); | ||
}); | ||
it('should allow putting and retrieving documents by their key', function(done) { | ||
@@ -180,19 +180,19 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
var key = 'Test Key' + Math.random(); | ||
doc.primaryKey = key; | ||
doc.save(function(err) { | ||
expect(err).to.not.be.ok; | ||
model.get(key, function(err, doc) { | ||
expect(err).to.not.be.ok; | ||
expect(doc).to.be.an('object'); | ||
expect(doc.constructor.name).to.equal('OsmosDataStoreDocument'); | ||
expect(doc.name).to.equal('Marco'); | ||
expect(doc.email).to.equal('marcot@tabini.ca'); | ||
done(); | ||
@@ -203,3 +203,3 @@ }); | ||
}); | ||
it('should allow deleting documents by their key', function(done) { | ||
@@ -209,6 +209,6 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
doc.save(function(err) { | ||
expect(err).to.not.be.ok; | ||
expect(doc.primaryKey).not.to.equal(undefined); | ||
@@ -218,3 +218,3 @@ | ||
expect(err).to.not.be.ok; | ||
model.get(doc.primaryKey, function(err, doc) { | ||
@@ -229,3 +229,3 @@ expect(doc).to.equal(undefined); | ||
}); | ||
it('should allow querying for individual documents', function(done) { | ||
@@ -235,3 +235,3 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
doc.save(function() { | ||
@@ -242,3 +242,3 @@ model.findOne( | ||
}, | ||
function(err, result) { | ||
@@ -256,3 +256,3 @@ expect(err).to.not.be.ok; | ||
}); | ||
it('should return multiple documents when using find()', function(done) { | ||
@@ -264,3 +264,3 @@ async.series( | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Marco'; | ||
@@ -271,3 +271,3 @@ doc.email = 'marcot@tabini.ca'; | ||
}, | ||
function(cb) { | ||
@@ -282,4 +282,14 @@ model.create(function(err, doc) { | ||
}, | ||
function(cb) { | ||
model.count({ name: 'Marco' }, function(err, count) { | ||
expect(err).to.not.exist; | ||
expect(count).to.be.equal(1); | ||
cb(); | ||
}); | ||
}, | ||
function(cb) { | ||
model.find( | ||
@@ -289,9 +299,9 @@ { | ||
}, | ||
function(err, docs) { | ||
expect(err).not.to.be.ok; | ||
expect(docs).to.be.an('array'); | ||
expect(docs.length).to.be.above(1); | ||
cb(null); | ||
@@ -302,12 +312,12 @@ } | ||
], | ||
done | ||
); | ||
}); | ||
it('should return document metadata when using findLimit()', function(done) { | ||
var email = 'marcot-' + Math.random() + '@tabini.ca'; | ||
this.timeout(15000); | ||
async.series( | ||
@@ -322,3 +332,3 @@ [ | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Marco'; | ||
@@ -333,3 +343,3 @@ doc.email = email; | ||
}, | ||
function(cb) { | ||
@@ -340,12 +350,12 @@ model.findLimit( | ||
}, | ||
0, | ||
2, | ||
function(err, result) { | ||
expect(err).not.to.be.ok; | ||
expect(result).to.be.an('object'); | ||
expect(result.count).to.equal(10); | ||
@@ -356,3 +366,3 @@ expect(result.start).to.equal(0); | ||
expect(result.docs.length).to.equal(2); | ||
cb(null); | ||
@@ -363,3 +373,3 @@ } | ||
], | ||
done | ||
@@ -371,5 +381,5 @@ ); | ||
var email = 'marcot-' + Math.random() + '@tabini.ca'; | ||
this.timeout(15000); | ||
async.series( | ||
@@ -384,3 +394,3 @@ [ | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Marco'; | ||
@@ -395,3 +405,3 @@ doc.email = email; | ||
}, | ||
function(cb) { | ||
@@ -402,12 +412,12 @@ model.findLimit( | ||
}, | ||
2, | ||
10, | ||
function(err, result) { | ||
expect(err).not.to.be.ok; | ||
expect(result).to.be.an('object'); | ||
expect(result.count).to.equal(10); | ||
@@ -418,3 +428,3 @@ expect(result.start).to.equal(2); | ||
expect(result.docs.length).to.equal(8); | ||
cb(null); | ||
@@ -425,3 +435,3 @@ } | ||
], | ||
done | ||
@@ -433,5 +443,5 @@ ); | ||
var email = 'marcot-' + Math.random() + '@tabini.ca'; | ||
this.timeout(15000); | ||
async.series( | ||
@@ -446,3 +456,3 @@ [ | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Marco'; | ||
@@ -457,3 +467,3 @@ doc.email = email; | ||
}, | ||
function(cb) { | ||
@@ -464,12 +474,12 @@ model.findLimit( | ||
}, | ||
2, | ||
10, | ||
function(err, result) { | ||
expect(err).not.to.be.ok; | ||
expect(result).to.be.an('object'); | ||
expect(result.count).to.equal(10); | ||
@@ -480,3 +490,3 @@ expect(result.start).to.equal(2); | ||
expect(result.docs.length).to.equal(8); | ||
cb(null); | ||
@@ -487,3 +497,3 @@ } | ||
], | ||
done | ||
@@ -493,2 +503,2 @@ ); | ||
}); | ||
}); |
@@ -44,17 +44,17 @@ /*jshint expr:true*/ | ||
describe('The MongoDB driver', function() { | ||
before(function(done) { | ||
MongoClient.connect('mongodb://localhost:27017/osmos', function(err, db) { | ||
expect(err).not.to.be.ok; | ||
var driver = new MongoDB(db); | ||
Osmos.drivers.register('mongoDB', driver); | ||
model = new Model('MongoPerson', schema, 'person', 'mongoDB'); | ||
model.transformers._id = { | ||
get: function(value) { | ||
if (!value) return value; | ||
return value.toHexString ? value.toHexString() : value; | ||
@@ -67,14 +67,14 @@ } | ||
}); | ||
it('should allow creating new documents', function(done) { | ||
model.create(function(err, doc) { | ||
expect(err).not.to.be.ok; | ||
expect(doc).to.be.an('object'); | ||
expect(doc.constructor.name).to.equal('OsmosDataStoreDocument'); | ||
done(); | ||
}); | ||
}); | ||
it('should allow posting documents and reading their key', function(done) { | ||
@@ -84,10 +84,10 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
expect(doc.primaryKey).to.equal(undefined); | ||
doc.save(function(err) { | ||
expect(err).to.be.undefined; | ||
expect(doc.primaryKey).not.to.equal(undefined); | ||
done(); | ||
@@ -97,3 +97,3 @@ }); | ||
}); | ||
it('should allow putting documents and reading their key', function(done) { | ||
@@ -103,12 +103,12 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
var key = new ObjectID().toHexString(); | ||
doc.primaryKey = key; | ||
doc.save(function(err) { | ||
expect(err).to.not.be.ok; | ||
expect(doc.primaryKey).to.equal(key); | ||
done(); | ||
@@ -118,13 +118,13 @@ }); | ||
}); | ||
it('should allow updating individual fields independently', function(done) { | ||
model.create(function(err, doc) { | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Manu'; | ||
doc.email = 'manu@example.org'; | ||
doc.save(function(err) { | ||
expect(err).to.not.be.ok; | ||
model.get(doc.primaryKey, function(err, doc2) { | ||
@@ -137,3 +137,3 @@ async.parallel( | ||
}, | ||
function(cb) { | ||
@@ -144,13 +144,13 @@ doc.email = 'joe@example.org'; | ||
], | ||
function(err) { | ||
expect(err).not.to.be.ok; | ||
model.get(doc.primaryKey, function(err, doc3) { | ||
expect(err).not.to.be.ok; | ||
expect(doc3).to.be.an('object'); | ||
expect(doc3.name).to.equal('Joe'); | ||
expect(doc3.email).to.equal('joe@example.org'); | ||
done(); | ||
@@ -160,3 +160,3 @@ }); | ||
); | ||
}); | ||
@@ -166,3 +166,3 @@ }); | ||
}); | ||
it('should allow putting and retrieving documents by their key', function(done) { | ||
@@ -172,19 +172,19 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
var key = new ObjectID().toHexString(); | ||
doc.primaryKey = key; | ||
doc.save(function(err) { | ||
expect(err).to.not.be.ok; | ||
model.get(key, function(err, doc) { | ||
expect(err).to.not.be.ok; | ||
expect(doc).to.be.an('object'); | ||
expect(doc.constructor.name).to.equal('OsmosDataStoreDocument'); | ||
expect(doc.name).to.equal('Marco'); | ||
expect(doc.email).to.equal('marcot@tabini.ca'); | ||
done(); | ||
@@ -195,3 +195,3 @@ }); | ||
}); | ||
it('should allow deleting documents by their key', function(done) { | ||
@@ -201,6 +201,6 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
doc.save(function(err) { | ||
expect(err).to.not.be.ok; | ||
expect(doc.primaryKey).not.to.equal(undefined); | ||
@@ -210,3 +210,3 @@ | ||
expect(err).to.not.be.ok; | ||
model.get(doc.primaryKey, function(err, doc) { | ||
@@ -221,3 +221,3 @@ expect(doc).to.equal(undefined); | ||
}); | ||
it('should allow querying for individual documents', function(done) { | ||
@@ -227,3 +227,3 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
doc.save(function() { | ||
@@ -234,3 +234,3 @@ model.findOne( | ||
}, | ||
function(err, result) { | ||
@@ -248,3 +248,3 @@ expect(err).to.not.be.ok; | ||
}); | ||
it('should allow querying for multiple documents based on secondary indices', function(done) { | ||
@@ -254,3 +254,3 @@ model.create(function(err, doc) { | ||
doc.email = 'marcot@tabini.ca'; | ||
doc.save(function() { | ||
@@ -262,3 +262,3 @@ model.find( | ||
}, | ||
function(err, result) { | ||
@@ -268,3 +268,3 @@ expect(err).to.not.be.ok; | ||
expect(result).to.be.an('array'); | ||
result.forEach(function(doc) { | ||
@@ -280,3 +280,3 @@ expect(doc.email).to.equal('marcot@tabini.ca'); | ||
}); | ||
it('should return multiple documents when using find()', function(done) { | ||
@@ -288,3 +288,3 @@ async.series( | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Marco'; | ||
@@ -295,3 +295,3 @@ doc.email = 'marcot@tabini.ca'; | ||
}, | ||
function(cb) { | ||
@@ -306,3 +306,3 @@ model.create(function(err, doc) { | ||
}, | ||
function(cb) { | ||
@@ -313,9 +313,9 @@ model.find( | ||
}, | ||
function(err, docs) { | ||
expect(err).not.to.be.ok; | ||
expect(docs).to.be.an('array'); | ||
expect(docs.length).to.be.above(1); | ||
cb(null); | ||
@@ -326,12 +326,12 @@ } | ||
], | ||
done | ||
); | ||
}); | ||
it('should return document metadata when using findLimit()', function(done) { | ||
var email = 'marcot-' + Math.random() + '@tabini.ca'; | ||
this.timeout(15000); | ||
async.series( | ||
@@ -346,3 +346,3 @@ [ | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Marco'; | ||
@@ -357,3 +357,3 @@ doc.email = email; | ||
}, | ||
function(cb) { | ||
@@ -364,12 +364,12 @@ model.findLimit( | ||
}, | ||
0, | ||
2, | ||
function(err, result) { | ||
expect(err).not.to.be.ok; | ||
expect(result).to.be.an('object'); | ||
expect(result.count).to.equal(10); | ||
@@ -380,3 +380,3 @@ expect(result.start).to.equal(0); | ||
expect(result.docs.length).to.equal(2); | ||
cb(null); | ||
@@ -387,3 +387,3 @@ } | ||
], | ||
done | ||
@@ -395,5 +395,5 @@ ); | ||
var email = 'marcot-' + Math.random() + '@tabini.ca'; | ||
this.timeout(15000); | ||
async.series( | ||
@@ -408,3 +408,3 @@ [ | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Marco'; | ||
@@ -419,3 +419,3 @@ doc.email = email; | ||
}, | ||
function(cb) { | ||
@@ -426,12 +426,12 @@ model.findLimit( | ||
}, | ||
2, | ||
10, | ||
function(err, result) { | ||
expect(err).not.to.be.ok; | ||
expect(result).to.be.an('object'); | ||
expect(result.count).to.equal(10); | ||
@@ -442,3 +442,3 @@ expect(result.start).to.equal(2); | ||
expect(result.docs.length).to.equal(8); | ||
cb(null); | ||
@@ -449,3 +449,3 @@ } | ||
], | ||
done | ||
@@ -455,64 +455,90 @@ ); | ||
it('should properly manage count with findLimit() when using a sort operation', function(done) { | ||
var email = 'marcot-' + Math.random() + '@tabini.ca'; | ||
this.timeout(15000); | ||
async.series( | ||
[ | ||
function(cb) { | ||
async.each( | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
it('should properly manage count with findLimit() when using a sort operation', function(done) { | ||
var email = 'marcot-' + Math.random() + '@tabini.ca'; | ||
function(datum, cb) { | ||
model.create(function(err, doc) { | ||
this.timeout(15000); | ||
async.series( | ||
[ | ||
function(cb) { | ||
async.each( | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
function(datum, cb) { | ||
model.create(function(err, doc) { | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Marco'; | ||
doc.email = email; | ||
doc.save(cb); | ||
}); | ||
}, | ||
cb | ||
); | ||
}, | ||
function(cb) { | ||
model.findLimit( | ||
{ | ||
$query : { | ||
email: email | ||
}, | ||
$orderBy: { | ||
_id: -1 | ||
} | ||
}, | ||
2, | ||
10, | ||
function(err, result) { | ||
expect(err).not.to.be.ok; | ||
doc.name = 'Marco'; | ||
doc.email = email; | ||
doc.save(cb); | ||
}); | ||
}, | ||
cb | ||
); | ||
expect(result).to.be.an('object'); | ||
expect(result.count).to.equal(10); | ||
expect(result.start).to.equal(2); | ||
expect(result.limit).to.equal(10); | ||
expect(result.docs).to.be.an('array'); | ||
expect(result.docs.length).to.equal(8); | ||
cb(null); | ||
} | ||
); | ||
} | ||
], | ||
done | ||
); | ||
}); | ||
it('should allow counting number of records', function(done) { | ||
async.series([ | ||
function(next) { | ||
model.create(function(err, doc) { | ||
expect(err).to.not.exist; | ||
doc.email = 'test@osmos.com'; | ||
doc.name = 'test'; | ||
doc.save(function(err) { | ||
expect(err).to.not.exist; | ||
next(); | ||
}); | ||
}); | ||
}, | ||
function(cb) { | ||
model.findLimit( | ||
{ | ||
$query : { | ||
email: email | ||
}, | ||
function(next) { | ||
model.count({ email: 'test@osmos.com' }, function(err, count) { | ||
expect(err).to.not.exist; | ||
expect(count).to.be.equal(1); | ||
$orderBy: { | ||
_id: -1 | ||
} | ||
}, | ||
2, | ||
10, | ||
function(err, result) { | ||
expect(err).not.to.be.ok; | ||
expect(result).to.be.an('object'); | ||
expect(result.count).to.equal(10); | ||
expect(result.start).to.equal(2); | ||
expect(result.limit).to.equal(10); | ||
expect(result.docs).to.be.an('array'); | ||
expect(result.docs.length).to.equal(8); | ||
cb(null); | ||
} | ||
); | ||
next(); | ||
}); | ||
} | ||
], | ||
done | ||
); | ||
], function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -449,2 +449,30 @@ /*jshint expr:true*/ | ||
it('should be able to count the number of elements in the database', function(done) { | ||
async.series([ | ||
function(next) { | ||
model.create(function(err, doc) { | ||
expect(err).to.not.exist; | ||
doc.total = 100; | ||
doc.email = 'test@osmos.com'; | ||
doc.save(function(err) { | ||
expect(err).to.not.exist; | ||
next(); | ||
}); | ||
}); | ||
}, | ||
function(next) { | ||
model.count({ email: 'test@osmos.com' }, function(err, count) { | ||
expect(err).to.not.exist; | ||
expect(count).to.be.equal(1); | ||
next(); | ||
}); | ||
} | ||
], function() { | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -95,3 +95,3 @@ /*jshint expr:true*/ | ||
expect(doc.primaryKey).to.be.undefined; | ||
doc.save(function(err) { | ||
@@ -153,3 +153,3 @@ expect(err).to.not.be.ok; | ||
expect(err).not.to.be.ok; | ||
expect(doc3).to.be.an('object'); | ||
@@ -270,3 +270,19 @@ expect(doc3.name).to.equal('Joe'); | ||
it('should allow finding number of documents matching spec', function(done) { | ||
model.create(function(err, doc) { | ||
doc.name = 'Tester'; | ||
doc.email = 'Osmos@osmos.com'; | ||
doc.save(function() { | ||
model.count({ name : 'Tester' }, function(err, count) { | ||
expect(err).to.not.exist; | ||
expect(count).to.be.equal(1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should allow performing complex queries using a custom function', function(done) { | ||
@@ -302,2 +318,2 @@ model.create(function(err, doc) { | ||
}); | ||
}); | ||
}); |
@@ -151,2 +151,12 @@ 'use strict'; | ||
it('should support counting the number of occurrences', function(done) { | ||
expect(model).to.respondTo('count'); | ||
model.count({ name: 'Marco' }, function(err, count) { | ||
expect(err).to.not.exist; | ||
expect(count).to.be.equal(1); | ||
done(); | ||
}); | ||
}); | ||
}); |
192298
4837