Implement scope.findById
This commit is contained in:
parent
112f2bd652
commit
34acc6c50a
22
lib/scope.js
22
lib/scope.js
|
@ -189,6 +189,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
||||||
f.build = build;
|
f.build = build;
|
||||||
f.create = create;
|
f.create = create;
|
||||||
f.destroyAll = destroyAll;
|
f.destroyAll = destroyAll;
|
||||||
|
f.findById = findById;
|
||||||
f.count = count;
|
f.count = count;
|
||||||
for (var i in definition.methods) {
|
for (var i in definition.methods) {
|
||||||
f[i] = definition.methods[i].bind(self);
|
f[i] = definition.methods[i].bind(self);
|
||||||
|
@ -237,6 +238,13 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
||||||
|
|
||||||
cls['__delete__' + name] = fn_delete;
|
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 fn_count = function (cb) {
|
||||||
var f = this[name].count;
|
var f = this[name].count;
|
||||||
f.apply(this[name], arguments);
|
f.apply(this[name], arguments);
|
||||||
|
@ -270,17 +278,27 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
||||||
*/
|
*/
|
||||||
function destroyAll(where, cb) {
|
function destroyAll(where, cb) {
|
||||||
if (typeof where === 'function') cb = where, where = {};
|
if (typeof where === 'function') cb = where, where = {};
|
||||||
|
var targetModel = definition.targetModel(this._receiver);
|
||||||
var scoped = (this._scope && this._scope.where) || {};
|
var scoped = (this._scope && this._scope.where) || {};
|
||||||
var filter = mergeQuery({ where: scoped }, { where: where || {} });
|
var filter = mergeQuery({ where: scoped }, { where: where || {} });
|
||||||
var targetModel = definition.targetModel(this._receiver);
|
|
||||||
targetModel.destroyAll(filter.where, cb);
|
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) {
|
function count(where, cb) {
|
||||||
if (typeof where === 'function') cb = where, where = {};
|
if (typeof where === 'function') cb = where, where = {};
|
||||||
|
var targetModel = definition.targetModel(this._receiver);
|
||||||
var scoped = (this._scope && this._scope.where) || {};
|
var scoped = (this._scope && this._scope.where) || {};
|
||||||
var filter = mergeQuery({ where: scoped }, { where: where || {} });
|
var filter = mergeQuery({ where: scoped }, { where: where || {} });
|
||||||
var targetModel = definition.targetModel(this._receiver);
|
|
||||||
targetModel.count(filter.where, cb);
|
targetModel.count(filter.where, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -133,6 +133,8 @@ describe('scope - order', function () {
|
||||||
|
|
||||||
describe('scope - filtered count and destroyAll', function () {
|
describe('scope - filtered count and destroyAll', function () {
|
||||||
|
|
||||||
|
var stationA;
|
||||||
|
|
||||||
before(function () {
|
before(function () {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
Station = db.define('Station', {
|
Station = db.define('Station', {
|
||||||
|
@ -154,7 +156,10 @@ describe('scope - filtered count and destroyAll', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(function (done) {
|
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) {
|
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) {
|
it('should count all in scope - active', function(done) {
|
||||||
Station.active.count(function(err, count) {
|
Station.active.count(function(err, count) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
|
|
Loading…
Reference in New Issue