Update the discover apis to take options

This commit is contained in:
Raymond Feng 2013-06-20 15:50:55 -07:00
parent bc62d28b12
commit a6ad39ba0f
1 changed files with 90 additions and 63 deletions

View File

@ -441,30 +441,30 @@ DataSource.prototype.discoverModelDefinitionsSync = function (options) {
/**
* Discover properties for a given model.
* @param owner The 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 (owner, table, cb) {
DataSource.prototype.discoverModelProperties = function (table, options, cb) {
this.freeze();
if (this.adapter.discoverModelProperties) {
this.adapter.discoverModelProperties(owner, table, cb);
this.adapter.discoverModelProperties(table, options, cb);
} else if (cb) {
cb();
}
};
DataSource.prototype.discoverModelPropertiesSync = function (owner, table) {
DataSource.prototype.discoverModelPropertiesSync = function (modelName, options) {
this.freeze();
if (this.adapter.discoverModelPropertiesSync) {
return this.adapter.discoverModelPropertiesSync(owner, table);
return this.adapter.discoverModelPropertiesSync(modelName, options);
}
return null;
};
/**
* Discover primary keys for a given owner/table
* Discover primary keys for a given owner/modelName
*
* Each primary key column description has the following columns:
* owner String => table schema (may be null)
@ -473,29 +473,29 @@ DataSource.prototype.discoverModelPropertiesSync = function (owner, table) {
* 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 The owner, default to current user
* @param table The table name
* The owner, default to current user
* @param modelName The table name
* @param cb Callback
*/
DataSource.prototype.discoverPrimaryKeys= function(owner, table, cb) {
DataSource.prototype.discoverPrimaryKeys= function(modelName, options, cb) {
this.freeze();
if (this.adapter.discoverPrimaryKeys) {
this.adapter.discoverPrimaryKeys(owner, table, cb);
this.adapter.discoverPrimaryKeys(modelName, options, cb);
} else if (cb) {
cb();
}
}
DataSource.prototype.discoverPrimaryKeysSync= function(owner, table) {
DataSource.prototype.discoverPrimaryKeysSync= function(modelName, options) {
this.freeze();
if (this.adapter.discoverPrimaryKeysSync) {
return this.adapter.discoverPrimaryKeysSync(owner, table);
return this.adapter.discoverPrimaryKeysSync(modelName, options);
}
return null;
}
/**
* Discover foreign keys for a given owner/table
* Discover foreign keys for a given owner/modelName
*
* fkOwner String => foreign key table schema (may be null)
* fkName String => foreign key name (may be null)
@ -508,23 +508,23 @@ DataSource.prototype.discoverPrimaryKeysSync= function(owner, table) {
* pkColumnName String => primary key column name being imported
*
* @param owner
* @param table
*
* @param modelName
* @param cb
*/
DataSource.prototype.discoverForeignKeys= function(owner, table, cb) {
DataSource.prototype.discoverForeignKeys= function(modelName, options, cb) {
this.freeze();
if (this.adapter.discoverForeignKeys) {
this.adapter.discoverForeignKeys(owner, table, cb);
this.adapter.discoverForeignKeys(modelName, options, cb);
} else if (cb) {
cb();
}
}
DataSource.prototype.discoverForeignKeysSync= function(owner, table) {
DataSource.prototype.discoverForeignKeysSync= function(modelName, options) {
this.freeze();
if (this.adapter.discoverForeignKeysSync) {
return this.adapter.discoverForeignKeysSync(owner, table);
return this.adapter.discoverForeignKeysSync(modelName, options);
}
return null;
}
@ -543,23 +543,23 @@ DataSource.prototype.discoverForeignKeysSync= function(owner, table) {
* pkTableName String => primary key table name being imported
* pkColumnName String => primary key column name being imported
*
* @param owner
* @param table
*
* @param modelName
* @param cb
*/
DataSource.prototype.discoverExportedForeignKeys= function(owner, table, cb) {
DataSource.prototype.discoverExportedForeignKeys= function(modelName, options, cb) {
this.freeze();
if (this.adapter.discoverExportedForeignKeys) {
this.adapter.discoverExportedForeignKeys(owner, table, cb);
this.adapter.discoverExportedForeignKeys(modelName, options, cb);
} else if (cb) {
cb();
}
}
DataSource.prototype.discoverExportedForeignKeysSync= function(owner, table) {
DataSource.prototype.discoverExportedForeignKeysSync= function(modelName, options) {
this.freeze();
if (this.adapter.discoverExportedForeignKeysSync) {
return this.adapter.discoverExportedForeignKeysSync(owner, table);
return this.adapter.discoverExportedForeignKeysSync(modelName, options);
}
return null;
}
@ -584,8 +584,17 @@ function fromDBName(dbName, camelCase) {
return parts.join('');
}
DataSource.prototype.discoverSchema = function (owner, tableOrView, cb) {
this.discoverSchemas(owner, tableOrView, {visited: {}, associations: false}, function(err, schemas) {
DataSource.prototype.discoverSchema = function (modelName, options, cb) {
options = options || {};
if(!cb && 'function' === typeof options) {
cb = options;
options = {};
}
options.visited = {};
options.associations = false;
this.discoverSchemas(modelName, options, function(err, schemas) {
if(err) {
cb && cb(err, schemas);
return;
@ -598,21 +607,28 @@ DataSource.prototype.discoverSchema = function (owner, tableOrView, cb) {
}
/**
* Discover schema from a given table/view
* @param owner
* @param table
* Discover schema from a given modelName/view
*
* @param modelName
* @param cb
*/
DataSource.prototype.discoverSchemas = function (owner, tableOrView, options, cb) {
DataSource.prototype.discoverSchemas = function (modelName, options, cb) {
options = options || {};
if(!cb && 'function' === typeof options) {
cb = options;
options = {};
}
var self = this;
var dataSourceName = this.name || this.adapter.name;
var tasks = [
this.discoverModelProperties.bind(this, owner, tableOrView),
this.discoverPrimaryKeys.bind(this, owner, tableOrView) ];
this.discoverModelProperties.bind(this, modelName, options),
this.discoverPrimaryKeys.bind(this, modelName, options) ];
if (options.associations) {
tasks.push(this.discoverForeignKeys.bind(this, owner, tableOrView));
tasks.push(this.discoverForeignKeys.bind(this, modelName, options));
}
async.parallel(tasks, function (err, results) {
@ -623,7 +639,7 @@ DataSource.prototype.discoverSchemas = function (owner, tableOrView, options, cb
}
var columns = results[0];
if (!columns) {
if (!columns || columns.length === 0) {
cb && cb();
return;
}
@ -640,7 +656,7 @@ DataSource.prototype.discoverSchemas = function (owner, tableOrView, options, cb
}
var schema = {
name: fromDBName(tableOrView, false),
name: fromDBName(modelName, false),
options: {
idInjection: false // DO NOT add id property
},
@ -650,7 +666,7 @@ DataSource.prototype.discoverSchemas = function (owner, tableOrView, options, cb
schema.options[dataSourceName] = {
schema: columns[0].owner,
table: tableOrView
table: modelName
};
columns.forEach(function (item) {
@ -674,9 +690,9 @@ DataSource.prototype.discoverSchemas = function (owner, tableOrView, options, cb
};
});
// Add current tableOrView to the visited tables
// Add current modelName to the visited tables
options.visited = options.visited || {};
var schemaKey = columns[0].owner + '.' + tableOrView;
var schemaKey = columns[0].owner + '.' + modelName;
if (!options.visited.hasOwnProperty(schemaKey)) {
if(self.settings.debug) {
console.log('Adding schema for ' + schemaKey);
@ -732,7 +748,13 @@ DataSource.prototype.discoverSchemas = function (owner, tableOrView, options, cb
if(self.settings.debug) {
console.log('Discovering related schema for ' + schemaKey);
}
moreTasks.push(DataSource.prototype.discoverSchemas.bind(self, otherTables[t].owner, otherTables[t].tableName, options));
var newOptions = {};
for(var key in options) {
newOptions[key] = options[key];
}
newOptions.owner = otherTables[t].owner;
moreTasks.push(DataSource.prototype.discoverSchemas.bind(self, otherTables[t].tableName, newOptions));
}
async.parallel(moreTasks, function (err, results) {
var result = results && results[0];
@ -745,21 +767,21 @@ DataSource.prototype.discoverSchemas = function (owner, tableOrView, options, cb
/**
* Discover schema from a given table/view
* @param owner
* @param table
*
* @param modelName
* @param cb
*/
DataSource.prototype.discoverSchemasSync = function (owner, tableOrView, options) {
DataSource.prototype.discoverSchemasSync = function (modelName, options) {
var self = this;
var dataSourceName = this.name || this.adapter.name;
var columns = this.discoverModelPropertiesSync(owner, tableOrView);
if (!columns) {
var columns = this.discoverModelPropertiesSync(modelName, options);
if (!columns || columns.length === 0) {
return [];
}
// Handle primary keys
var primaryKeys = this.discoverPrimaryKeysSync(owner, tableOrView);
var primaryKeys = this.discoverPrimaryKeysSync(modelName, options);
var pks = {};
primaryKeys.forEach(function (pk) {
pks[pk.columnName] = pk.keySeq;
@ -770,7 +792,7 @@ DataSource.prototype.discoverSchemasSync = function (owner, tableOrView, options
}
var schema = {
name: fromDBName(tableOrView, false),
name: fromDBName(modelName, false),
options: {
idInjection: false // DO NOT add id property
},
@ -779,8 +801,8 @@ DataSource.prototype.discoverSchemasSync = function (owner, tableOrView, options
};
schema.options[dataSourceName] = {
schema: columns[0].owner,
table: tableOrView
schema: columns.length > 0 && columns[0].owner,
table: modelName
};
columns.forEach(function (item) {
@ -804,9 +826,9 @@ DataSource.prototype.discoverSchemasSync = function (owner, tableOrView, options
};
});
// Add current tableOrView to the visited tables
// Add current modelName to the visited tables
options.visited = options.visited || {};
var schemaKey = columns[0].owner + '.' + tableOrView;
var schemaKey = columns[0].owner + '.' + modelName;
if (!options.visited.hasOwnProperty(schemaKey)) {
if (self.settings.debug) {
console.log('Adding schema for ' + schemaKey);
@ -818,7 +840,7 @@ DataSource.prototype.discoverSchemasSync = function (owner, tableOrView, options
if (options.associations) {
// Handle foreign keys
var fks = {};
var foreignKeys = this.discoverForeignKeysSync(owner, tableOrView);
var foreignKeys = this.discoverForeignKeysSync(modelName, options);
foreignKeys.forEach(function (fk) {
var fkInfo = {
keySeq: fk.keySeq,
@ -862,7 +884,12 @@ DataSource.prototype.discoverSchemasSync = function (owner, tableOrView, options
if (self.settings.debug) {
console.log('Discovering related schema for ' + schemaKey);
}
self.discoverSchemasSync(otherTables[t].owner, otherTables[t].tableName, options);
var newOptions = {};
for(var key in options) {
newOptions[key] = options[key];
}
newOptions.owner = otherTables[t].owner;
self.discoverSchemasSync(otherTables[t].tableName, newOptions);
}
return options.visited;
@ -870,15 +897,15 @@ DataSource.prototype.discoverSchemasSync = function (owner, tableOrView, options
}
/**
* Discover and build models from the given owner/tableOrView
* @param owner
* @param tableOrView
* Discover and build models from the given owner/modelName
*
* @param modelName
* @param options
* @param cb
*/
DataSource.prototype.discoverAndBuildModels = function (owner, tableOrView, options, cb) {
DataSource.prototype.discoverAndBuildModels = function (modelName, options, cb) {
var self = this;
this.discoverSchemas(owner, tableOrView, options, function (err, schemas) {
this.discoverSchemas(modelName, options, function (err, schemas) {
if (err) {
cb && cb(err, schemas);
return;
@ -896,13 +923,13 @@ DataSource.prototype.discoverAndBuildModels = function (owner, tableOrView, opti
}
/**
* Discover and build models from the given owner/tableOrView synchronously
* @param owner
* @param tableOrView
* Discover and build models from the given owner/modelName synchronously
*
* @param modelName
* @param options
*/
DataSource.prototype.discoverAndBuildModelsSync = function (owner, tableOrView, options) {
var schemas = this.discoverSchemasSync(owner, tableOrView, options);
DataSource.prototype.discoverAndBuildModelsSync = function (modelName, options) {
var schemas = this.discoverSchemasSync(modelName, options);
var schemaList = [];
for (var s in schemas) {