From 3c13ce5a8f9d5898c662ff62fcbd98d98c92358b Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 10 Jul 2014 13:46:06 -0700 Subject: [PATCH] Refactor modelBuilder to registry and set up default model --- lib/models/model.js | 4 +--- lib/registry.js | 21 ++++++++++++++------- test/loopback.test.js | 19 ++++++++++++++++++- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/models/model.js b/lib/models/model.js index ab1053c9..b706ad86 100644 --- a/lib/models/model.js +++ b/lib/models/model.js @@ -3,8 +3,6 @@ */ var registry = require('../registry'); var compat = require('../compat'); -var ModelBuilder = require('loopback-datasource-juggler').ModelBuilder; -var modeler = new ModelBuilder(); var assert = require('assert'); /** @@ -14,7 +12,7 @@ var assert = require('assert'); * @param {Object} data */ -var Model = module.exports = modeler.define('Model'); +var Model = module.exports = registry.modelBuilder.define('Model'); Model.shared = true; diff --git a/lib/registry.js b/lib/registry.js index acbb7384..c209f668 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -9,12 +9,16 @@ var assert = require('assert'); var extend = require('util')._extend; -var DataSource = require('loopback-datasource-juggler').DataSource; +var juggler = require('loopback-datasource-juggler'); +var DataSource = juggler.DataSource; +var ModelBuilder = juggler.ModelBuilder; var registry = module.exports; registry.defaultDataSources = {}; +registry.modelBuilder = new ModelBuilder(); + /** * Create a named vanilla JavaScript class constructor with an attached * set of properties and options. @@ -179,7 +183,7 @@ registry.configureModel = function(ModelCtor, config) { * @header loopback.getModel(modelName) */ registry.getModel = function(modelName) { - return this.Model.modelBuilder.models[modelName]; + return this.modelBuilder.models[modelName]; }; /** @@ -194,7 +198,7 @@ registry.getModel = function(modelName) { registry.getModelByType = function(modelType) { assert(typeof modelType === 'function', 'The model type must be a constructor'); - var models = this.Model.modelBuilder.models; + var models = this.modelBuilder.models; for(var m in models) { if(models[m].prototype instanceof modelType) { return models[m]; @@ -216,10 +220,10 @@ registry.getModelByType = function(modelType) { */ registry.createDataSource = function (name, options) { - var loopback = this; - var ds = new DataSource(name, options, loopback.Model.modelBuilder); + var self = this; + var ds = new DataSource(name, options, self.modelBuilder); ds.createModel = function (name, properties, settings) { - var ModelCtor = loopback.createModel(name, properties, settings); + var ModelCtor = self.createModel(name, properties, settings); ModelCtor.attachTo(ds); return ModelCtor; }; @@ -293,7 +297,7 @@ registry.getDefaultDataSourceForType = function(type) { */ registry.autoAttach = function() { - var models = this.Model.modelBuilder.models; + var models = this.modelBuilder.models; assert.equal(typeof models, 'object', 'Cannot autoAttach without a models object'); Object.keys(models).forEach(function(modelName) { @@ -327,3 +331,6 @@ registry.DataSource = DataSource; registry.Model = require('./models/model'); registry.DataModel = require('./models/data-model'); + +// Set the default model base class. This is done after the Model class is defined. +registry.modelBuilder.defaultModelBaseClass = registry.Model; diff --git a/test/loopback.test.js b/test/loopback.test.js index bb0ef95a..b105b540 100644 --- a/test/loopback.test.js +++ b/test/loopback.test.js @@ -13,7 +13,7 @@ describe('loopback', function() { }); }); - describe('loopback.createDataSource(options)', function(){ + describe('loopback.createDataSource(options)', function() { it('Create a data source with a connector.', function() { var dataSource = loopback.createDataSource({ connector: loopback.Memory @@ -22,6 +22,23 @@ describe('loopback', function() { }); }); + describe('data source created by loopback', function() { + it('should create model extending Model by default', function () { + var dataSource = loopback.createDataSource({ + connector: loopback.Memory + }); + var m1 = dataSource.createModel('m1', {}); + assert(m1.prototype instanceof loopback.Model); + }); + }); + + describe('model created by loopback', function() { + it('should extend from Model by default', function() { + var m1 = loopback.createModel('m1', {}); + assert(m1.prototype instanceof loopback.Model); + }); + }); + describe('loopback.autoAttach', function () { it('doesn\'t overwrite model with datasource configured', function () { var ds1 = loopback.createDataSource('db1', {