Add discoverSchema

This commit is contained in:
Raymond Feng 2013-05-21 14:25:23 -07:00
parent fcdd8c2bca
commit 88f174ee39
2 changed files with 57 additions and 2 deletions

View File

@ -86,10 +86,16 @@ function loadSchemasSync(schemaFile, dataSource) {
dataSource = new DataSource('memory');
}
var models = {};
// Read the schema JSON file
var schemas = JSON.parse(fs.readFileSync(schemaFile));
return parseSchemas(dataSource, schemas);
}
function parseSchemas(dataSource, schemas) {
var models = {};
if (Array.isArray(schemas)) {
// An array already
} else if (schemas.properties && schemas.name) {
@ -129,3 +135,4 @@ function loadSchemasSync(schemaFile, dataSource) {
}
exports.loadSchemasSync = loadSchemasSync;
exports.parseSchemas = parseSchemas;

View File

@ -346,6 +346,54 @@ DataSource.prototype.discoverForeignKeys= function(owner, table, cb) {
}
}
/**
* Discover ADL schema from a given table/view
* @param owner
* @param table
* @param cb
*/
DataSource.prototype.discoverSchema = function(owner, table, cb) {
var dataSourceName = this.name;
this.discoverModelProperties(owner, table, function (err, columns) {
if (err) {
cb && cb(err);
return;
}
if(!columns) {
cb && cb();
return;
}
var schema = {
name: table,
options: {
},
properties: {
}
};
schema.options[dataSourceName] = {
schema: owner,
table: table
};
columns.forEach(function (item) {
var i = item;
schema.properties[item.columnName] =
{
type: item.type,
required: (item.nullable === 'N'),
length: item.dataLength
};
schema.properties[item.columnName][dataSourceName] = {
columnName: i.columnName,
dataType: i.dataType,
nullable: i.nullable
};
});
cb && cb(null, schema);
});
}
/**
* Check whether migrations needed
* This method make sense only for sql adapters.