Fix the discover methods

This commit is contained in:
Raymond Feng 2013-05-21 11:59:16 -07:00
parent 184c190223
commit 26230be417
2 changed files with 41 additions and 36 deletions

View File

@ -14,20 +14,21 @@ var builtinTypes = {
}
/**
* Resolve the type to be a function
* @param type
* @returns {*}
* Resolve the type string to be a function, for example, 'String' to String
* @param type The type string, such as 'number', 'Number', 'boolean', or 'String'. It's case insensitive
* @returns {Function} if the type is resolved
*/
function getSchemaType(type) {
if (!type) {
return type;
}
if (Array.isArray(type) && type.length > 0) {
// For array types, the first item should be the type string
var itemType = getSchemaType(type[0]);
if (typeof itemType === 'function') {
return [itemType];
}
else return itemType;
else return itemType; // Not resolved, return the type string
}
if (typeof type === 'string') {
var schemaType = builtinTypes[type.toLowerCase()];
@ -37,6 +38,7 @@ function getSchemaType(type) {
return type;
}
} else if (type.constructor.name == 'Object') {
// We also support the syntax {type: 'string', ...}
if (type.type) {
return getSchemaType(type.type);
} else {
@ -46,18 +48,18 @@ function getSchemaType(type) {
}
/**
*
* @param name
* @param properties
* @param associations
* Build a schema
* @param name The name of the schema
* @param properties The properties of the schema
* @param associations An array of associations between models
* @returns {*}
*/
function buildSchema(name, properties, associations) {
for (var p in properties) {
console.log(name + "." + p + ": " + properties[p]);
// console.log(name + "." + p + ": " + properties[p]);
var type = getSchemaType(properties[p]);
if (typeof type === 'string') {
console.log('Association: ' + type);
// console.log('Association: ' + type);
associations.push({
source: name,
target: type,
@ -79,19 +81,22 @@ function buildSchema(name, properties, associations) {
* @returns A map of schemas keyed by name
*/
function loadSchemasSync(schemaFile, dataSource) {
// Set up the data source
if(!dataSource) {
dataSource = new DataSource('memory');
}
var models = {};
// Read the schema JSON file
var schemas = JSON.parse(fs.readFileSync(schemaFile));
if (Array.isArray(schemas)) {
// An array already
} else if (schemas.properties && schemas.name) {
// Only one item
schemas = [schemas];
} else {
// Anonymous schema
schemas = [
{
name: 'Anonymous',
@ -103,14 +108,15 @@ function loadSchemasSync(schemaFile, dataSource) {
var associations = [];
for (var s in schemas) {
var name = schemas[s].name;
console.log('Loading ' + name);
// console.log('Loading ' + name);
var jdbSchema = buildSchema(name, schemas[s].properties, associations);
console.dir(jdbSchema);
// console.dir(jdbSchema);
var model = dataSource.define(name, jdbSchema);
console.dir(model);
models[name.toLowerCase()] = 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()];

View File

@ -282,13 +282,12 @@ DataSource.prototype.discoverModels = function (options, cb) {
/**
* Discover properties for a given model.
* @param options An object that contains the following settings:
* model: The model name
* limit: The page size
* offset: The starting index
* @param owner The owner
* @param table The table/view name
* @param cb Callback
* The method return an array of properties, including {owner, tableName, columnName, dataType, dataLength, nullable}
*/
DataSource.prototype.discoverModelProperties = function (options, cb) {
DataSource.prototype.discoverModelProperties = function (owner, table, cb) {
this.freeze();
if (this.adapter.discoverModelProperties) {
this.adapter.discoverModelProperties(options, cb);
@ -301,15 +300,15 @@ DataSource.prototype.discoverModelProperties = function (options, cb) {
* Discover primary keys for a given owner/table
*
* Each primary key column description has the following columns:
* TABLE_SCHEM String => table schema (may be null)
* TABLE_NAME String => table name
* COLUMN_NAME String => column name
* KEY_SEQ short => sequence number within primary key( a value of 1 represents the first column of the primary key, a value of 2 would represent the second column within the primary key).
* PK_NAME String => primary key name (may be null)
* owner String => table schema (may be null)
* tableName String => table name
* columnName String => column name
* keySeq Number => sequence number within primary key( a value of 1 represents the first column of the primary key, a value of 2 would represent the second column within the primary key).
* pkName String => primary key name (may be null)
*
* @param owner
* @param table
* @param cb
* @param owner The owner, default to current user
* @param table The table name
* @param cb Callback
*/
DataSource.prototype.discoverPrimaryKeys= function(owner, table, cb) {
this.freeze();
@ -323,16 +322,16 @@ DataSource.prototype.discoverPrimaryKeys= function(owner, table, cb) {
/**
* Discover foreign keys for a given owner/table
*
* PKTABLE_SCHEM String => primary key table schema being imported (may be null)
* PKTABLE_NAME String => primary key table name being imported
* PKCOLUMN_NAME String => primary key column name being imported
* FKTABLE_CAT String => foreign key table catalog (may be null)
* FKTABLE_SCHEM String => foreign key table schema (may be null)
* FKTABLE_NAME String => foreign key table name
* FKCOLUMN_NAME String => foreign key column name
* KEY_SEQ short => sequence number within a foreign key( a value of 1 represents the first column of the foreign key, a value of 2 would represent the second column within the foreign key).
* FK_NAME String => foreign key name (may be null)
* PK_NAME String => primary key name (may be null)
* fkOwner String => foreign key table schema (may be null)
* fkName String => foreign key name (may be null)
* fkTableName String => foreign key table name
* fkColumnName String => foreign key column name
* keySeq short => sequence number within a foreign key( a value of 1 represents the first column of the foreign key, a value of 2 would represent the second column within the foreign key).
* pkOwner String => primary key table schema being imported (may be null)
* pkName String => primary key name (may be null)
* pkTableName String => primary key table name being imported
* pkColumnName String => primary key column name being imported
*
* @param owner
* @param table