简体   繁体   English

将 .sql 文件转换为 .json 或 javascript 对象或续集模型文件的最佳方法

[英]Best way to convert a .sql file to a .json or to a javascript Object or to a sequelize model file

I want ro create a accessible JSON or JS-Object out of a .sql file.我想从.sql文件中创建一个可访问的JSONJS-Object What is the best way to do this, or are there any solutions for that available.这样做的最佳方法是什么,或者是否有任何可用的解决方案。

Or is there any good solution to create a sequelize model file out of a .sql或者有什么好的解决方案可以从.sql创建sequelize模型文件

my .sql file:我的 .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`);

Example JSON that i want to create (It is just an example, i would accept any file with i can work with):我想创建的示例JSON (这只是一个示例,我会接受我可以使用的任何文件):

{
"AuthenticationSettings" : {
"type" : "create",
"fields" : {
 "AUTSET_Id" : "Integer"
...
 }
}

If you want to convert SQL Schema into a JSON Object then you can use sql-ddl-to-json-schema It's not very feature full but can work for your use-case.如果您想将 SQL 模式转换为 JSON 对象,那么您可以使用sql-ddl-to-json-schema它的功能不是很完整,但可以适用于您的用例。 For example例如

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])

that will convert and show the output as这将转换并将输出显示为

{
  '$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' }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM