我使用sequelize-auto
从我的数据库生成数据类型,效果很好。结果是这样的:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('ReportTasks', {
ReportTaskID: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
BatchID: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'ReportBatches',
key: 'BatchID'
}
},
Name: {
type: DataTypes.STRING,
allowNull: true
},
Year: {
type: DataTypes.INTEGER,
allowNull: true
},
Month: {
type: DataTypes.INTEGER,
allowNull: true
},
CompanyKey: {
type: DataTypes.INTEGER,
allowNull: true
},
CompanyName: {
type: DataTypes.STRING,
allowNull: true
},
StartDate: {
type: DataTypes.DATE,
allowNull: true
},
EndDate: {
type: DataTypes.DATE,
allowNull: true
},
Status: {
type: DataTypes.STRING,
allowNull: true
}
}, {
tableName: 'ReportTasks'
});
};
现在我正在尝试设置Breeze服务器,它需要元数据。这些示例从json
文件中提取元数据,但我想从我的模型中构建元数据。我怎样才能做到这一点?
这是示例中的代码(http://breeze.github.io/doc-node-sequelize/introduction.html):
function createSequelizeManager() {
var metadata = readMetadata();
var sm = new SequelizeManager(dbConfig, sequelizeOptions);
sm.importMetadata(metadata);
return sm;
}
function readMetadata() {
var filename = "TodoMetadata.json";
if (!fs.existsSync(filename)) {
throw new Error("Unable to locate file: " + filename);
}
var metadata = fs.readFileSync(filename, 'utf8');
return JSON.parse(metadata);
}
答案 0 :(得分:1)
现在似乎在这里处理:Breeze-Sequelize
答案 1 :(得分:0)
所以...我砍掉了一个工具的超级基本开头来做这个反向映射。它不支持除int,string,bool,date之外的任何数据类型,但这是我现在所需要的。
它完成了由sequelize-auto
生成模型并为微风创建半可用元数据规范的工作。
这是一般性的想法(不要判断我,我知道它的坏处):
var Sequelize = require('sequelize');
var SequelizeAuto = require('sequelize-auto')
var fs = require('fs');
var jsonfile = require('jsonfile');
var config = require('./config');
var auto = new SequelizeAuto(config.database.name, config.database.username, config.database.password, {
dialect: config.database.dialect,
dialectModulePath: config.database.dialectModulePath,
dialectOptions: {
driver: config.database.driver,
instanceName: config.database.instanceName
},
directory: './models',
host: config.database.host,
username: config.database.username,
password: config.database.password,
database: config.database.name,
tables: config.database.tables,
define: {
timestamps: false
}
});
var tables;
var foreignKeys;
var metadata = {
"metadataVersion": "1.0.5",
"namingConvention": "noChange",
"localQueryComparisonOptions": "caseInsensitiveSQL",
"dataServices": [
{
"serviceName": "breeze/",
"hasServerMetadata": true,
"jsonResultsAdapter": "webApi_default",
"useJsonp": false
}
],
"structuralTypes": [],
};
auto.run(function (err) {
if (err) throw err;
createMetadata(auto.tables);
});
function createMetadata(tables) {
// now create metadata
Object.keys(auto.tables).forEach(function (tableName) {
var table = auto.tables[tableName];
var structuralType = transformTable(tableName);
var fieldIndex = 0;
Object.keys(table).forEach(function (field) {
var val = table[field];
var dataProperty = transformField(fieldIndex, field, val);
structuralType.dataProperties.push(dataProperty);
fieldIndex++;
});
// add to metadata
metadata.structuralTypes.push(structuralType);
});
console.log('metadata', metadata);
// write to file
jsonfile.writeFile('./metadata/metadata.json', metadata, { spaces: 2 }, function (err) {
console.log(err);
});
}
function transformTable(tableName) {
return {
"shortName": tableName,
"namespace": "Models",
"autoGeneratedKeyType": "Identity",
"defaultResourceName": tableName,
"dataProperties": []
};
}
function transformField(fieldIndex, field, val) {
var result = {
"name": field,
"dataType": _dataTypeMap[val.type] || 'String',
"isPartOfKey": fieldIndex == 0 ? true : false,
"isNullable": !!val.allowNull,
"defaultValue": val.defaultValue,
"validators": [],
};
if (!val.allowNull) {
result.validators.push({ "name": "required" });
}
return result;
}
// todo - add support for all types
var _dataTypeMap = {
NVARCHAR: 'String',
BIT: 'Boolean',
DATETIME: 'DateTime',
INT: 'Int32'
};