Update buildModels and support associations via foreign keys

This commit is contained in:
Raymond Feng 2013-05-31 10:25:11 -07:00
parent 2d62b5ba6a
commit 7b861c2aac
4 changed files with 23 additions and 15 deletions

View File

@ -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);
}

View File

@ -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};

View File

@ -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;

View File

@ -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());