Fix the discover methods
This commit is contained in:
parent
184c190223
commit
26230be417
|
@ -14,20 +14,21 @@ var builtinTypes = {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve the type to be a function
|
* Resolve the type string to be a function, for example, 'String' to String
|
||||||
* @param type
|
* @param type The type string, such as 'number', 'Number', 'boolean', or 'String'. It's case insensitive
|
||||||
* @returns {*}
|
* @returns {Function} if the type is resolved
|
||||||
*/
|
*/
|
||||||
function getSchemaType(type) {
|
function getSchemaType(type) {
|
||||||
if (!type) {
|
if (!type) {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
if (Array.isArray(type) && type.length > 0) {
|
if (Array.isArray(type) && type.length > 0) {
|
||||||
|
// For array types, the first item should be the type string
|
||||||
var itemType = getSchemaType(type[0]);
|
var itemType = getSchemaType(type[0]);
|
||||||
if (typeof itemType === 'function') {
|
if (typeof itemType === 'function') {
|
||||||
return [itemType];
|
return [itemType];
|
||||||
}
|
}
|
||||||
else return itemType;
|
else return itemType; // Not resolved, return the type string
|
||||||
}
|
}
|
||||||
if (typeof type === 'string') {
|
if (typeof type === 'string') {
|
||||||
var schemaType = builtinTypes[type.toLowerCase()];
|
var schemaType = builtinTypes[type.toLowerCase()];
|
||||||
|
@ -37,6 +38,7 @@ function getSchemaType(type) {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
} else if (type.constructor.name == 'Object') {
|
} else if (type.constructor.name == 'Object') {
|
||||||
|
// We also support the syntax {type: 'string', ...}
|
||||||
if (type.type) {
|
if (type.type) {
|
||||||
return getSchemaType(type.type);
|
return getSchemaType(type.type);
|
||||||
} else {
|
} else {
|
||||||
|
@ -46,18 +48,18 @@ function getSchemaType(type) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Build a schema
|
||||||
* @param name
|
* @param name The name of the schema
|
||||||
* @param properties
|
* @param properties The properties of the schema
|
||||||
* @param associations
|
* @param associations An array of associations between models
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
function buildSchema(name, properties, associations) {
|
function buildSchema(name, properties, associations) {
|
||||||
for (var p in properties) {
|
for (var p in properties) {
|
||||||
console.log(name + "." + p + ": " + properties[p]);
|
// console.log(name + "." + p + ": " + properties[p]);
|
||||||
var type = getSchemaType(properties[p]);
|
var type = getSchemaType(properties[p]);
|
||||||
if (typeof type === 'string') {
|
if (typeof type === 'string') {
|
||||||
console.log('Association: ' + type);
|
// console.log('Association: ' + type);
|
||||||
associations.push({
|
associations.push({
|
||||||
source: name,
|
source: name,
|
||||||
target: type,
|
target: type,
|
||||||
|
@ -79,19 +81,22 @@ function buildSchema(name, properties, associations) {
|
||||||
* @returns A map of schemas keyed by name
|
* @returns A map of schemas keyed by name
|
||||||
*/
|
*/
|
||||||
function loadSchemasSync(schemaFile, dataSource) {
|
function loadSchemasSync(schemaFile, dataSource) {
|
||||||
|
// Set up the data source
|
||||||
if(!dataSource) {
|
if(!dataSource) {
|
||||||
dataSource = new DataSource('memory');
|
dataSource = new DataSource('memory');
|
||||||
}
|
}
|
||||||
|
|
||||||
var models = {};
|
var models = {};
|
||||||
|
|
||||||
|
// Read the schema JSON file
|
||||||
var schemas = JSON.parse(fs.readFileSync(schemaFile));
|
var schemas = JSON.parse(fs.readFileSync(schemaFile));
|
||||||
if (Array.isArray(schemas)) {
|
if (Array.isArray(schemas)) {
|
||||||
// An array already
|
// An array already
|
||||||
} else if (schemas.properties && schemas.name) {
|
} else if (schemas.properties && schemas.name) {
|
||||||
|
// Only one item
|
||||||
schemas = [schemas];
|
schemas = [schemas];
|
||||||
} else {
|
} else {
|
||||||
|
// Anonymous schema
|
||||||
schemas = [
|
schemas = [
|
||||||
{
|
{
|
||||||
name: 'Anonymous',
|
name: 'Anonymous',
|
||||||
|
@ -103,14 +108,15 @@ function loadSchemasSync(schemaFile, dataSource) {
|
||||||
var associations = [];
|
var associations = [];
|
||||||
for (var s in schemas) {
|
for (var s in schemas) {
|
||||||
var name = schemas[s].name;
|
var name = schemas[s].name;
|
||||||
console.log('Loading ' + name);
|
// console.log('Loading ' + name);
|
||||||
var jdbSchema = buildSchema(name, schemas[s].properties, associations);
|
var jdbSchema = buildSchema(name, schemas[s].properties, associations);
|
||||||
console.dir(jdbSchema);
|
// console.dir(jdbSchema);
|
||||||
var model = dataSource.define(name, jdbSchema);
|
var model = dataSource.define(name, jdbSchema);
|
||||||
console.dir(model);
|
console.dir(model);
|
||||||
models[name.toLowerCase()] = model;
|
models[name.toLowerCase()] = model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect the models based on the associations
|
||||||
for (var i = 0; i < associations.length; i++) {
|
for (var i = 0; i < associations.length; i++) {
|
||||||
var association = associations[i];
|
var association = associations[i];
|
||||||
var sourceModel = models[association.source.toLowerCase()];
|
var sourceModel = models[association.source.toLowerCase()];
|
||||||
|
|
|
@ -282,13 +282,12 @@ DataSource.prototype.discoverModels = function (options, cb) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Discover properties for a given model.
|
* Discover properties for a given model.
|
||||||
* @param options An object that contains the following settings:
|
* @param owner The owner
|
||||||
* model: The model name
|
* @param table The table/view name
|
||||||
* limit: The page size
|
* @param cb Callback
|
||||||
* offset: The starting index
|
|
||||||
* The method return an array of properties, including {owner, tableName, columnName, dataType, dataLength, nullable}
|
* 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();
|
this.freeze();
|
||||||
if (this.adapter.discoverModelProperties) {
|
if (this.adapter.discoverModelProperties) {
|
||||||
this.adapter.discoverModelProperties(options, cb);
|
this.adapter.discoverModelProperties(options, cb);
|
||||||
|
@ -301,15 +300,15 @@ DataSource.prototype.discoverModelProperties = function (options, cb) {
|
||||||
* Discover primary keys for a given owner/table
|
* Discover primary keys for a given owner/table
|
||||||
*
|
*
|
||||||
* Each primary key column description has the following columns:
|
* Each primary key column description has the following columns:
|
||||||
* TABLE_SCHEM String => table schema (may be null)
|
* owner String => table schema (may be null)
|
||||||
* TABLE_NAME String => table name
|
* tableName String => table name
|
||||||
* COLUMN_NAME String => column name
|
* columnName 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).
|
* 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).
|
||||||
* PK_NAME String => primary key name (may be null)
|
* pkName String => primary key name (may be null)
|
||||||
*
|
*
|
||||||
* @param owner
|
* @param owner The owner, default to current user
|
||||||
* @param table
|
* @param table The table name
|
||||||
* @param cb
|
* @param cb Callback
|
||||||
*/
|
*/
|
||||||
DataSource.prototype.discoverPrimaryKeys= function(owner, table, cb) {
|
DataSource.prototype.discoverPrimaryKeys= function(owner, table, cb) {
|
||||||
this.freeze();
|
this.freeze();
|
||||||
|
@ -323,16 +322,16 @@ DataSource.prototype.discoverPrimaryKeys= function(owner, table, cb) {
|
||||||
/**
|
/**
|
||||||
* Discover foreign keys for a given owner/table
|
* Discover foreign keys for a given owner/table
|
||||||
*
|
*
|
||||||
* PKTABLE_SCHEM String => primary key table schema being imported (may be null)
|
* fkOwner String => foreign key table schema (may be null)
|
||||||
* PKTABLE_NAME String => primary key table name being imported
|
* fkName String => foreign key name (may be null)
|
||||||
* PKCOLUMN_NAME String => primary key column name being imported
|
* fkTableName String => foreign key table name
|
||||||
* FKTABLE_CAT String => foreign key table catalog (may be null)
|
* fkColumnName String => foreign key column name
|
||||||
* FKTABLE_SCHEM String => foreign key table schema (may be null)
|
* 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).
|
||||||
* FKTABLE_NAME String => foreign key table name
|
* pkOwner String => primary key table schema being imported (may be null)
|
||||||
* FKCOLUMN_NAME String => foreign key column name
|
* pkName String => primary key name (may be null)
|
||||||
* 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).
|
* pkTableName String => primary key table name being imported
|
||||||
* FK_NAME String => foreign key name (may be null)
|
* pkColumnName String => primary key column name being imported
|
||||||
* PK_NAME String => primary key name (may be null)
|
|
||||||
*
|
*
|
||||||
* @param owner
|
* @param owner
|
||||||
* @param table
|
* @param table
|
||||||
|
|
Loading…
Reference in New Issue