Allows scopes to be defined in LDL
This commit is contained in:
parent
97c5cfd644
commit
24c4381221
|
@ -7,6 +7,7 @@ var jutil = require('./jutil');
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
var ModelBaseClass = require('./model.js');
|
var ModelBaseClass = require('./model.js');
|
||||||
var DataAccessObject = require('./dao.js');
|
var DataAccessObject = require('./dao.js');
|
||||||
|
var defineScope = require('./scope.js').defineScope;
|
||||||
var EventEmitter = require('events').EventEmitter;
|
var EventEmitter = require('events').EventEmitter;
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
@ -346,6 +347,20 @@ DataSource.relationTypes = ['belongsTo', 'hasMany', 'hasAndBelongsToMany'];
|
||||||
function isModelDataSourceAttached(model) {
|
function isModelDataSourceAttached(model) {
|
||||||
return model && (!model.settings.unresolved) && (model.dataSource instanceof DataSource);
|
return model && (!model.settings.unresolved) && (model.dataSource instanceof DataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Define scopes for the model class from the scopes object
|
||||||
|
* @param modelClass
|
||||||
|
* @param scopes
|
||||||
|
*/
|
||||||
|
DataSource.prototype.defineScopes = function (modelClass, scopes) {
|
||||||
|
if(scopes) {
|
||||||
|
for(var s in scopes) {
|
||||||
|
defineScope(modelClass, modelClass, s, scopes[s]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Define relations for the model class from the relations object
|
* Define relations for the model class from the relations object
|
||||||
* @param modelClass
|
* @param modelClass
|
||||||
|
@ -451,6 +466,9 @@ DataSource.prototype.setupDataAccess = function (modelClass, settings) {
|
||||||
var relations = settings.relationships || settings.relations;
|
var relations = settings.relationships || settings.relations;
|
||||||
this.defineRelations(modelClass, relations);
|
this.defineRelations(modelClass, relations);
|
||||||
|
|
||||||
|
var scopes = settings.scopes || {};
|
||||||
|
this.defineScopes(modelClass, scopes);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
12
lib/scope.js
12
lib/scope.js
|
@ -188,6 +188,18 @@ function defineScope(cls, targetClass, name, params, methods) {
|
||||||
base.order = update.order;
|
base.order = update.order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(update.limit !== undefined) {
|
||||||
|
base.limit = update.limit;
|
||||||
|
}
|
||||||
|
if(update.skip !== undefined) {
|
||||||
|
base.skip = update.skip;
|
||||||
|
}
|
||||||
|
if(update.offset !== undefined) {
|
||||||
|
base.offset = update.offset;
|
||||||
|
}
|
||||||
|
if(update.fields !== undefined) {
|
||||||
|
base.fields = update.fields;
|
||||||
|
}
|
||||||
return base;
|
return base;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
// This test written in mocha+should.js
|
// This test written in mocha+should.js
|
||||||
var should = require('./init.js');
|
var should = require('./init.js');
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
var async = require('async');
|
||||||
|
|
||||||
var jdb = require('../');
|
var jdb = require('../');
|
||||||
var ModelBuilder = jdb.ModelBuilder;
|
var ModelBuilder = jdb.ModelBuilder;
|
||||||
|
@ -796,6 +797,33 @@ describe('Load models with relations', function () {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Model with scopes', function () {
|
||||||
|
it('should create scopes', function (done) {
|
||||||
|
var ds = new DataSource('memory');
|
||||||
|
var User = ds.define('User', {name: String, vip: Boolean, age: Number},
|
||||||
|
{scopes: {vips: {where: {vip: true}}, top5: {limit: 5, order: 'age'}}});
|
||||||
|
|
||||||
|
var users = [];
|
||||||
|
for (var i = 0; i < 10; i++) {
|
||||||
|
users.push({name: 'User' + i, vip: i % 3 === 0, age: 20 + i * 2});
|
||||||
|
}
|
||||||
|
async.each(users, function (user, callback) {
|
||||||
|
User.create(user, callback);
|
||||||
|
}, function (err) {
|
||||||
|
User.vips(function (err, vips) {
|
||||||
|
if(err) {
|
||||||
|
return done(err);
|
||||||
|
}
|
||||||
|
assert.equal(vips.length, 4);
|
||||||
|
User.top5(function (err, top5) {
|
||||||
|
assert.equal(top5.length, 5);
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('DataAccessObject', function () {
|
describe('DataAccessObject', function () {
|
||||||
var ds, model, where;
|
var ds, model, where;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue