From 88f174ee39537910f1052ffffa46ced91f7c56a6 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 21 May 2013 14:25:23 -0700 Subject: [PATCH] Add discoverSchema --- lib/adl-loader.js | 11 +++++++++-- lib/datasource.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/lib/adl-loader.js b/lib/adl-loader.js index 5eb3d56f..824adb16 100644 --- a/lib/adl-loader.js +++ b/lib/adl-loader.js @@ -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; diff --git a/lib/datasource.js b/lib/datasource.js index e88605c2..e5a98cf0 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -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.