2014-09-25 21:06:06 +00:00
|
|
|
/**
|
|
|
|
* Dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
var remoting = require('strong-remoting');
|
2014-09-25 21:25:40 +00:00
|
|
|
var jutil = require('loopback-datasource-juggler/lib/jutil');
|
|
|
|
var RelationMixin = require('./relations');
|
2014-12-19 22:30:56 +00:00
|
|
|
var InclusionMixin = require('loopback-datasource-juggler/lib/include');
|
2014-09-25 21:06:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the RemoteConnector class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = RemoteConnector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an instance of the connector with the given `settings`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function RemoteConnector(settings) {
|
2014-09-25 21:19:39 +00:00
|
|
|
assert(typeof settings ===
|
|
|
|
'object',
|
|
|
|
'cannot initiaze RemoteConnector without a settings object');
|
2014-09-25 21:06:06 +00:00
|
|
|
this.client = settings.client;
|
|
|
|
this.adapter = settings.adapter || 'rest';
|
|
|
|
this.protocol = settings.protocol || 'http'
|
|
|
|
this.root = settings.root || '';
|
|
|
|
this.host = settings.host || 'localhost';
|
|
|
|
this.port = settings.port || 3000;
|
|
|
|
this.remotes = remoting.create();
|
|
|
|
this.name = 'remote-connector';
|
|
|
|
|
2014-09-25 21:19:39 +00:00
|
|
|
if (settings.url) {
|
2014-09-25 21:06:06 +00:00
|
|
|
this.url = settings.url;
|
|
|
|
} else {
|
|
|
|
this.url = this.protocol + '://' + this.host + ':' + this.port + this.root;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle mixins in the define() method
|
2014-09-25 21:19:39 +00:00
|
|
|
var DAO = this.DataAccessObject = function() {
|
|
|
|
};
|
2014-12-19 22:30:56 +00:00
|
|
|
|
2014-09-25 21:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoteConnector.prototype.connect = function() {
|
|
|
|
this.remotes.connect(this.url, this.adapter);
|
2014-09-25 21:25:40 +00:00
|
|
|
};
|
2014-09-25 21:06:06 +00:00
|
|
|
|
|
|
|
RemoteConnector.initialize = function(dataSource, callback) {
|
2014-09-25 21:19:39 +00:00
|
|
|
var connector = dataSource.connector =
|
|
|
|
new RemoteConnector(dataSource.settings);
|
2014-09-25 21:06:06 +00:00
|
|
|
connector.connect();
|
2014-10-10 08:29:23 +00:00
|
|
|
process.nextTick(callback);
|
2014-09-25 21:25:40 +00:00
|
|
|
};
|
2014-09-25 21:06:06 +00:00
|
|
|
|
|
|
|
RemoteConnector.prototype.define = function(definition) {
|
|
|
|
var Model = definition.model;
|
|
|
|
var remotes = this.remotes;
|
|
|
|
|
2014-09-25 21:19:39 +00:00
|
|
|
assert(Model.sharedClass,
|
|
|
|
'cannot attach ' +
|
|
|
|
Model.modelName +
|
|
|
|
' to a remote connector without a Model.sharedClass');
|
2014-09-25 21:06:06 +00:00
|
|
|
|
2014-09-25 21:25:40 +00:00
|
|
|
jutil.mixin(Model, RelationMixin);
|
2014-12-19 22:30:56 +00:00
|
|
|
jutil.mixin(Model, InclusionMixin);
|
2014-09-25 21:06:06 +00:00
|
|
|
remotes.addClass(Model.sharedClass);
|
2014-10-01 21:36:08 +00:00
|
|
|
this.resolve(Model);
|
2014-09-25 21:25:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RemoteConnector.prototype.resolve = function(Model) {
|
|
|
|
var remotes = this.remotes;
|
2014-09-25 21:06:06 +00:00
|
|
|
|
2014-09-25 21:19:39 +00:00
|
|
|
Model.sharedClass.methods().forEach(function(remoteMethod) {
|
2014-09-25 21:25:40 +00:00
|
|
|
if (remoteMethod.name !== 'Change' && remoteMethod.name !== 'Checkpoint') {
|
|
|
|
createProxyMethod(Model, remotes, remoteMethod);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2014-09-25 21:06:06 +00:00
|
|
|
|
|
|
|
function createProxyMethod(Model, remotes, remoteMethod) {
|
|
|
|
var scope = remoteMethod.isStatic ? Model : Model.prototype;
|
|
|
|
var original = scope[remoteMethod.name];
|
2014-09-25 21:19:39 +00:00
|
|
|
|
2015-03-27 00:14:35 +00:00
|
|
|
function remoteMethodProxy() {
|
2014-09-25 21:06:06 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var lastArgIsFunc = typeof args[args.length - 1] === 'function';
|
|
|
|
var callback;
|
2014-09-25 21:19:39 +00:00
|
|
|
if (lastArgIsFunc) {
|
2014-09-25 21:06:06 +00:00
|
|
|
callback = args.pop();
|
|
|
|
}
|
|
|
|
|
2014-09-25 21:25:40 +00:00
|
|
|
if (remoteMethod.isStatic) {
|
|
|
|
return remotes.invoke(remoteMethod.stringName, args, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctorArgs = [this.id];
|
|
|
|
return remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback);
|
2014-09-25 21:06:06 +00:00
|
|
|
}
|
2015-03-27 00:14:35 +00:00
|
|
|
|
|
|
|
scope[remoteMethod.name] = remoteMethodProxy;
|
|
|
|
remoteMethod.aliases.forEach(function(alias) {
|
|
|
|
scope[alias] = remoteMethodProxy;
|
|
|
|
});
|
2014-09-25 21:06:06 +00:00
|
|
|
}
|
|
|
|
|
2014-09-25 21:19:39 +00:00
|
|
|
function noop() {
|
|
|
|
}
|