Enhance support for composite keys

This commit is contained in:
Raymond Feng 2013-05-30 16:06:04 -07:00
parent d9998d8294
commit 8912defe8e
2 changed files with 36 additions and 4 deletions

View File

@ -443,7 +443,7 @@ DataSource.prototype.discoverSchema = function (owner, table, cb) {
pks[pk.columnName] = pk.keySeq;
});
console.log(pks);
// console.log(pks);
var foreignKeys = results[2];
foreignKeys.forEach(function(fk) {
@ -455,7 +455,7 @@ DataSource.prototype.discoverSchema = function (owner, table, cb) {
};
});
console.log(fks);
// console.log(fks);
if (!columns) {
cb && cb();
@ -492,6 +492,7 @@ DataSource.prototype.discoverSchema = function (owner, table, cb) {
schema.properties[propName][dataSourceName] = {
columnName: i.columnName,
dataType: i.dataType,
dataLength: i.dataLength,
nullable: i.nullable
};
});
@ -609,9 +610,9 @@ DataSource.prototype.idColumnName = function(modelName) {
}
/**
* Find the ID column name
* Find the ID property name
* @param modelName
* @returns {String} columnName for ID
* @returns {String} property for ID
*/
DataSource.prototype.idName = function(modelName) {
var props = this.definitions[modelName].properties;
@ -623,6 +624,28 @@ DataSource.prototype.idName = function(modelName) {
return null;
}
/**
* Find the ID property names sorted by the index
* @param 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;
}
/**
* Define foreign key

View File

@ -98,6 +98,15 @@ BaseSQL.prototype.idName = function (model) {
return this.dataSource(model).idName(model);
};
/**
* Get the id property names
* @param model The model name
* @returns {[String]} The id property names
*/
BaseSQL.prototype.idNames = function (model) {
return this.dataSource(model).idNames(model);
};
/**
* Get the id column name
* @param model The model name