Rename association to relation
This commit is contained in:
parent
31c5504050
commit
995a2139c8
|
@ -173,7 +173,7 @@ can be used to build LoopBack models.
|
||||||
You can also discover and build model classes in one shot:
|
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
|
// 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
|
// Now we have an object of models keyed by the model name
|
||||||
// Find the 1st record for Inventory
|
// Find the 1st record for Inventory
|
||||||
|
|
|
@ -15,9 +15,9 @@
|
||||||
"id": "string",
|
"id": "string",
|
||||||
"customer": {
|
"customer": {
|
||||||
"type": "Customer",
|
"type": "Customer",
|
||||||
"association": {
|
"relation": {
|
||||||
"type": "belongsTo",
|
"type": "belongsTo",
|
||||||
"inverse": "account"
|
"as": "account"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"balance": "number"
|
"balance": "number"
|
||||||
|
|
|
@ -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 {String} modelName The model name
|
||||||
* @param {Object} [options] The options
|
* @param {Object} [options] The options
|
||||||
|
@ -886,7 +886,7 @@ DataSource.prototype.discoverSchema = function (modelName, options, cb) {
|
||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
options.visited = {};
|
options.visited = {};
|
||||||
options.associations = false;
|
options.relations = false;
|
||||||
|
|
||||||
this.discoverSchemas(modelName, options, function(err, schemas) {
|
this.discoverSchemas(modelName, options, function(err, schemas) {
|
||||||
if(err) {
|
if(err) {
|
||||||
|
@ -906,7 +906,7 @@ DataSource.prototype.discoverSchema = function (modelName, options, cb) {
|
||||||
* `options`
|
* `options`
|
||||||
*
|
*
|
||||||
* {String} owner/schema - The database owner/schema name
|
* {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} all - If all owners are included
|
||||||
* {Boolean} views - If views 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.discoverModelProperties.bind(this, modelName, options),
|
||||||
this.discoverPrimaryKeys.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));
|
tasks.push(this.discoverForeignKeys.bind(this, modelName, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1007,7 +1008,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var otherTables = {};
|
var otherTables = {};
|
||||||
if (options.associations) {
|
if (followingRelations) {
|
||||||
// Handle foreign keys
|
// Handle foreign keys
|
||||||
var fks = {};
|
var fks = {};
|
||||||
var foreignKeys = results[2];
|
var foreignKeys = results[2];
|
||||||
|
@ -1033,7 +1034,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
|
||||||
var propName = fromDBName(fk.pkTableName, true);
|
var propName = fromDBName(fk.pkTableName, true);
|
||||||
schema.properties[propName] = {
|
schema.properties[propName] = {
|
||||||
type: fromDBName(fk.pkTableName, false),
|
type: fromDBName(fk.pkTableName, false),
|
||||||
association: {
|
relation: {
|
||||||
type: 'belongsTo',
|
type: 'belongsTo',
|
||||||
foreignKey: fromDBName(fk.pkColumnName, true)
|
foreignKey: fromDBName(fk.pkColumnName, true)
|
||||||
}
|
}
|
||||||
|
@ -1077,7 +1078,7 @@ DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
|
||||||
* `options`
|
* `options`
|
||||||
*
|
*
|
||||||
* {String} owner/schema - The database owner/schema name
|
* {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} all - If all owners are included
|
||||||
* {Boolean} views - If views are included
|
* {Boolean} views - If views are included
|
||||||
*
|
*
|
||||||
|
@ -1154,7 +1155,8 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var otherTables = {};
|
var otherTables = {};
|
||||||
if (options.associations) {
|
var followingRelations = options.associations || options.relations;
|
||||||
|
if (followingRelations) {
|
||||||
// Handle foreign keys
|
// Handle foreign keys
|
||||||
var fks = {};
|
var fks = {};
|
||||||
var foreignKeys = this.discoverForeignKeysSync(modelName, options);
|
var foreignKeys = this.discoverForeignKeysSync(modelName, options);
|
||||||
|
@ -1180,7 +1182,7 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
|
||||||
var propName = fromDBName(fk.pkTableName, true);
|
var propName = fromDBName(fk.pkTableName, true);
|
||||||
schema.properties[propName] = {
|
schema.properties[propName] = {
|
||||||
type: fromDBName(fk.pkTableName, false),
|
type: fromDBName(fk.pkTableName, false),
|
||||||
association: {
|
relation: {
|
||||||
type: 'belongsTo',
|
type: 'belongsTo',
|
||||||
foreignKey: fromDBName(fk.pkColumnName, true)
|
foreignKey: fromDBName(fk.pkColumnName, true)
|
||||||
}
|
}
|
||||||
|
@ -1219,7 +1221,7 @@ DataSource.prototype.discoverSchemasSync = function (modelName, options) {
|
||||||
* `options`
|
* `options`
|
||||||
*
|
*
|
||||||
* {String} owner/schema - The database owner/schema name
|
* {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} all - If all owners are included
|
||||||
* {Boolean} views - If views are included
|
* {Boolean} views - If views are included
|
||||||
*
|
*
|
||||||
|
@ -1252,7 +1254,7 @@ DataSource.prototype.discoverAndBuildModels = function (modelName, options, cb)
|
||||||
* `options`
|
* `options`
|
||||||
*
|
*
|
||||||
* {String} owner/schema - The database owner/schema name
|
* {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} all - If all owners are included
|
||||||
* {Boolean} views - If views are included
|
* {Boolean} views - If views are included
|
||||||
*
|
*
|
||||||
|
|
|
@ -568,23 +568,23 @@ ModelBuilder.prototype.buildModels = function (schemas) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var associations = [];
|
var relations = [];
|
||||||
for (var s in schemas) {
|
for (var s in schemas) {
|
||||||
var name = this.getSchemaName(schemas[s].name);
|
var name = this.getSchemaName(schemas[s].name);
|
||||||
schemas[s].name = name;
|
schemas[s].name = name;
|
||||||
var model = this.define(schemas[s].name, schemas[s].properties, schemas[s].options);
|
var model = this.define(schemas[s].name, schemas[s].properties, schemas[s].options);
|
||||||
models[name] = model;
|
models[name] = model;
|
||||||
associations = associations.concat(model.definition.associations);
|
relations = relations.concat(model.definition.relations);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect the models based on the associations
|
// Connect the models based on the relations
|
||||||
for (var i = 0; i < associations.length; i++) {
|
for (var i = 0; i < relations.length; i++) {
|
||||||
var association = associations[i];
|
var relation = relations[i];
|
||||||
var sourceModel = models[association.source];
|
var sourceModel = models[relation.source];
|
||||||
var targetModel = models[association.target];
|
var targetModel = models[relation.target];
|
||||||
if (sourceModel && targetModel) {
|
if (sourceModel && targetModel) {
|
||||||
if(typeof sourceModel[association.relation] === 'function') {
|
if(typeof sourceModel[relation.type] === 'function') {
|
||||||
sourceModel[association.relation](targetModel, {as: association.as});
|
sourceModel[relation.type](targetModel, {as: relation.as});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ function ModelDefinition(modelBuilder, name, properties, settings) {
|
||||||
this.rawProperties = properties || {}; // Keep the raw property definitions
|
this.rawProperties = properties || {}; // Keep the raw property definitions
|
||||||
this.settings = settings || {};
|
this.settings = settings || {};
|
||||||
}
|
}
|
||||||
this.associations = [];
|
this.relations = [];
|
||||||
this.properties = null;
|
this.properties = null;
|
||||||
this.build();
|
this.build();
|
||||||
}
|
}
|
||||||
|
@ -208,7 +208,7 @@ ModelDefinition.prototype.indexes = function () {
|
||||||
ModelDefinition.prototype.build = function (forceRebuild) {
|
ModelDefinition.prototype.build = function (forceRebuild) {
|
||||||
if(forceRebuild) {
|
if(forceRebuild) {
|
||||||
this.properties = null;
|
this.properties = null;
|
||||||
this.associations = [];
|
this.relations = [];
|
||||||
this._ids = null;
|
this._ids = null;
|
||||||
}
|
}
|
||||||
if (this.properties) {
|
if (this.properties) {
|
||||||
|
@ -219,10 +219,10 @@ ModelDefinition.prototype.build = function (forceRebuild) {
|
||||||
var prop = this.rawProperties[p];
|
var prop = this.rawProperties[p];
|
||||||
var type = this.modelBuilder.resolveType(prop);
|
var type = this.modelBuilder.resolveType(prop);
|
||||||
if (typeof type === 'string') {
|
if (typeof type === 'string') {
|
||||||
this.associations.push({
|
this.relations.push({
|
||||||
source: this.name,
|
source: this.name,
|
||||||
target: type,
|
target: type,
|
||||||
relation: Array.isArray(prop) ? 'hasMany' : 'belongsTo',
|
type: Array.isArray(prop) ? 'hasMany' : 'belongsTo',
|
||||||
as: p
|
as: p
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -15,9 +15,9 @@
|
||||||
"id": "string",
|
"id": "string",
|
||||||
"customer": {
|
"customer": {
|
||||||
"type": "Customer",
|
"type": "Customer",
|
||||||
"association": {
|
"relation": {
|
||||||
"type": "belongsTo",
|
"type": "belongsTo",
|
||||||
"inverse": "account"
|
"as": "account"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"balance": "number"
|
"balance": "number"
|
||||||
|
|
Loading…
Reference in New Issue