diff --git a/docs/datasource-connector.md b/docs/datasource-connector.md index de3b64f8..f2770534 100644 --- a/docs/datasource-connector.md +++ b/docs/datasource-connector.md @@ -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 diff --git a/examples/schemas.json b/examples/schemas.json index 447726b4..0ca01ff5 100644 --- a/examples/schemas.json +++ b/examples/schemas.json @@ -15,9 +15,9 @@ "id": "string", "customer": { "type": "Customer", - "association": { + "relation": { "type": "belongsTo", - "inverse": "account" + "as": "account" } }, "balance": "number" diff --git a/lib/datasource.js b/lib/datasource.js index 1e53dbeb..70040a28 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -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 * diff --git a/lib/model-builder.js b/lib/model-builder.js index af8f2bcb..b152bbf6 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -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}); } } } diff --git a/lib/model-definition.js b/lib/model-definition.js index fb16dc2a..c13d1cf1 100644 --- a/lib/model-definition.js +++ b/lib/model-definition.js @@ -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 { diff --git a/test/test2-schemas.json b/test/test2-schemas.json index 447726b4..0ca01ff5 100644 --- a/test/test2-schemas.json +++ b/test/test2-schemas.json @@ -15,9 +15,9 @@ "id": "string", "customer": { "type": "Customer", - "association": { + "relation": { "type": "belongsTo", - "inverse": "account" + "as": "account" } }, "balance": "number"