javascript - 将 .sql 文件转换为 .json 或 javascript 对象或 sequelize 模型文件的最佳方法
问题描述
我想创建一个可访问的文件JSON
或文件。最好的方法是什么,或者有什么可用的解决方案。JS-Object
.sql
或者有什么好的解决方案可以sequelize
从.sql
我的 .sql 文件:
CREATE TABLE `AuthenticationSettings` (
`AUTSET_Id` int PRIMARY KEY AUTO_INCREMENT,
`updated_at` timestamp );
CREATE TABLE `ClinicAuthentiation` (
`CLINAUT_Id` int PRIMARY KEY AUTO_INCREMENT,
`updated_at` timestamp );
ALTER TABLE `AuthenticationSettings` ADD FOREIGN KEY (`AUTSET_Id`) REFERENCES `ClinicAuthentiation` (`AUTSET_Id`);
JSON
我要创建的示例(这只是一个示例,我可以接受任何我可以使用的文件):
{
"AuthenticationSettings" : {
"type" : "create",
"fields" : {
"AUTSET_Id" : "Integer"
...
}
}
解决方案
如果您想将 SQL Schema 转换为 JSON 对象,那么您可以使用sql-ddl-to-json-schema它的功能不是很完整,但可以适用于您的用例。例如
const { Parser } = require('sql-ddl-to-json-schema');
const parser = new Parser('mysql');
const sql = `
CREATE TABLE AuthenticationSettings (
AUTSET_Id int PRIMARY KEY AUTO_INCREMENT,
updated_at timestamp
);
`;
const options = {};
const jsonSchemaDocuments = parser.feed(sql)
.toJsonSchemaArray(options);
console.log(jsonSchemaDocuments[0])
这将转换并将输出显示为
{
'$schema': 'http://json-schema.org/draft-07/schema',
'$comment': 'JSON Schema for AuthenticationSettings table',
'$id': 'AuthenticationSettings',
title: 'AuthenticationSettings',
type: 'object',
required: [ 'AUTSET_Id' ],
definitions: {
AUTSET_Id: {
'$comment': 'primary key',
type: 'integer',
minimum: 1,
maximum: 2147483647
},
updated_at: { type: 'string' }
},
properties: {
AUTSET_Id: { '$ref': '#/definitions/AUTSET_Id' },
updated_at: { '$ref': '#/definitions/updated_at' }
}
}
推荐阅读
- c - 在 Windows 上将 C/Fortran 代码与 pgi 一起编译时出现问题:“main 已定义”
- reactjs - 将 ReactJs 应用程序连接到 SpringBoot MVC/PostgresQL 后端
- react-native - 在 Expo 应用程序中使用 Auth0 获取 id_token
- angular - CSS Grid 可以用网格容器的非直接子元素填充网格区域吗?还是网格容器外的元素?
- c# - System.ComponentModel.Win32Exception:“系统找不到指定的文件”
- sql-server - 从日期/时间列中删除日期
- batch-file - 复制具有相同名称但不同扩展名的文件列表
- android - React Native : Could not find com.android.tools.build:gradle:2.2.3
- c# - C#我如何要求管理员,而不是“强迫”他
- dotnetnuke - Dot Net Nuke 9 创建站点模板