Implement scope.findById
This commit is contained in:
parent
112f2bd652
commit
34acc6c50a
24
lib/scope.js
24
lib/scope.js
|
@ -189,6 +189,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
|||
f.build = build;
|
||||
f.create = create;
|
||||
f.destroyAll = destroyAll;
|
||||
f.findById = findById;
|
||||
f.count = count;
|
||||
for (var i in definition.methods) {
|
||||
f[i] = definition.methods[i].bind(self);
|
||||
|
@ -237,6 +238,13 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
|||
|
||||
cls['__delete__' + name] = fn_delete;
|
||||
|
||||
var fn_findById = function (cb) {
|
||||
var f = this[name].findById;
|
||||
f.apply(this[name], arguments);
|
||||
};
|
||||
|
||||
cls['__findById__' + name] = fn_findById;
|
||||
|
||||
var fn_count = function (cb) {
|
||||
var f = this[name].count;
|
||||
f.apply(this[name], arguments);
|
||||
|
@ -270,19 +278,29 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
|||
*/
|
||||
function destroyAll(where, cb) {
|
||||
if (typeof where === 'function') cb = where, where = {};
|
||||
var targetModel = definition.targetModel(this._receiver);
|
||||
var scoped = (this._scope && this._scope.where) || {};
|
||||
var filter = mergeQuery({ where: scoped }, { where: where || {} });
|
||||
var targetModel = definition.targetModel(this._receiver);
|
||||
targetModel.destroyAll(filter.where, cb);
|
||||
}
|
||||
|
||||
function findById(id, cb) {
|
||||
var targetModel = definition.targetModel(this._receiver);
|
||||
var idName = targetModel.definition.idName();
|
||||
var where = {};
|
||||
where[idName] = id;
|
||||
var scoped = (this._scope && this._scope.where) || {};
|
||||
var filter = mergeQuery({ where: scoped }, { where: where });
|
||||
targetModel.findOne(filter, cb);
|
||||
}
|
||||
|
||||
function count(where, cb) {
|
||||
if (typeof where === 'function') cb = where, where = {};
|
||||
var targetModel = definition.targetModel(this._receiver);
|
||||
var scoped = (this._scope && this._scope.where) || {};
|
||||
var filter = mergeQuery({ where: scoped }, { where: where || {} });
|
||||
var targetModel = definition.targetModel(this._receiver);
|
||||
targetModel.count(filter.where, cb);
|
||||
}
|
||||
|
||||
|
||||
return definition;
|
||||
}
|
||||
|
|
|
@ -133,6 +133,8 @@ describe('scope - order', function () {
|
|||
|
||||
describe('scope - filtered count and destroyAll', function () {
|
||||
|
||||
var stationA;
|
||||
|
||||
before(function () {
|
||||
db = getSchema();
|
||||
Station = db.define('Station', {
|
||||
|
@ -154,7 +156,10 @@ describe('scope - filtered count and destroyAll', function () {
|
|||
});
|
||||
|
||||
beforeEach(function (done) {
|
||||
Station.create({ name: 'a', order: 1 }, done);
|
||||
Station.create({ name: 'a', order: 1 }, function(err, inst) {
|
||||
stationA = inst;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
beforeEach(function (done) {
|
||||
|
@ -177,6 +182,22 @@ describe('scope - filtered count and destroyAll', function () {
|
|||
});
|
||||
});
|
||||
|
||||
it('should find by id - match', function(done) {
|
||||
Station.active.findById(stationA.id, function(err, station) {
|
||||
should.not.exist(err);
|
||||
station.name.should.equal('a');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should find by id - no match', function(done) {
|
||||
Station.inactive.findById(stationA.id, function(err, station) {
|
||||
should.not.exist(err);
|
||||
should.not.exist(station);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should count all in scope - active', function(done) {
|
||||
Station.active.count(function(err, count) {
|
||||
should.not.exist(err);
|
||||
|
|
Loading…
Reference in New Issue