Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,30 @@ app.models = function () {
app.dataSource = function (name, config) {
this.dataSources[name] =
this.dataSources[classify(name)] =
this.dataSources[camelize(name)] = dataSourcesFromConfig(config);
this.dataSources[camelize(name)] =
dataSourcesFromConfig(config, this.connectors);
}

/**
* Register a connector.
*
* When a new data-source is being added via `app.dataSource`, the connector
* name is looked up in the registered connectors first.
*
* Connectors are required to be explicitly registered only for applications
* using browserify, because browserify does not support dynamic require,
* which is used by LoopBack to automatically load the connector module.
*
* @param {String} name Name of the connector, e.g. 'mysql'.
* @param {Object} connector Connector object as returned
* by `require('loopback-connector-{name}')`.
*/
app.connector = function(name, connector) {
this.connectors[name] =
this.connectors[classify(name)] =
this.connectors[camelize(name)] = connector;
};

/**
* Get all remote objects.
* @returns {Object} [Remote objects](http://apidocs.strongloop.com/strong-remoting/#remoteobjectsoptions).
Expand Down Expand Up @@ -523,17 +544,22 @@ function camelize(str) {
return stringUtils.camelize(str);
}

function dataSourcesFromConfig(config) {
function dataSourcesFromConfig(config, connectorRegistry) {
var connectorPath;

assert(typeof config === 'object',
'cannont create data source without config object');

if(typeof config.connector === 'string') {
connectorPath = path.join(__dirname, 'connectors', config.connector+'.js');
var name = config.connector;
if (connectorRegistry[name]) {
config.connector = connectorRegistry[name];
} else {
connectorPath = path.join(__dirname, 'connectors', name + '.js');

if(fs.existsSync(connectorPath)) {
config.connector = require(connectorPath);
if (fs.existsSync(connectorPath)) {
config.connector = require(connectorPath);
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ function createApplication() {
// Create a new instance of datasources registry per each app instance
app.datasources = app.dataSources = {};

// 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);

return app;
}

Expand Down
43 changes: 43 additions & 0 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ describe('app', function() {
});
});

describe('app.dataSource', function() {
it('looks up the connector in `app.connectors`', function() {
app.connector('custom', loopback.Memory);
app.dataSource('custom', { connector: 'custom' });
expect(app.dataSources.custom.name).to.equal(loopback.Memory.name);
});
});

describe('app.boot([options])', function () {
beforeEach(function () {
app.boot({
Expand Down Expand Up @@ -502,4 +510,39 @@ describe('app', function() {
});
});
});

describe('app.connectors', function() {
it('is unique per app instance', function() {
app.connectors.foo = 'bar';
var anotherApp = loopback();
expect(anotherApp.connectors.foo).to.equal(undefined);
});

it('includes Remote connector', function() {
expect(app.connectors.remote).to.equal(loopback.Remote);
});

it('includes Memory connector', function() {
expect(app.connectors.memory).to.equal(loopback.Memory);
});
});

describe('app.connector', function() {
// any connector will do
it('adds the connector to the registry', function() {
app.connector('foo-bar', loopback.Memory);
expect(app.connectors['foo-bar']).to.equal(loopback.Memory);
});

it('adds a classified alias', function() {
app.connector('foo-bar', loopback.Memory);
expect(app.connectors.FooBar).to.equal(loopback.Memory);
});

it('adds a camelized alias', function() {
app.connector('FOO-BAR', loopback.Memory);
console.log(app.connectors);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this... but try and keep the test output clean in master. Its really hard to find these sometimes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an oversight, I don't rely on console logs in the tests I write. Fixed in 5b53da9.

expect(app.connectors.FOOBAR).to.equal(loopback.Memory);
});
});
});