2014-01-17 18:40:41 +00:00
|
|
|
/*!
|
2013-07-16 18:05:38 +00:00
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var express = require('express')
|
|
|
|
, fs = require('fs')
|
|
|
|
, ejs = require('ejs')
|
|
|
|
, EventEmitter = require('events').EventEmitter
|
|
|
|
, path = require('path')
|
|
|
|
, proto = require('./application')
|
|
|
|
, utils = require('express/node_modules/connect').utils
|
2013-07-30 21:26:49 +00:00
|
|
|
, DataSource = require('loopback-datasource-juggler').DataSource
|
|
|
|
, ModelBuilder = require('loopback-datasource-juggler').ModelBuilder
|
2013-07-16 18:05:38 +00:00
|
|
|
, assert = require('assert')
|
|
|
|
, i8n = require('inflection');
|
|
|
|
|
|
|
|
/**
|
2014-01-17 18:40:41 +00:00
|
|
|
* `loopback` is the main entry for LoopBack core module. It provides static
|
|
|
|
* methods to create models and data sources. The module itself is a function
|
|
|
|
* that creates loopback `app`. For example,
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* var loopback = require('loopback');
|
|
|
|
* var app = loopback();
|
|
|
|
* ```
|
2013-07-16 18:05:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
var loopback = exports = module.exports = createApplication;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Framework version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.version = require('../package.json').version;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose mime.
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.mime = express.mime;
|
|
|
|
|
2014-01-22 10:22:23 +00:00
|
|
|
/*!
|
|
|
|
* Compatibility layer, intentionally left undocumented.
|
|
|
|
*/
|
|
|
|
loopback.compat = require('./compat');
|
|
|
|
|
2013-07-16 18:05:38 +00:00
|
|
|
/**
|
|
|
|
* Create an loopback application.
|
|
|
|
*
|
|
|
|
* @return {Function}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
function createApplication() {
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
utils.merge(app, proto);
|
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2014-01-17 18:40:41 +00:00
|
|
|
/*!
|
2013-07-16 18:05:38 +00:00
|
|
|
* Expose express.middleware as loopback.*
|
|
|
|
* for example `loopback.errorHandler` etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (var key in express) {
|
|
|
|
Object.defineProperty(
|
|
|
|
loopback
|
|
|
|
, key
|
|
|
|
, Object.getOwnPropertyDescriptor(express, key));
|
|
|
|
}
|
|
|
|
|
2014-01-17 18:40:41 +00:00
|
|
|
/*!
|
2013-07-16 18:05:38 +00:00
|
|
|
* Expose additional loopback middleware
|
|
|
|
* for example `loopback.configure` etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fs
|
|
|
|
.readdirSync(path.join(__dirname, 'middleware'))
|
|
|
|
.filter(function (file) {
|
|
|
|
return file.match(/\.js$/);
|
|
|
|
})
|
|
|
|
.forEach(function (m) {
|
|
|
|
loopback[m.replace(/\.js$/, '')] = require('./middleware/' + m);
|
|
|
|
});
|
|
|
|
|
2014-01-17 18:40:41 +00:00
|
|
|
/*!
|
2013-07-16 18:05:38 +00:00
|
|
|
* Error handler title
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.errorHandler.title = 'Loopback';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a data source with passing the provided options to the connector.
|
|
|
|
*
|
|
|
|
* @param {String} name (optional)
|
|
|
|
* @param {Object} options
|
|
|
|
*
|
|
|
|
* - connector - an loopback connector
|
|
|
|
* - other values - see the specified `connector` docs
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.createDataSource = function (name, options) {
|
2013-11-15 19:16:20 +00:00
|
|
|
var ds = new DataSource(name, options, loopback.Model.modelBuilder);
|
2013-07-16 18:05:38 +00:00
|
|
|
ds.createModel = function (name, properties, settings) {
|
|
|
|
var ModelCtor = loopback.createModel(name, properties, settings);
|
|
|
|
ModelCtor.attachTo(ds);
|
|
|
|
return ModelCtor;
|
2013-10-25 03:40:33 +00:00
|
|
|
};
|
2013-11-19 20:23:02 +00:00
|
|
|
|
2013-11-20 22:18:54 +00:00
|
|
|
if(ds.settings && ds.settings.defaultForType) {
|
|
|
|
loopback.setDefaultDataSourceForType(ds.settings.defaultForType, ds);
|
2013-11-19 20:23:02 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 18:05:38 +00:00
|
|
|
return ds;
|
2013-10-25 03:40:33 +00:00
|
|
|
};
|
2013-07-16 18:05:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a named vanilla JavaScript class constructor with an attached set of properties and options.
|
|
|
|
*
|
|
|
|
* @param {String} name - must be unique
|
|
|
|
* @param {Object} properties
|
|
|
|
* @param {Object} options (optional)
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.createModel = function (name, properties, options) {
|
2013-12-10 01:11:01 +00:00
|
|
|
options = options || {};
|
|
|
|
var BaseModel = options.base || options.super;
|
|
|
|
|
|
|
|
if(typeof BaseModel === 'string') {
|
|
|
|
BaseModel = loopback.getModel(BaseModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseModel = BaseModel || loopback.Model;
|
|
|
|
|
|
|
|
var model = BaseModel.extend(name, properties, options);
|
2013-11-19 00:13:40 +00:00
|
|
|
|
2013-11-19 20:23:02 +00:00
|
|
|
// try to attach
|
2013-11-20 22:18:54 +00:00
|
|
|
try {
|
|
|
|
loopback.autoAttachModel(model);
|
|
|
|
} catch(e) {}
|
2013-11-19 20:23:02 +00:00
|
|
|
|
2013-11-19 00:13:40 +00:00
|
|
|
return model;
|
2013-07-16 18:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a remote method to a model.
|
|
|
|
* @param {Function} fn
|
|
|
|
* @param {Object} options (optional)
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.remoteMethod = function (fn, options) {
|
|
|
|
fn.shared = true;
|
|
|
|
if(typeof options === 'object') {
|
|
|
|
Object.keys(options).forEach(function (key) {
|
|
|
|
fn[key] = options[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
fn.http = fn.http || {verb: 'get'};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a template helper.
|
|
|
|
*
|
|
|
|
* var render = loopback.template('foo.ejs');
|
|
|
|
* var html = render({foo: 'bar'});
|
|
|
|
*
|
|
|
|
* @param {String} path Path to the template file.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.template = function (file) {
|
|
|
|
var templates = this._templates || (this._templates = {});
|
|
|
|
var str = templates[file] || (templates[file] = fs.readFileSync(file, 'utf8'));
|
|
|
|
return ejs.compile(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an in-memory data source. Use one if it already exists.
|
|
|
|
*
|
|
|
|
* @param {String} [name] The name of the data source. If not provided, the `'default'` is used.
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.memory = function (name) {
|
|
|
|
name = name || 'default';
|
|
|
|
var memory = (
|
|
|
|
this._memoryDataSources
|
|
|
|
|| (this._memoryDataSources = {})
|
|
|
|
)[name];
|
|
|
|
|
|
|
|
if(!memory) {
|
|
|
|
memory = this._memoryDataSources[name] = loopback.createDataSource({
|
|
|
|
connector: loopback.Memory
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
2013-11-15 17:41:26 +00:00
|
|
|
/**
|
2014-01-17 18:23:59 +00:00
|
|
|
* Look up a model class by name from all models created by loopback.createModel()
|
2013-11-15 17:41:26 +00:00
|
|
|
* @param {String} modelName The model name
|
2014-01-17 18:23:59 +00:00
|
|
|
* @return {Model} The model class
|
2013-11-15 17:41:26 +00:00
|
|
|
*/
|
|
|
|
loopback.getModel = function(modelName) {
|
2013-11-15 19:16:20 +00:00
|
|
|
return loopback.Model.modelBuilder.models[modelName];
|
2013-11-15 17:41:26 +00:00
|
|
|
};
|
|
|
|
|
2014-01-23 22:26:45 +00:00
|
|
|
/**
|
|
|
|
* Look up a model class by the base model class. The method can be used by LoopBack
|
|
|
|
* to find configured models in models.json over the base model.
|
|
|
|
* @param {Model} The base model class
|
|
|
|
* @return {Model} The subclass if found or the base class
|
|
|
|
*/
|
|
|
|
loopback.getModelByType = function(modelType) {
|
|
|
|
assert(typeof modelType === 'function', 'The model type must be a constructor');
|
|
|
|
var models = loopback.Model.modelBuilder.models;
|
|
|
|
for(var m in models) {
|
|
|
|
if(models[m].prototype instanceof modelType) {
|
|
|
|
return models[m];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modelType;
|
|
|
|
};
|
|
|
|
|
2013-11-19 00:13:40 +00:00
|
|
|
/**
|
|
|
|
* Set the default `dataSource` for a given `type`.
|
2014-01-17 18:40:41 +00:00
|
|
|
* @param {String} type The datasource type
|
|
|
|
* @param {Object|DataSource} dataSource The data source settings or instance
|
|
|
|
* @return {DataSource} The data source instance
|
2013-11-19 00:13:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.setDefaultDataSourceForType = function(type, dataSource) {
|
|
|
|
var defaultDataSources = this.defaultDataSources || (this.defaultDataSources = {});
|
|
|
|
|
|
|
|
if(!(dataSource instanceof DataSource)) {
|
|
|
|
dataSource = this.createDataSource(dataSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultDataSources[type] = dataSource;
|
|
|
|
return dataSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default `dataSource` for a given `type`.
|
2014-01-17 18:40:41 +00:00
|
|
|
* @param {String} type The datasource type
|
|
|
|
* @return {DataSource} The data source instance
|
2013-11-19 00:13:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.getDefaultDataSourceForType = function(type) {
|
|
|
|
return this.defaultDataSources && this.defaultDataSources[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach any model that does not have a dataSource to
|
|
|
|
* the default dataSource for the type the Model requests
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.autoAttach = function() {
|
|
|
|
var models = this.Model.modelBuilder.models;
|
|
|
|
assert.equal(typeof models, 'object', 'Cannot autoAttach without a models object');
|
|
|
|
|
|
|
|
Object.keys(models).forEach(function(modelName) {
|
|
|
|
var ModelCtor = models[modelName];
|
|
|
|
|
2014-01-20 22:32:47 +00:00
|
|
|
// Only auto attach if the model doesn't have an explicit data source
|
|
|
|
if(ModelCtor && (!(ModelCtor.dataSource instanceof DataSource))) {
|
2013-11-19 20:23:02 +00:00
|
|
|
loopback.autoAttachModel(ModelCtor);
|
2013-11-19 00:13:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-19 20:23:02 +00:00
|
|
|
loopback.autoAttachModel = function(ModelCtor) {
|
|
|
|
if(ModelCtor.autoAttach) {
|
|
|
|
var ds = loopback.getDefaultDataSourceForType(ModelCtor.autoAttach);
|
|
|
|
|
2013-11-20 22:18:54 +00:00
|
|
|
assert(ds instanceof DataSource, 'cannot autoAttach model "'
|
|
|
|
+ ModelCtor.modelName
|
|
|
|
+ '". No dataSource found of type ' + ModelCtor.autoAttach);
|
2013-11-19 20:23:02 +00:00
|
|
|
|
|
|
|
ModelCtor.attachTo(ds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 18:05:38 +00:00
|
|
|
/*
|
|
|
|
* Built in models / services
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.Model = require('./models/model');
|
|
|
|
loopback.Email = require('./models/email');
|
|
|
|
loopback.User = require('./models/user');
|
2013-10-11 20:44:10 +00:00
|
|
|
loopback.Application = require('./models/application');
|
2013-11-13 19:49:08 +00:00
|
|
|
loopback.AccessToken = require('./models/access-token');
|
2013-11-19 19:02:43 +00:00
|
|
|
loopback.Role = require('./models/role').Role;
|
|
|
|
loopback.RoleMapping = require('./models/role').RoleMapping;
|
2013-11-19 00:13:40 +00:00
|
|
|
loopback.ACL = require('./models/acl').ACL;
|
|
|
|
loopback.Scope = require('./models/acl').Scope;
|
2014-01-26 21:20:19 +00:00
|
|
|
loopback.Change = require('./models/change');
|
2013-11-19 00:13:40 +00:00
|
|
|
|
2014-01-17 18:40:41 +00:00
|
|
|
/*!
|
2013-11-19 00:13:40 +00:00
|
|
|
* Automatically attach these models to dataSources
|
|
|
|
*/
|
|
|
|
|
|
|
|
var dataSourceTypes = {
|
|
|
|
DB: 'db',
|
|
|
|
MAIL: 'mail'
|
|
|
|
};
|
|
|
|
|
|
|
|
loopback.Email.autoAttach = dataSourceTypes.MAIL;
|
|
|
|
loopback.User.autoAttach = dataSourceTypes.DB;
|
|
|
|
loopback.AccessToken.autoAttach = dataSourceTypes.DB;
|
|
|
|
loopback.Role.autoAttach = dataSourceTypes.DB;
|
2013-11-19 19:02:43 +00:00
|
|
|
loopback.RoleMapping.autoAttach = dataSourceTypes.DB;
|
2013-11-19 00:13:40 +00:00
|
|
|
loopback.ACL.autoAttach = dataSourceTypes.DB;
|
|
|
|
loopback.Scope.autoAttach = dataSourceTypes.DB;
|
|
|
|
loopback.Application.autoAttach = dataSourceTypes.DB;
|