Refactor modelBuilder to registry and set up default model
This commit is contained in:
parent
f8ffd978cf
commit
3c13ce5a8f
|
@ -3,8 +3,6 @@
|
||||||
*/
|
*/
|
||||||
var registry = require('../registry');
|
var registry = require('../registry');
|
||||||
var compat = require('../compat');
|
var compat = require('../compat');
|
||||||
var ModelBuilder = require('loopback-datasource-juggler').ModelBuilder;
|
|
||||||
var modeler = new ModelBuilder();
|
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,7 +12,7 @@ var assert = require('assert');
|
||||||
* @param {Object} data
|
* @param {Object} data
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Model = module.exports = modeler.define('Model');
|
var Model = module.exports = registry.modelBuilder.define('Model');
|
||||||
|
|
||||||
Model.shared = true;
|
Model.shared = true;
|
||||||
|
|
||||||
|
|
|
@ -9,12 +9,16 @@
|
||||||
|
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var extend = require('util')._extend;
|
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;
|
var registry = module.exports;
|
||||||
|
|
||||||
registry.defaultDataSources = {};
|
registry.defaultDataSources = {};
|
||||||
|
|
||||||
|
registry.modelBuilder = new ModelBuilder();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a named vanilla JavaScript class constructor with an attached
|
* Create a named vanilla JavaScript class constructor with an attached
|
||||||
* set of properties and options.
|
* set of properties and options.
|
||||||
|
@ -179,7 +183,7 @@ registry.configureModel = function(ModelCtor, config) {
|
||||||
* @header loopback.getModel(modelName)
|
* @header loopback.getModel(modelName)
|
||||||
*/
|
*/
|
||||||
registry.getModel = function(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) {
|
registry.getModelByType = function(modelType) {
|
||||||
assert(typeof modelType === 'function',
|
assert(typeof modelType === 'function',
|
||||||
'The model type must be a constructor');
|
'The model type must be a constructor');
|
||||||
var models = this.Model.modelBuilder.models;
|
var models = this.modelBuilder.models;
|
||||||
for(var m in models) {
|
for(var m in models) {
|
||||||
if(models[m].prototype instanceof modelType) {
|
if(models[m].prototype instanceof modelType) {
|
||||||
return models[m];
|
return models[m];
|
||||||
|
@ -216,10 +220,10 @@ registry.getModelByType = function(modelType) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
registry.createDataSource = function (name, options) {
|
registry.createDataSource = function (name, options) {
|
||||||
var loopback = this;
|
var self = this;
|
||||||
var ds = new DataSource(name, options, loopback.Model.modelBuilder);
|
var ds = new DataSource(name, options, self.modelBuilder);
|
||||||
ds.createModel = function (name, properties, settings) {
|
ds.createModel = function (name, properties, settings) {
|
||||||
var ModelCtor = loopback.createModel(name, properties, settings);
|
var ModelCtor = self.createModel(name, properties, settings);
|
||||||
ModelCtor.attachTo(ds);
|
ModelCtor.attachTo(ds);
|
||||||
return ModelCtor;
|
return ModelCtor;
|
||||||
};
|
};
|
||||||
|
@ -293,7 +297,7 @@ registry.getDefaultDataSourceForType = function(type) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
registry.autoAttach = function() {
|
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');
|
assert.equal(typeof models, 'object', 'Cannot autoAttach without a models object');
|
||||||
|
|
||||||
Object.keys(models).forEach(function(modelName) {
|
Object.keys(models).forEach(function(modelName) {
|
||||||
|
@ -327,3 +331,6 @@ registry.DataSource = DataSource;
|
||||||
|
|
||||||
registry.Model = require('./models/model');
|
registry.Model = require('./models/model');
|
||||||
registry.DataModel = require('./models/data-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;
|
||||||
|
|
|
@ -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() {
|
it('Create a data source with a connector.', function() {
|
||||||
var dataSource = loopback.createDataSource({
|
var dataSource = loopback.createDataSource({
|
||||||
connector: loopback.Memory
|
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 () {
|
describe('loopback.autoAttach', function () {
|
||||||
it('doesn\'t overwrite model with datasource configured', function () {
|
it('doesn\'t overwrite model with datasource configured', function () {
|
||||||
var ds1 = loopback.createDataSource('db1', {
|
var ds1 = loopback.createDataSource('db1', {
|
||||||
|
|
Loading…
Reference in New Issue