Fix copy of model definitions

This commit is contained in:
Raymond Feng 2013-10-02 17:20:54 -07:00
parent c2baf8f05e
commit 691743493c
2 changed files with 15 additions and 66 deletions

View File

@ -2,6 +2,7 @@
* Module dependencies
*/
var ModelBuilder = require('./model-builder.js').ModelBuilder;
var ModelDefinition = require('./model-definition.js');
var jutil = require('./jutil');
var ModelBaseClass = require('./model.js');
var DataAccessObject = require('./dao.js');
@ -421,10 +422,7 @@ DataSource.prototype.attach = function (ModelCtor) {
ModelCtor.dataSource = this;
// add to def
this.definitions[className] = {
properties: properties,
settings: settings
};
this.definitions[className] = new ModelDefinition(this, className, properties, settings);
this.models[className] = ModelCtor;
@ -1182,12 +1180,7 @@ DataSource.prototype.freeze = function freeze() {
* @param {String} modelName The model name
*/
DataSource.prototype.tableName = function (modelName) {
var settings = this.definitions[modelName].settings;
if(settings[this.connector.name]) {
return settings[this.connector.name].table || modelName;
} else {
return modelName;
}
return this.definitions[modelName].tableName(this.connector.name);
};
/**
@ -1197,15 +1190,7 @@ DataSource.prototype.tableName = function (modelName) {
* @returns {String} columnName
*/
DataSource.prototype.columnName = function (modelName, propertyName) {
if(!propertyName) {
return propertyName;
}
var property = this.definitions[modelName].properties[propertyName];
if(property && property[this.connector.name]) {
return property[this.connector.name].columnName || propertyName;
} else {
return propertyName;
}
return this.definitions[modelName].columnName(this.connector.name);
};
/**
@ -1215,15 +1200,7 @@ DataSource.prototype.columnName = function (modelName, propertyName) {
* @returns {Object} column metadata
*/
DataSource.prototype.columnMetadata = function (modelName, propertyName) {
if(!propertyName) {
return propertyName;
}
var property = this.definitions[modelName].properties[propertyName];
if(property && property[this.connector.name]) {
return property[this.connector.name];
} else {
return null;
}
return this.definitions[modelName].columnMetadata(this.connector.name, propertyName);
};
/**
@ -1232,16 +1209,7 @@ DataSource.prototype.columnMetadata = function (modelName, propertyName) {
* @returns {String[]} column names
*/
DataSource.prototype.columnNames = function (modelName) {
var props = this.definitions[modelName].properties;
var cols = [];
for(var p in props) {
if(props[p][this.connector.name]) {
cols.push(props[p][this.connector.name].columnName || p);
} else {
cols.push(p);
}
}
return cols;
return this.definitions[modelName].columnNames(this.connector.name);
};
/**
@ -1250,7 +1218,7 @@ DataSource.prototype.columnNames = function (modelName) {
* @returns {String} columnName for ID
*/
DataSource.prototype.idColumnName = function(modelName) {
return this.columnName(modelName, this.idName(modelName));
return this.definitions[modelName].idColumnName(this.connector.name);
};
/**
@ -1259,16 +1227,10 @@ DataSource.prototype.idColumnName = function(modelName) {
* @returns {String} property name for ID
*/
DataSource.prototype.idName = function(modelName) {
var props = this.definitions[modelName].properties;
if(props.id && props.id.id) {
return 'id';
if(!this.definitions[modelName].idName) {
console.log(this.definitions[modelName]);
}
for(var key in props) {
if(props[key].id) {
return key;
}
}
return null;
return this.definitions[modelName].idName();
};
/**
@ -1277,20 +1239,7 @@ DataSource.prototype.idName = function(modelName) {
* @returns {String[]} property names for IDs
*/
DataSource.prototype.idNames = function (modelName) {
var ids = [];
var props = this.definitions[modelName].properties;
for (var key in props) {
if (props[key].id && props[key].id > 0) {
ids.push({name: key, id: props[key].id});
}
}
ids.sort(function (a, b) {
return a.key - b.key;
});
var names = ids.map(function (id) {
return id.name;
});
return names;
return this.definitions[modelName].idNames();
};
@ -1372,10 +1321,7 @@ DataSource.prototype.copyModel = function copyModel(Master) {
// store class in model pool
dataSource.models[className] = Slave;
dataSource.definitions[className] = {
properties: md.properties,
settings: md.settings
};
dataSource.definitions[className] = new ModelDefinition(dataSource, md.name, md.properties, md.settings);
if ((!dataSource.isTransaction) && dataSource.connector && dataSource.connector.define) {
dataSource.connector.define({

View File

@ -162,6 +162,9 @@ ModelDefinition.prototype.idColumnName = function(connectorType) {
*/
ModelDefinition.prototype.idName = function() {
var id = this.ids()[0];
if(this.properties.id && this.properties.id.id) {
return 'id';
}
return id && id.name;
};