Move looback remote connector to npm module

This commit is contained in:
Krishna Raman 2014-09-25 13:28:51 -07:00
parent 17ac0e8dd4
commit b5516757ae
3 changed files with 5 additions and 94 deletions

View File

@ -12,7 +12,7 @@ var datasourceJuggler = require('loopback-datasource-juggler');
loopback.Connector = require('./lib/connectors/base-connector');
loopback.Memory = require('./lib/connectors/memory');
loopback.Mail = require('./lib/connectors/mail');
loopback.Remote = require('./lib/connectors/remote');
loopback.Remote = require('loopback-connector-remote');
/**
* Types

View File

@ -1,90 +0,0 @@
/**
* Dependencies.
*/
var assert = require('assert');
var remoting = require('strong-remoting');
var DataAccessObject = require('loopback-datasource-juggler/lib/dao');
/**
* Export the RemoteConnector class.
*/
module.exports = RemoteConnector;
/**
* Create an instance of the connector with the given `settings`.
*/
function RemoteConnector(settings) {
assert(typeof settings === 'object', 'cannot initiaze RemoteConnector without a settings object');
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();
// TODO(ritch) make sure this name works with Model.getSourceId()
this.name = 'remote-connector';
if(settings.url) {
this.url = settings.url;
} else {
this.url = this.protocol + '://' + this.host + ':' + this.port + this.root;
}
// handle mixins in the define() method
var DAO = this.DataAccessObject = function() {};
}
RemoteConnector.prototype.connect = function() {
this.remotes.connect(this.url, this.adapter);
}
RemoteConnector.initialize = function(dataSource, callback) {
var connector = dataSource.connector = new RemoteConnector(dataSource.settings);
connector.connect();
callback();
}
RemoteConnector.prototype.define = function(definition) {
var Model = definition.model;
var remotes = this.remotes;
var SharedClass;
assert(Model.sharedClass, 'cannot attach ' + Model.modelName
+ ' to a remote connector without a Model.sharedClass');
remotes.addClass(Model.sharedClass);
Model
.sharedClass
.methods()
.forEach(function(remoteMethod) {
// TODO(ritch) more elegant way of ignoring a nested shared class
if(remoteMethod.name !== 'Change'
&& remoteMethod.name !== 'Checkpoint') {
createProxyMethod(Model, remotes, remoteMethod);
}
});
}
function createProxyMethod(Model, remotes, remoteMethod) {
var scope = remoteMethod.isStatic ? Model : Model.prototype;
var original = scope[remoteMethod.name];
scope[remoteMethod.name] = function remoteMethodProxy() {
var args = Array.prototype.slice.call(arguments);
var lastArgIsFunc = typeof args[args.length - 1] === 'function';
var callback;
if(lastArgIsFunc) {
callback = args.pop();
}
remotes.invoke(remoteMethod.stringName, args, callback);
}
}
function noop() {}

View File

@ -33,16 +33,17 @@
},
"dependencies": {
"async": "~0.9.0",
"bcryptjs": "~2.0.2",
"body-parser": "~1.8.1",
"canonical-json": "0.0.4",
"debug": "~2.0.0",
"ejs": "~1.0.0",
"express": "4.x",
"strong-remoting": "^2.4.0",
"bcryptjs": "~2.0.2",
"debug": "~2.0.0",
"inflection": "~1.4.2",
"loopback-connector-remote": "^1.0.1",
"nodemailer": "~1.3.0",
"nodemailer-stub-transport": "~0.1.4",
"strong-remoting": "^2.4.0",
"uid2": "0.0.3",
"underscore": "~1.7.0",
"underscore.string": "~2.3.3"