2014-01-17 18:40:41 +00:00
|
|
|
/*!
|
2013-07-16 18:05:38 +00:00
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2014-11-11 19:27:39 +00:00
|
|
|
var express = require('express');
|
|
|
|
var loopbackExpress = require('./server-app');
|
2014-10-30 20:49:47 +00:00
|
|
|
var proto = require('./application');
|
|
|
|
var fs = require('fs');
|
|
|
|
var ejs = require('ejs');
|
|
|
|
var path = require('path');
|
|
|
|
var merge = require('util')._extend;
|
|
|
|
var assert = require('assert');
|
2015-04-01 21:50:36 +00:00
|
|
|
var Registry = require('./registry');
|
|
|
|
var juggler = require('loopback-datasource-juggler');
|
2013-07-16 18:05:38 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* LoopBack core module. It provides static properties and
|
2014-01-17 18:40:41 +00:00
|
|
|
* methods to create models and data sources. The module itself is a function
|
2014-10-15 07:07:30 +00:00
|
|
|
* that creates loopback `app`. For example:
|
2014-01-17 18:40:41 +00:00
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* var loopback = require('loopback');
|
|
|
|
* var app = loopback();
|
|
|
|
* ```
|
2014-04-02 22:46:39 +00:00
|
|
|
*
|
2014-10-15 07:07:30 +00:00
|
|
|
* @property {String} version Version of LoopBack framework. Static read-only property.
|
|
|
|
* @property {String} mime
|
|
|
|
* @property {Boolean} isBrowser True if running in a browser environment; false otherwise. Static read-only property.
|
|
|
|
* @property {Boolean} isServer True if running in a server environment; false otherwise. Static read-only property.
|
2015-04-01 21:50:36 +00:00
|
|
|
* @property {Registry} registry The global `Registry` object.
|
2014-11-04 18:59:38 +00:00
|
|
|
* @property {String} faviconFile Path to a default favicon shipped with LoopBack.
|
|
|
|
* Use as follows: `app.use(require('serve-favicon')(loopback.faviconFile));`
|
2014-04-02 22:46:39 +00:00
|
|
|
* @class loopback
|
|
|
|
* @header loopback
|
2013-07-16 18:05:38 +00:00
|
|
|
*/
|
|
|
|
|
2014-11-04 07:13:21 +00:00
|
|
|
var loopback = module.exports = createApplication;
|
2013-07-16 18:05:38 +00:00
|
|
|
|
2014-10-15 07:07:30 +00:00
|
|
|
/*!
|
2014-06-10 09:19:36 +00:00
|
|
|
* Framework version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.version = require('../package.json').version;
|
|
|
|
|
2014-10-15 07:07:30 +00:00
|
|
|
/*!
|
2013-07-16 18:05:38 +00:00
|
|
|
* Expose mime.
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.mime = express.mime;
|
|
|
|
|
2015-04-17 17:21:50 +00:00
|
|
|
loopback.registry = new Registry();
|
|
|
|
|
2015-04-17 16:02:04 +00:00
|
|
|
Object.defineProperties(loopback, {
|
|
|
|
Model: {
|
|
|
|
get: function() { return this.registry.getModel('Model'); }
|
|
|
|
},
|
|
|
|
PersistedModel: {
|
|
|
|
get: function() { return this.registry.getModel('PersistedModel'); }
|
|
|
|
},
|
|
|
|
defaultDataSources: {
|
|
|
|
get: function() { return this.registry.defaultDataSources; }
|
|
|
|
},
|
|
|
|
modelBuilder: {
|
|
|
|
get: function() { return this.registry.modelBuilder; }
|
2015-04-01 21:50:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-11 01:05:44 +00:00
|
|
|
/*!
|
2013-07-16 18:05:38 +00:00
|
|
|
* Create an loopback application.
|
|
|
|
*
|
|
|
|
* @return {Function}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
function createApplication(options) {
|
2014-11-11 19:27:39 +00:00
|
|
|
var app = loopbackExpress();
|
2013-07-16 18:05:38 +00:00
|
|
|
|
2014-02-12 00:01:51 +00:00
|
|
|
merge(app, proto);
|
2013-07-16 18:05:38 +00:00
|
|
|
|
2014-06-13 08:27:23 +00:00
|
|
|
app.loopback = loopback;
|
|
|
|
|
2014-02-04 19:28:19 +00:00
|
|
|
// Create a new instance of models registry per each app instance
|
|
|
|
app.models = function() {
|
|
|
|
return proto.models.apply(this, arguments);
|
|
|
|
};
|
2013-07-16 18:05:38 +00:00
|
|
|
|
2014-05-27 12:33:42 +00:00
|
|
|
// Create a new instance of datasources registry per each app instance
|
|
|
|
app.datasources = app.dataSources = {};
|
|
|
|
|
2014-05-28 13:02:55 +00:00
|
|
|
// Create a new instance of connector registry per each app instance
|
|
|
|
app.connectors = {};
|
|
|
|
|
|
|
|
// Register built-in connectors. It's important to keep this code
|
|
|
|
// hand-written, so that all require() calls are static
|
|
|
|
// and thus browserify can process them (include connectors in the bundle)
|
|
|
|
app.connector('memory', loopback.Memory);
|
|
|
|
app.connector('remote', loopback.Remote);
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
if (loopback.localRegistry || options && options.localRegistry === true) {
|
|
|
|
// setup the app registry
|
|
|
|
var registry = app.registry = new Registry();
|
2015-06-01 10:07:15 +00:00
|
|
|
if (options && options.loadBuiltinModels === true) {
|
|
|
|
require('./builtin-models')(registry);
|
|
|
|
}
|
2015-04-01 21:50:36 +00:00
|
|
|
} else {
|
|
|
|
app.registry = loopback.registry;
|
|
|
|
}
|
|
|
|
|
2013-07-16 18:05:38 +00:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2014-06-06 09:47:25 +00:00
|
|
|
function mixin(source) {
|
|
|
|
for (var key in source) {
|
|
|
|
var desc = Object.getOwnPropertyDescriptor(source, key);
|
2014-06-23 12:44:19 +00:00
|
|
|
|
|
|
|
// Fix for legacy (pre-ES5) browsers like PhantomJS
|
|
|
|
if (!desc) continue;
|
|
|
|
|
2014-06-06 09:47:25 +00:00
|
|
|
Object.defineProperty(loopback, key, desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mixin(require('./runtime'));
|
|
|
|
|
2014-01-17 18:40:41 +00:00
|
|
|
/*!
|
2014-06-03 08:15:15 +00:00
|
|
|
* Expose static express methods like `express.errorHandler`.
|
2013-07-16 18:05:38 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-06 09:47:25 +00:00
|
|
|
mixin(express);
|
2013-07-16 18:05:38 +00:00
|
|
|
|
2014-06-03 08:15:15 +00:00
|
|
|
/*!
|
|
|
|
* Expose additional middleware like session as loopback.*
|
|
|
|
* This will keep the loopback API compatible with express 3.x
|
|
|
|
*
|
|
|
|
* ***only in node***
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (loopback.isServer) {
|
|
|
|
var middlewares = require('./express-middleware');
|
2014-06-06 09:47:25 +00:00
|
|
|
mixin(middlewares);
|
2014-05-03 18:20:45 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2014-02-12 00:01:51 +00:00
|
|
|
*
|
|
|
|
* ***only in node***
|
2013-07-16 18:05:38 +00:00
|
|
|
*/
|
|
|
|
|
2014-02-12 00:01:51 +00:00
|
|
|
if (loopback.isServer) {
|
|
|
|
fs
|
2014-11-12 11:36:16 +00:00
|
|
|
.readdirSync(path.join(__dirname, '..', 'server', 'middleware'))
|
2014-10-30 20:49:47 +00:00
|
|
|
.filter(function(file) {
|
2014-02-12 00:01:51 +00:00
|
|
|
return file.match(/\.js$/);
|
|
|
|
})
|
2014-10-30 20:49:47 +00:00
|
|
|
.forEach(function(m) {
|
2014-11-12 11:36:16 +00:00
|
|
|
loopback[m.replace(/\.js$/, '')] = require('../server/middleware/' + m);
|
2014-02-12 00:01:51 +00:00
|
|
|
});
|
2014-11-12 11:36:16 +00:00
|
|
|
|
|
|
|
loopback.urlNotFound = loopback['url-not-found'];
|
|
|
|
delete loopback['url-not-found'];
|
2014-02-12 00:01:51 +00:00
|
|
|
}
|
2013-07-16 18:05:38 +00:00
|
|
|
|
2014-11-03 09:00:24 +00:00
|
|
|
/*
|
|
|
|
* Expose path to the default favicon file
|
|
|
|
*
|
|
|
|
* ***only in node***
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (loopback.isServer) {
|
2014-11-04 18:59:38 +00:00
|
|
|
/*!
|
2014-11-03 09:00:24 +00:00
|
|
|
* Path to a default favicon shipped with LoopBack.
|
|
|
|
*
|
|
|
|
* **Example**
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* app.use(require('serve-favicon')(loopback.faviconFile));
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
loopback.faviconFile = path.resolve(__dirname, '../favicon.ico');
|
|
|
|
}
|
|
|
|
|
2014-01-17 18:40:41 +00:00
|
|
|
/*!
|
2013-07-16 18:05:38 +00:00
|
|
|
* Error handler title
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.errorHandler.title = 'Loopback';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a remote method to a model.
|
|
|
|
* @param {Function} fn
|
|
|
|
* @param {Object} options (optional)
|
|
|
|
*/
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
loopback.remoteMethod = function(fn, options) {
|
2013-07-16 18:05:38 +00:00
|
|
|
fn.shared = true;
|
2014-10-30 20:49:47 +00:00
|
|
|
if (typeof options === 'object') {
|
|
|
|
Object.keys(options).forEach(function(key) {
|
2013-07-16 18:05:38 +00:00
|
|
|
fn[key] = options[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
fn.http = fn.http || {verb: 'get'};
|
2014-02-14 18:31:30 +00:00
|
|
|
};
|
2013-07-16 18:05:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}
|
|
|
|
*/
|
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
loopback.template = function(file) {
|
2013-07-16 18:05:38 +00:00
|
|
|
var templates = this._templates || (this._templates = {});
|
|
|
|
var str = templates[file] || (templates[file] = fs.readFileSync(file, 'utf8'));
|
|
|
|
return ejs.compile(str);
|
2014-02-14 18:31:30 +00:00
|
|
|
};
|
2013-07-16 18:05:38 +00:00
|
|
|
|
2015-03-27 13:45:37 +00:00
|
|
|
require('../server/current-context')(loopback);
|
2014-11-10 18:12:00 +00:00
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
/**
|
|
|
|
* Create a named vanilla JavaScript class constructor with an attached
|
|
|
|
* set of properties and options.
|
|
|
|
*
|
|
|
|
* This function comes with two variants:
|
|
|
|
* * `loopback.createModel(name, properties, options)`
|
|
|
|
* * `loopback.createModel(config)`
|
|
|
|
*
|
|
|
|
* In the second variant, the parameters `name`, `properties` and `options`
|
|
|
|
* are provided in the config object. Any additional config entries are
|
|
|
|
* interpreted as `options`, i.e. the following two configs are identical:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* { name: 'Customer', base: 'User' }
|
|
|
|
* { name: 'Customer', options: { base: 'User' } }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* **Example**
|
|
|
|
*
|
|
|
|
* Create an `Author` model using the three-parameter variant:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* loopback.createModel(
|
|
|
|
* 'Author',
|
|
|
|
* {
|
|
|
|
* firstName: 'string',
|
|
|
|
* lastName: 'string'
|
|
|
|
* },
|
|
|
|
* {
|
|
|
|
* relations: {
|
|
|
|
* books: {
|
|
|
|
* model: 'Book',
|
|
|
|
* type: 'hasAndBelongsToMany'
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Create the same model using a config object:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* loopback.createModel({
|
|
|
|
* name: 'Author',
|
|
|
|
* properties: {
|
|
|
|
* firstName: 'string',
|
|
|
|
* lastName: 'string'
|
|
|
|
* },
|
|
|
|
* relations: {
|
|
|
|
* books: {
|
|
|
|
* model: 'Book',
|
|
|
|
* type: 'hasAndBelongsToMany'
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {String} name Unique name.
|
|
|
|
* @param {Object} properties
|
|
|
|
* @param {Object} options (optional)
|
|
|
|
*
|
|
|
|
* @header loopback.createModel
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.createModel = function(name, properties, options) {
|
|
|
|
return this.registry.createModel.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alter an existing Model class.
|
|
|
|
* @param {Model} ModelCtor The model constructor to alter.
|
|
|
|
* @options {Object} config Additional configuration to apply
|
|
|
|
* @property {DataSource} dataSource Attach the model to a dataSource.
|
|
|
|
* @property {Object} [relations] Model relations to add/update.
|
|
|
|
*
|
|
|
|
* @header loopback.configureModel(ModelCtor, config)
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.configureModel = function(ModelCtor, config) {
|
|
|
|
return this.registry.configureModel.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up a model class by name from all models created by
|
|
|
|
* `loopback.createModel()`
|
|
|
|
* @param {String} modelName The model name
|
|
|
|
* @returns {Model} The model class
|
|
|
|
*
|
|
|
|
* @header loopback.findModel(modelName)
|
|
|
|
*/
|
|
|
|
loopback.findModel = function(modelName) {
|
|
|
|
return this.registry.findModel.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up a model class by name from all models created by
|
|
|
|
* `loopback.createModel()`. Throw an error when no such model exists.
|
|
|
|
*
|
|
|
|
* @param {String} modelName The model name
|
|
|
|
* @returns {Model} The model class
|
|
|
|
*
|
|
|
|
* @header loopback.getModel(modelName)
|
|
|
|
*/
|
|
|
|
loopback.getModel = function(modelName) {
|
|
|
|
return this.registry.getModel.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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} modelType The base model class
|
|
|
|
* @returns {Model} The subclass if found or the base class
|
|
|
|
*
|
|
|
|
* @header loopback.getModelByType(modelType)
|
|
|
|
*/
|
|
|
|
loopback.getModelByType = function(modelType) {
|
|
|
|
return this.registry.getModelByType.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a data source with passing the provided options to the connector.
|
|
|
|
*
|
|
|
|
* @param {String} name Optional name.
|
|
|
|
* @options {Object} options Data Source options
|
|
|
|
* @property {Object} connector LoopBack connector.
|
|
|
|
* @property {*} [*] Other connector properties.
|
|
|
|
* See the relevant connector documentation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.createDataSource = function(name, options) {
|
|
|
|
return this.registry.createDataSource.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
return this.registry.memory.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default `dataSource` for a given `type`.
|
|
|
|
* @param {String} type The datasource type.
|
|
|
|
* @param {Object|DataSource} dataSource The data source settings or instance
|
|
|
|
* @returns {DataSource} The data source instance.
|
|
|
|
*
|
|
|
|
* @header loopback.setDefaultDataSourceForType(type, dataSource)
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.setDefaultDataSourceForType = function(type, dataSource) {
|
|
|
|
return this.registry.setDefaultDataSourceForType.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default `dataSource` for a given `type`.
|
|
|
|
* @param {String} type The datasource type.
|
|
|
|
* @returns {DataSource} The data source instance
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.getDefaultDataSourceForType = function(type) {
|
|
|
|
return this.registry.getDefaultDataSourceForType.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach any model that does not have a dataSource to
|
|
|
|
* the default dataSource for the type the Model requests
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback.autoAttach = function() {
|
|
|
|
return this.registry.autoAttach.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
loopback.autoAttachModel = function(ModelCtor) {
|
|
|
|
return this.registry.autoAttachModel.apply(this.registry, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
// temporary alias to simplify migration of code based on <=2.0.0-beta3
|
2015-04-17 16:02:04 +00:00
|
|
|
// TODO(bajtos) Remove this in v3.0
|
2015-04-01 21:50:36 +00:00
|
|
|
Object.defineProperty(loopback, 'DataModel', {
|
|
|
|
get: function() {
|
|
|
|
return this.registry.DataModel;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-11 01:05:44 +00:00
|
|
|
/*!
|
2013-07-16 18:05:38 +00:00
|
|
|
* Built in models / services
|
|
|
|
*/
|
|
|
|
|
2014-10-09 14:51:12 +00:00
|
|
|
require('./builtin-models')(loopback);
|
2015-04-01 21:50:36 +00:00
|
|
|
|
|
|
|
loopback.DataSource = juggler.DataSource;
|