convert dbmodel to json

This commit is contained in:
Jan Prochazka 2024-06-21 16:15:56 +02:00
parent a8999855bf
commit 297b321bc8
5 changed files with 58 additions and 1 deletions

View File

@ -35,6 +35,7 @@
"build:web:docker": "yarn workspace dbgate-web build",
"build:plugins:frontend": "workspaces-run --only=\"dbgate-plugin-*\" -- yarn build:frontend",
"build:plugins:frontend:watch": "workspaces-run --parallel --only=\"dbgate-plugin-*\" -- yarn build:frontend:watch",
"storage-json": "dbmodel model-to-json storage-db packages/api/src/storageModel.json",
"plugins:copydist": "workspaces-run --only=\"dbgate-plugin-*\" -- yarn copydist",
"build:app:local": "yarn plugins:copydist && cd app && yarn build:local",
"start:app:local": "cd app && yarn start:local",

View File

@ -0,0 +1,16 @@
const importDbModel = require('../utility/importDbModel');
const fs = require('fs');
async function dbModelToJson({ modelFolder, outputFile, commonjs }) {
const dbInfo = await importDbModel(modelFolder);
const json = JSON.stringify(dbInfo, null, 2);
if (commonjs) {
fs.writeFileSync(outputFile, `module.exports = ${json};`);
return;
} else {
fs.writeFileSync(outputFile, json);
}
}
module.exports = dbModelToJson;

View File

@ -27,6 +27,8 @@ const loadDatabase = require('./loadDatabase');
const generateModelSql = require('./generateModelSql');
const modifyJsonLinesReader = require('./modifyJsonLinesReader');
const dataDuplicator = require('./dataDuplicator');
const dbModelToJson = require('./dbModelToJson');
const jsonToDbModel = require('./jsonToDbModel');
const dbgateApi = {
queryReader,
@ -57,6 +59,8 @@ const dbgateApi = {
generateModelSql,
modifyJsonLinesReader,
dataDuplicator,
dbModelToJson,
jsonToDbModel,
};
requirePlugin.initializeDbgateApi(dbgateApi);

View File

@ -0,0 +1,9 @@
const exportDbModel = require('../utility/exportDbModel');
const fs = require('fs');
async function jsonToDbModel({ modelFile, outputDir }) {
const dbInfo = JSON.parse(fs.readFileSync(modelFile, 'utf-8'));
await exportDbModel(dbInfo, outputDir);
}
module.exports = jsonToDbModel;

View File

@ -38,7 +38,8 @@ program
'--load-data-condition <condition>',
'regex, which table data will be loaded and stored in model (in load command)'
)
.requiredOption('-e, --engine <engine>', 'engine name, eg. mysql@dbgate-plugin-mysql');
.option('-e, --engine <engine>', 'engine name, eg. mysql@dbgate-plugin-mysql')
.option('--commonjs', 'Creates CommonJS module');
program
.command('deploy <modelFolder>')
@ -117,4 +118,30 @@ program
);
});
program
.command('json-to-model <jsonFile> <modelFolder>')
.description('Converts JSON file to model')
.action((jsonFile, modelFolder) => {
runAndExit(
dbgateApi.jsonToDbModel({
modelFile: jsonFile,
outputDir: modelFolder,
})
);
});
program
.command('model-to-json <modelFolder> <jsonFile>')
.description('Converts model to JSON file')
.action((modelFolder, jsonFile) => {
const { commonjs } = program.opts();
runAndExit(
dbgateApi.dbModelToJson({
modelFolder,
outputFile: jsonFile,
commonjs,
})
);
});
program.parse(process.argv);