首页 > 解决方案 > 将 .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"
...
 }
}

标签: javascriptsqljsonsequelize.js

解决方案


如果您想将 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' }
  }
}


推荐阅读