Fix copy of model definitions
This commit is contained in:
parent
c2baf8f05e
commit
691743493c
|
@ -2,6 +2,7 @@
|
||||||
* Module dependencies
|
* Module dependencies
|
||||||
*/
|
*/
|
||||||
var ModelBuilder = require('./model-builder.js').ModelBuilder;
|
var ModelBuilder = require('./model-builder.js').ModelBuilder;
|
||||||
|
var ModelDefinition = require('./model-definition.js');
|
||||||
var jutil = require('./jutil');
|
var jutil = require('./jutil');
|
||||||
var ModelBaseClass = require('./model.js');
|
var ModelBaseClass = require('./model.js');
|
||||||
var DataAccessObject = require('./dao.js');
|
var DataAccessObject = require('./dao.js');
|
||||||
|
@ -421,10 +422,7 @@ DataSource.prototype.attach = function (ModelCtor) {
|
||||||
ModelCtor.dataSource = this;
|
ModelCtor.dataSource = this;
|
||||||
|
|
||||||
// add to def
|
// add to def
|
||||||
this.definitions[className] = {
|
this.definitions[className] = new ModelDefinition(this, className, properties, settings);
|
||||||
properties: properties,
|
|
||||||
settings: settings
|
|
||||||
};
|
|
||||||
|
|
||||||
this.models[className] = ModelCtor;
|
this.models[className] = ModelCtor;
|
||||||
|
|
||||||
|
@ -1182,12 +1180,7 @@ DataSource.prototype.freeze = function freeze() {
|
||||||
* @param {String} modelName The model name
|
* @param {String} modelName The model name
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.tableName = function (modelName) {
|
DataSource.prototype.tableName = function (modelName) {
|
||||||
var settings = this.definitions[modelName].settings;
|
return this.definitions[modelName].tableName(this.connector.name);
|
||||||
if(settings[this.connector.name]) {
|
|
||||||
return settings[this.connector.name].table || modelName;
|
|
||||||
} else {
|
|
||||||
return modelName;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1197,15 +1190,7 @@ DataSource.prototype.tableName = function (modelName) {
|
||||||
* @returns {String} columnName
|
* @returns {String} columnName
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.columnName = function (modelName, propertyName) {
|
DataSource.prototype.columnName = function (modelName, propertyName) {
|
||||||
if(!propertyName) {
|
return this.definitions[modelName].columnName(this.connector.name);
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1215,15 +1200,7 @@ DataSource.prototype.columnName = function (modelName, propertyName) {
|
||||||
* @returns {Object} column metadata
|
* @returns {Object} column metadata
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.columnMetadata = function (modelName, propertyName) {
|
DataSource.prototype.columnMetadata = function (modelName, propertyName) {
|
||||||
if(!propertyName) {
|
return this.definitions[modelName].columnMetadata(this.connector.name, propertyName);
|
||||||
return propertyName;
|
|
||||||
}
|
|
||||||
var property = this.definitions[modelName].properties[propertyName];
|
|
||||||
if(property && property[this.connector.name]) {
|
|
||||||
return property[this.connector.name];
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1232,16 +1209,7 @@ DataSource.prototype.columnMetadata = function (modelName, propertyName) {
|
||||||
* @returns {String[]} column names
|
* @returns {String[]} column names
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.columnNames = function (modelName) {
|
DataSource.prototype.columnNames = function (modelName) {
|
||||||
var props = this.definitions[modelName].properties;
|
return this.definitions[modelName].columnNames(this.connector.name);
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1250,7 +1218,7 @@ DataSource.prototype.columnNames = function (modelName) {
|
||||||
* @returns {String} columnName for ID
|
* @returns {String} columnName for ID
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.idColumnName = function(modelName) {
|
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
|
* @returns {String} property name for ID
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.idName = function(modelName) {
|
DataSource.prototype.idName = function(modelName) {
|
||||||
var props = this.definitions[modelName].properties;
|
if(!this.definitions[modelName].idName) {
|
||||||
if(props.id && props.id.id) {
|
console.log(this.definitions[modelName]);
|
||||||
return 'id';
|
|
||||||
}
|
}
|
||||||
for(var key in props) {
|
return this.definitions[modelName].idName();
|
||||||
if(props[key].id) {
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1277,20 +1239,7 @@ DataSource.prototype.idName = function(modelName) {
|
||||||
* @returns {String[]} property names for IDs
|
* @returns {String[]} property names for IDs
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.idNames = function (modelName) {
|
DataSource.prototype.idNames = function (modelName) {
|
||||||
var ids = [];
|
return this.definitions[modelName].idNames();
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -1372,10 +1321,7 @@ DataSource.prototype.copyModel = function copyModel(Master) {
|
||||||
|
|
||||||
// store class in model pool
|
// store class in model pool
|
||||||
dataSource.models[className] = Slave;
|
dataSource.models[className] = Slave;
|
||||||
dataSource.definitions[className] = {
|
dataSource.definitions[className] = new ModelDefinition(dataSource, md.name, md.properties, md.settings);
|
||||||
properties: md.properties,
|
|
||||||
settings: md.settings
|
|
||||||
};
|
|
||||||
|
|
||||||
if ((!dataSource.isTransaction) && dataSource.connector && dataSource.connector.define) {
|
if ((!dataSource.isTransaction) && dataSource.connector && dataSource.connector.define) {
|
||||||
dataSource.connector.define({
|
dataSource.connector.define({
|
||||||
|
|
|
@ -162,6 +162,9 @@ ModelDefinition.prototype.idColumnName = function(connectorType) {
|
||||||
*/
|
*/
|
||||||
ModelDefinition.prototype.idName = function() {
|
ModelDefinition.prototype.idName = function() {
|
||||||
var id = this.ids()[0];
|
var id = this.ids()[0];
|
||||||
|
if(this.properties.id && this.properties.id.id) {
|
||||||
|
return 'id';
|
||||||
|
}
|
||||||
return id && id.name;
|
return id && id.name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue