Add scope definitions to the model class
See https://github.com/strongloop/loopback/issues/454
This commit is contained in:
parent
0adf440cd1
commit
7fe7bf9b9a
21
lib/scope.js
21
lib/scope.js
|
@ -8,11 +8,12 @@ exports.defineScope = defineScope;
|
||||||
exports.mergeQuery = mergeQuery;
|
exports.mergeQuery = mergeQuery;
|
||||||
|
|
||||||
function ScopeDefinition(definition) {
|
function ScopeDefinition(definition) {
|
||||||
this.sourceModel = definition.sourceModel;
|
this.modelFrom = definition.modelFrom || definition.sourceModel;
|
||||||
this.targetModel = definition.targetModel || definition.sourceModel;
|
this.modelTo = definition.modelTo || definition.targetModel;
|
||||||
this.name = definition.name;
|
this.name = definition.name;
|
||||||
this.params = definition.params;
|
this.params = definition.params;
|
||||||
this.methods = definition.methods;
|
this.methods = definition.methods;
|
||||||
|
this.options = definition.options;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefresh, cb) {
|
ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefresh, cb) {
|
||||||
|
@ -40,7 +41,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
|
||||||
|| actualRefresh) {
|
|| actualRefresh) {
|
||||||
// It either doesn't hit the cache or refresh is required
|
// It either doesn't hit the cache or refresh is required
|
||||||
var params = mergeQuery(actualCond, scopeParams);
|
var params = mergeQuery(actualCond, scopeParams);
|
||||||
return this.targetModel.find(params, function (err, data) {
|
return this.modelTo.find(params, function (err, data) {
|
||||||
if (!err && saveOnCache) {
|
if (!err && saveOnCache) {
|
||||||
defineCachedRelations(self);
|
defineCachedRelations(self);
|
||||||
self.__cachedRelations[name] = data;
|
self.__cachedRelations[name] = data;
|
||||||
|
@ -62,7 +63,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
|
||||||
* to return the query object
|
* to return the query object
|
||||||
* @param methods An object of methods keyed by the method name to be bound to the class
|
* @param methods An object of methods keyed by the method name to be bound to the class
|
||||||
*/
|
*/
|
||||||
function defineScope(cls, targetClass, name, params, methods) {
|
function defineScope(cls, targetClass, name, params, methods, options) {
|
||||||
|
|
||||||
// collect meta info about scope
|
// collect meta info about scope
|
||||||
if (!cls._scopeMeta) {
|
if (!cls._scopeMeta) {
|
||||||
|
@ -80,13 +81,17 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var definition = new ScopeDefinition({
|
var definition = new ScopeDefinition({
|
||||||
sourceModel: cls,
|
modelFrom: cls,
|
||||||
targetModel: targetClass,
|
modelTo: targetClass,
|
||||||
name: name,
|
name: name,
|
||||||
params: params,
|
params: params,
|
||||||
methods: methods
|
methods: methods,
|
||||||
|
options: options || {}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
cls.scopes = cls.scopes || {};
|
||||||
|
cls.scopes[name] = definition;
|
||||||
|
|
||||||
// Define a property for the scope
|
// Define a property for the scope
|
||||||
Object.defineProperty(cls, name, {
|
Object.defineProperty(cls, name, {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
|
@ -115,7 +120,7 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
f._scope = typeof definition.params === 'function' ?
|
f._scope = typeof definition.params === 'function' ?
|
||||||
definition.params.call(self) : definition.params;
|
definition.params.call(self) : definition.params;
|
||||||
|
|
||||||
f._targetClass = definition.targetModel.modelName;
|
f._targetClass = definition.modelTo.modelName;
|
||||||
if (f._scope.collect) {
|
if (f._scope.collect) {
|
||||||
f._targetClass = i8n.capitalize(f._scope.collect);
|
f._targetClass = i8n.capitalize(f._scope.collect);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,14 @@ describe('scope', function () {
|
||||||
db = getSchema();
|
db = getSchema();
|
||||||
Railway = db.define('Railway', {
|
Railway = db.define('Railway', {
|
||||||
URID: {type: String, index: true}
|
URID: {type: String, index: true}
|
||||||
|
}, {
|
||||||
|
scopes: {
|
||||||
|
highSpeed: {
|
||||||
|
where: {
|
||||||
|
highSpeed: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
Station = db.define('Station', {
|
Station = db.define('Station', {
|
||||||
USID: {type: String, index: true},
|
USID: {type: String, index: true},
|
||||||
|
@ -25,8 +33,14 @@ describe('scope', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should define scope using options.scopes', function () {
|
||||||
|
Railway.scopes.should.have.property('highSpeed');
|
||||||
|
Railway.highSpeed.should.be.function;
|
||||||
|
});
|
||||||
|
|
||||||
it('should define scope with query', function (done) {
|
it('should define scope with query', function (done) {
|
||||||
Station.scope('active', {where: {isActive: true}});
|
Station.scope('active', {where: {isActive: true}});
|
||||||
|
Station.scopes.should.have.property('active');
|
||||||
Station.active.create(function (err, station) {
|
Station.active.create(function (err, station) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(station);
|
should.exist(station);
|
||||||
|
|
Loading…
Reference in New Issue