Merge pull request #48 from strongloop/feature/fix-discovery

Attach models to the data source
This commit is contained in:
Raymond Feng 2013-12-05 15:40:43 -08:00
commit 2e4ed3f057
3 changed files with 18 additions and 16 deletions

View File

@ -234,7 +234,7 @@ DataSource.prototype.setup = function(name, settings) {
} }
connector = require(name); connector = require(name);
} catch (e) { } catch (e) {
return console.log('\nWARNING: LoopbackData connector "' + name + '" is not installed,\nso your models would not work, to fix run:\n\n npm install ' + name, '\n'); return console.log('\nWARNING: LoopBack connector "' + name + '" is not installed,\nso your models would not work, to fix run:\n\n npm install ' + name, '\n');
} }
} }
} }
@ -922,7 +922,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
} }
var self = this; var self = this;
var schemaName = this.name || this.connector.name; var schemaName = this.connector.name || this.name;
var tasks = [ var tasks = [
this.discoverModelProperties.bind(this, modelName, options), this.discoverModelProperties.bind(this, modelName, options),
@ -1029,14 +1029,13 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
console.log('Foreign keys: ', fks); console.log('Foreign keys: ', fks);
} }
schema.options.relations = {};
foreignKeys.forEach(function (fk) { foreignKeys.forEach(function (fk) {
var propName = fromDBName(fk.pkTableName, true); var propName = fromDBName(fk.pkTableName, true);
schema.properties[propName] = { schema.options.relations[propName] = {
type: fromDBName(fk.pkTableName, false), model: fromDBName(fk.pkTableName, false),
relation: { type: 'belongsTo',
type: 'belongsTo', foreignKey: fromDBName(fk.fkColumnName, true)
foreignKey: fromDBName(fk.pkColumnName, true)
}
}; };
var key = fk.pkOwner + '.' + fk.pkTableName; var key = fk.pkOwner + '.' + fk.pkTableName;
@ -1177,14 +1176,13 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
console.log('Foreign keys: ', fks); console.log('Foreign keys: ', fks);
} }
schema.options.relations = {};
foreignKeys.forEach(function (fk) { foreignKeys.forEach(function (fk) {
var propName = fromDBName(fk.pkTableName, true); var propName = fromDBName(fk.pkTableName, true);
schema.properties[propName] = { schema.options.relations[propName] = {
type: fromDBName(fk.pkTableName, false), model: fromDBName(fk.pkTableName, false),
relation: { type: 'belongsTo',
type: 'belongsTo', foreignKey: fromDBName(fk.fkColumnName, true)
foreignKey: fromDBName(fk.pkColumnName, true)
}
}; };
var key = fk.pkOwner + '.' + fk.pkTableName; var key = fk.pkOwner + '.' + fk.pkTableName;
@ -1243,6 +1241,10 @@ DataSource.prototype.discoverAndBuildModels = function (modelName, options, cb)
} }
var models = self.modelBuilder.buildModels(schemaList); var models = self.modelBuilder.buildModels(schemaList);
// Now attach the models to the data source
for(var m in models) {
models[m].attachTo(self);
}
cb && cb(err, models); cb && cb(err, models);
}); });
}; };

View File

@ -195,7 +195,7 @@ Relation.belongsTo = function (anotherClass, params) {
} }
} }
var idName = this.dataSource.idName(this.modelName) || 'id'; var idName = this.dataSource.idName(anotherClass.modelName) || 'id';
var methodName = params.as || i8n.camelize(anotherClass.modelName, true); var methodName = params.as || i8n.camelize(anotherClass.modelName, true);
var fk = params.foreignKey || methodName + 'Id'; var fk = params.foreignKey || methodName + 'Id';

View File

@ -1,6 +1,6 @@
{ {
"name": "loopback-datasource-juggler", "name": "loopback-datasource-juggler",
"version": "1.2.5", "version": "1.2.6",
"description": "LoopBack DataSoure Juggler", "description": "LoopBack DataSoure Juggler",
"keywords": [ "keywords": [
"StrongLoop", "StrongLoop",