Merge pull request #40 from strongloop/feature/rename-association

Rename association to relation
This commit is contained in:
Raymond Feng 2013-11-18 11:26:38 -08:00
commit 3354ef72c7
6 changed files with 31 additions and 29 deletions

View File

@ -173,7 +173,7 @@ can be used to build LoopBack models.
You can also discover and build model classes in one shot:
// Start with INVENTORY table and follow the primary/foreign relationships to discover associated tables
ds.discoverAndBuildModels('INVENTORY', {visited: {}, associations: true}, function (err, models) {
ds.discoverAndBuildModels('INVENTORY', {visited: {}, relations: true}, function (err, models) {
// Now we have an object of models keyed by the model name
// Find the 1st record for Inventory

View File

@ -15,9 +15,9 @@
"id": "string",
"customer": {
"type": "Customer",
"association": {
"relation": {
"type": "belongsTo",
"inverse": "account"
"as": "account"
}
},
"balance": "number"

View File

@ -872,7 +872,7 @@ function fromDBName(dbName, camelCase) {
}
/**
* Discover one schema from the given model without following the associations
* Discover one schema from the given model without following the relations
*
* @param {String} modelName The model name
* @param {Object} [options] The options
@ -886,7 +886,7 @@ DataSource.prototype.discoverSchema = function (modelName, options, cb) {
options = {};
}
options.visited = {};
options.associations = false;
options.relations = false;
this.discoverSchemas(modelName, options, function(err, schemas) {
if(err) {
@ -906,7 +906,7 @@ DataSource.prototype.discoverSchema = function (modelName, options, cb) {
* `options`
*
* {String} owner/schema - The database owner/schema name
* {Boolean} associations - If relations (primary key/foreign key) are navigated
* {Boolean} relations - If relations (primary key/foreign key) are navigated
* {Boolean} all - If all owners are included
* {Boolean} views - If views are included
*
@ -929,7 +929,8 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
this.discoverModelProperties.bind(this, modelName, options),
this.discoverPrimaryKeys.bind(this, modelName, options) ];
if (options.associations) {
var followingRelations = options.associations || options.relations;
if (followingRelations) {
tasks.push(this.discoverForeignKeys.bind(this, modelName, options));
}
@ -1007,7 +1008,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
}
var otherTables = {};
if (options.associations) {
if (followingRelations) {
// Handle foreign keys
var fks = {};
var foreignKeys = results[2];
@ -1033,7 +1034,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
var propName = fromDBName(fk.pkTableName, true);
schema.properties[propName] = {
type: fromDBName(fk.pkTableName, false),
association: {
relation: {
type: 'belongsTo',
foreignKey: fromDBName(fk.pkColumnName, true)
}
@ -1077,7 +1078,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
* `options`
*
* {String} owner/schema - The database owner/schema name
* {Boolean} associations - If relations (primary key/foreign key) are navigated
* {Boolean} relations - If relations (primary key/foreign key) are navigated
* {Boolean} all - If all owners are included
* {Boolean} views - If views are included
*
@ -1154,7 +1155,8 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
}
var otherTables = {};
if (options.associations) {
var followingRelations = options.associations || options.relations;
if (followingRelations) {
// Handle foreign keys
var fks = {};
var foreignKeys = this.discoverForeignKeysSync(modelName, options);
@ -1180,7 +1182,7 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
var propName = fromDBName(fk.pkTableName, true);
schema.properties[propName] = {
type: fromDBName(fk.pkTableName, false),
association: {
relation: {
type: 'belongsTo',
foreignKey: fromDBName(fk.pkColumnName, true)
}
@ -1219,7 +1221,7 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
* `options`
*
* {String} owner/schema - The database owner/schema name
* {Boolean} associations - If relations (primary key/foreign key) are navigated
* {Boolean} relations - If relations (primary key/foreign key) are navigated
* {Boolean} all - If all owners are included
* {Boolean} views - If views are included
*
@ -1252,7 +1254,7 @@ DataSource.prototype.discoverAndBuildModels = function (modelName, options, cb)
* `options`
*
* {String} owner/schema - The database owner/schema name
* {Boolean} associations - If relations (primary key/foreign key) are navigated
* {Boolean} relations - If relations (primary key/foreign key) are navigated
* {Boolean} all - If all owners are included
* {Boolean} views - If views are included
*

View File

@ -568,23 +568,23 @@ ModelBuilder.prototype.buildModels = function (schemas) {
}
}
var associations = [];
var relations = [];
for (var s in schemas) {
var name = this.getSchemaName(schemas[s].name);
schemas[s].name = name;
var model = this.define(schemas[s].name, schemas[s].properties, schemas[s].options);
models[name] = model;
associations = associations.concat(model.definition.associations);
relations = relations.concat(model.definition.relations);
}
// Connect the models based on the associations
for (var i = 0; i < associations.length; i++) {
var association = associations[i];
var sourceModel = models[association.source];
var targetModel = models[association.target];
// Connect the models based on the relations
for (var i = 0; i < relations.length; i++) {
var relation = relations[i];
var sourceModel = models[relation.source];
var targetModel = models[relation.target];
if (sourceModel && targetModel) {
if(typeof sourceModel[association.relation] === 'function') {
sourceModel[association.relation](targetModel, {as: association.as});
if(typeof sourceModel[relation.type] === 'function') {
sourceModel[relation.type](targetModel, {as: relation.as});
}
}
}

View File

@ -39,7 +39,7 @@ function ModelDefinition(modelBuilder, name, properties, settings) {
this.rawProperties = properties || {}; // Keep the raw property definitions
this.settings = settings || {};
}
this.associations = [];
this.relations = [];
this.properties = null;
this.build();
}
@ -208,7 +208,7 @@ ModelDefinition.prototype.indexes = function () {
ModelDefinition.prototype.build = function (forceRebuild) {
if(forceRebuild) {
this.properties = null;
this.associations = [];
this.relations = [];
this._ids = null;
}
if (this.properties) {
@ -219,10 +219,10 @@ ModelDefinition.prototype.build = function (forceRebuild) {
var prop = this.rawProperties[p];
var type = this.modelBuilder.resolveType(prop);
if (typeof type === 'string') {
this.associations.push({
this.relations.push({
source: this.name,
target: type,
relation: Array.isArray(prop) ? 'hasMany' : 'belongsTo',
type: Array.isArray(prop) ? 'hasMany' : 'belongsTo',
as: p
});
} else {

View File

@ -15,9 +15,9 @@
"id": "string",
"customer": {
"type": "Customer",
"association": {
"relation": {
"type": "belongsTo",
"inverse": "account"
"as": "account"
}
},
"balance": "number"