From 24c4381221e8a333629516d64729378fad5bffc1 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 3 Mar 2014 17:16:37 -0800 Subject: [PATCH] Allows scopes to be defined in LDL --- lib/datasource.js | 18 ++++++++++++++++++ lib/scope.js | 12 ++++++++++++ test/loopback-dl.test.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/lib/datasource.js b/lib/datasource.js index 0bff0eee..99dd6565 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -7,6 +7,7 @@ var jutil = require('./jutil'); var utils = require('./utils'); var ModelBaseClass = require('./model.js'); var DataAccessObject = require('./dao.js'); +var defineScope = require('./scope.js').defineScope; var EventEmitter = require('events').EventEmitter; var util = require('util'); var assert = require('assert'); @@ -346,6 +347,20 @@ DataSource.relationTypes = ['belongsTo', 'hasMany', 'hasAndBelongsToMany']; function isModelDataSourceAttached(model) { 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 * @param modelClass @@ -451,6 +466,9 @@ DataSource.prototype.setupDataAccess = function (modelClass, settings) { var relations = settings.relationships || settings.relations; this.defineRelations(modelClass, relations); + var scopes = settings.scopes || {}; + this.defineScopes(modelClass, scopes); + }; /** diff --git a/lib/scope.js b/lib/scope.js index a54aad37..d5ec8b61 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -188,6 +188,18 @@ function defineScope(cls, targetClass, name, params, methods) { 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; } diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index e826e188..4ac1f91b 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -1,6 +1,7 @@ // This test written in mocha+should.js var should = require('./init.js'); var assert = require('assert'); +var async = require('async'); var jdb = require('../'); 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 () { var ds, model, where;