Start to add discoverSchema and name mapping

This commit is contained in:
Raymond Feng 2013-05-22 10:41:08 -07:00
parent aebd7de68c
commit 5b32141e31
4 changed files with 160 additions and 41 deletions

View File

@ -131,11 +131,28 @@ Schema.prototype.define = function defineClass(className, properties, settings)
settings: settings settings: settings
}; };
ModelClass.prototype.__defineGetter__('id', function () { var idInjection = settings.idInjection;
return this.__data.id; for(var p in properties) {
}); if(properties[p].id) {
idInjection = false;
ModelClass.prototype.__defineGetter__('id', function () {
return this.__data[p];
});
break;
}
}
// Add the id property
if (idInjection !== false) {
ModelClass.prototype.__defineGetter__('id', function () {
return this.__data.id;
});
properties.id = properties.id || { type: Number }; // Set up the id property
properties.id = properties.id || { type: Number, id: true };
if (!properties.id.id) {
properties.id.id = true;
}
}
ModelClass.forEachProperty = function (cb) { ModelClass.forEachProperty = function (cb) {
Object.keys(properties).forEach(cb); Object.keys(properties).forEach(cb);

View File

@ -9,6 +9,7 @@ var EventEmitter = require('events').EventEmitter;
var util = require('util'); var util = require('util');
var path = require('path'); var path = require('path');
var fs = require('fs'); var fs = require('fs');
var async = require('async');
var existsSync = fs.existsSync || path.existsSync; var existsSync = fs.existsSync || path.existsSync;
@ -372,47 +373,76 @@ function fromDBName(dbName, camelCase) {
* @param table * @param table
* @param cb * @param cb
*/ */
DataSource.prototype.discoverSchema = function(owner, table, cb) { DataSource.prototype.discoverSchema = function (owner, table, cb) {
var dataSourceName = this.name; var dataSourceName = this.name || this.adapter.name;
this.discoverModelProperties(owner, table, function (err, columns) {
if (err) { async.parallel(
cb && cb(err); [
return; this.discoverModelProperties.bind(this, owner, table),
} this.discoverPrimaryKeys.bind(this, owner, table),
if(!columns) { this.discoverForeignKeys.bind(this, owner, table)
cb && cb(); ], function (err, results) {
return;
} if (err) {
var schema = { cb && cb(err);
name: fromDBName(table, false), return;
options: {
},
properties: {
} }
};
schema.options[dataSourceName] = { var columns = results[0];
schema: owner, var primaryKeys = results[1];
table: table var pks = {}, fks = {};
}; primaryKeys.forEach(function(pk) {
columns.forEach(function (item) { pks[pk.columnName] = pk.keySeq;
var i = item; });
var propName = fromDBName(item.columnName, true);
schema.properties[propName] = console.log(pks);
{
type: item.type, var foreignKeys = results[2];
required: (item.nullable === 'N'), foreignKeys.forEach(function(fk) {
length: item.dataLength fks[fk.fkColumnName] = fk.keySeq;
});
if (!columns) {
cb && cb();
return;
}
var schema = {
name: fromDBName(table, false),
options: {
idInjection: false // DO NOT add id property
},
properties: {
}
}; };
schema.properties[propName][dataSourceName] = {
columnName: i.columnName, schema.options[dataSourceName] = {
dataType: i.dataType, schema: owner,
nullable: i.nullable table: table
}; };
columns.forEach(function (item) {
var i = item;
var propName = fromDBName(item.columnName, true);
schema.properties[propName] =
{
type: item.type,
required: (item.nullable === 'N'),
length: item.dataLength
};
if(pks[item.columnName]) {
schema.properties[propName].id = pks[item.columnName];
}
schema.properties[propName][dataSourceName] = {
columnName: i.columnName,
dataType: i.dataType,
nullable: i.nullable
};
});
cb && cb(null, schema);
}); });
cb && cb(null, schema);
});
} }
/** /**
@ -452,9 +482,72 @@ DataSource.prototype.freeze = function freeze() {
* @param {String} modelName * @param {String} modelName
*/ */
DataSource.prototype.tableName = function (modelName) { DataSource.prototype.tableName = function (modelName) {
return this.definitions[modelName].settings.table = this.definitions[modelName].settings.table || modelName var settings = this.definitions[modelName].settings;
if(settings[this.adapter.name]) {
return settings[this.adapter.name].table || modelName;
} else {
return modelName;
}
}; };
/**
* Return column name for specified modelName and propertyName
* @param modelName
* @param propertyName
* @returns {String} columnName
*/
DataSource.prototype.columnName = function (modelName, propertyName) {
var property = this.definitions[modelName].properties[propertyName];
if(property[this.adapter.name]) {
return property[this.adapter.name].columnName || propertyName;
} else {
return propertyName;
}
};
/**
* Return column names for specified modelName
* @param modelName
* @returns {[String]} column names
*/
DataSource.prototype.columnNames = function (modelName) {
var props = this.definitions[modelName].properties;
var cols = [];
for(var p in props) {
if(props[p][this.adapter.name]) {
cols.push(props[p][this.adapter.name].columnName || p);
} else {
cols.push(p);
}
}
return cols;
};
/**
* Find the ID column name
* @param modelName
* @returns {String} columnName for ID
*/
DataSource.prototype.getIDColumnName = function(modelName) {
var props = this.definitions[modelName].properties;
var self = this;
Object.keys(props).forEach(function(key) {
var columnInfo = props[key][self.adapter.name];
if(columnInfo && columnInfo.id) {
if(columnInfo.columnName) {
return columnInfo.columnName;
} else {
return key;
}
}
});
if(props.id) {
return 'id';
}
return null;
}
/** /**
* Define foreign key * Define foreign key
* @param {String} className * @param {String} className

View File

@ -25,6 +25,10 @@ BaseSQL.prototype.table = function (model) {
return this._models[model].model.schema.tableName(model); return this._models[model].model.schema.tableName(model);
}; };
BaseSQL.prototype.column = function (model, property) {
return this._models[model].model.schema.columnName(model, property);
};
BaseSQL.prototype.escapeName = function (name) { BaseSQL.prototype.escapeName = function (name) {
throw new Error('escapeName method should be declared in adapter'); throw new Error('escapeName method should be declared in adapter');
}; };
@ -33,6 +37,10 @@ BaseSQL.prototype.tableEscaped = function (model) {
return this.escapeName(this.table(model)); return this.escapeName(this.table(model));
}; };
BaseSQL.prototype.columnEscaped = function (model, property) {
return this.escapeName(this.column(model, property));
};
BaseSQL.prototype.define = function (descr) { BaseSQL.prototype.define = function (descr) {
if (!descr.settings) descr.settings = {}; if (!descr.settings) descr.settings = {};
this._models[descr.model.modelName] = descr; this._models[descr.model.modelName] = descr;

View File

@ -72,6 +72,7 @@
"mocha": "~1.8.2" "mocha": "~1.8.2"
}, },
"dependencies": { "dependencies": {
"async": "latest",
"inflection": "~1.2.5" "inflection": "~1.2.5"
} }
} }