diff --git a/examples/load-schemas.js b/examples/load-schemas.js index 9b1b83bb..148d5d82 100644 --- a/examples/load-schemas.js +++ b/examples/load-schemas.js @@ -16,7 +16,7 @@ function loadSchemasSync(schemaFile, dataSource) { // Read the schema JSON file var schemas = JSON.parse(fs.readFileSync(schemaFile)); - return DataSource.buildModels(dataSource, schemas); + return dataSource.buildModels(schemas); } diff --git a/lib/datasource.js b/lib/datasource.js index c49b8d8d..927254c1 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -538,6 +538,15 @@ DataSource.prototype.discoverSchemas = function (owner, table, options, cb) { } foreignKeys.forEach(function (fk) { + var propName = fromDBName(fk.pkTableName, true); + schema.properties[propName] = { + type: fromDBName(fk.pkTableName, false), + association: { + type: 'belongsTo', + foreignKey: fromDBName(fk.pkColumnName, true) + } + }; + var key = fk.pkOwner + '.' + fk.pkTableName; if (!options.visited.hasOwnProperty(key) && !otherTables.hasOwnProperty(key)) { otherTables[key] = {owner: fk.pkOwner, tableName: fk.pkTableName}; diff --git a/lib/model-builder.js b/lib/model-builder.js index b2455cbb..e719cb82 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -432,14 +432,13 @@ function buildSchema(name, properties, associations) { /** * Build models from schema definitions - * @param modelBuilder The model builder * @param schemas The schemas can be one of the following three formats: * 1. An array of named schema definition JSON objects * 2. A schema definition JSON object * 3. A list of property definitions (anonymous) * @returns {Object} A map of model constructors keyed by model name */ -function buildModels(modelBuilder, schemas) { +ModelBuilder.prototype.buildModels = function (schemas) { var models = {}; if (Array.isArray(schemas)) { @@ -461,15 +460,15 @@ function buildModels(modelBuilder, schemas) { for (var s in schemas) { var name = schemas[s].name; var schema = buildSchema(name, schemas[s].properties, associations); - var model = modelBuilder.define(name, schema); - models[name.toLowerCase()] = model; + var model = this.define(name, schema); + models[name] = model; } // Connect the models based on the associations for (var i = 0; i < associations.length; i++) { var association = associations[i]; - var sourceModel = models[association.source.toLowerCase()]; - var targetModel = models[association.target.toLowerCase()]; + var sourceModel = models[association.source]; + var targetModel = models[association.target]; if (sourceModel && targetModel) { if(typeof sourceModel[association.relation] === 'function') { sourceModel[association.relation](targetModel, {as: association.as}); @@ -479,5 +478,5 @@ function buildModels(modelBuilder, schemas) { return models; } -ModelBuilder.buildModels = buildModels; + diff --git a/test/adl.test.js b/test/adl.test.js index 5034f945..5a83edfd 100644 --- a/test/adl.test.js +++ b/test/adl.test.js @@ -168,23 +168,23 @@ describe('Load models from json', function () { // Read the schema JSON file var schemas = JSON.parse(fs.readFileSync(schemaFile)); - return DataSource.buildModels(dataSource, schemas); + return dataSource.buildModels(schemas); } var models = loadSchemasSync(path.join(__dirname, 'test1-schemas.json')); - models.should.have.property('anonymous'); - models.anonymous.should.have.property('modelName', 'Anonymous'); + models.should.have.property('Anonymous'); + models.Anonymous.should.have.property('modelName', 'Anonymous'); - var m1 = new models.anonymous({title: 'Test'}); + var m1 = new models.Anonymous({title: 'Test'}); m1.should.have.property('title', 'Test'); m1.should.have.property('author', 'Raymond'); models = loadSchemasSync(path.join(__dirname, 'test2-schemas.json')); - models.should.have.property('address'); - models.should.have.property('account'); - models.should.have.property('customer'); + models.should.have.property('Address'); + models.should.have.property('Account'); + models.should.have.property('Customer'); for (var s in models) { var m = models[s]; console.log(m.modelName, new m());