mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-20 03:06:00 +00:00
export db model to directory
This commit is contained in:
31
packages/api/src/utility/exportDbModel.js
Normal file
31
packages/api/src/utility/exportDbModel.js
Normal file
@@ -0,0 +1,31 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const yaml = require('js-yaml');
|
||||
const { tableInfoToYaml } = require('dbgate-tools');
|
||||
|
||||
async function exportDbModel(dbModel, outputDir) {
|
||||
const { tables, views, procedures, functions, triggers, matviews } = dbModel;
|
||||
|
||||
if (!fs.existsSync(outputDir)) {
|
||||
await fs.mkdir(outputDir);
|
||||
}
|
||||
|
||||
for (const table of tables || []) {
|
||||
const content = yaml.dump(tableInfoToYaml(table));
|
||||
await fs.writeFile(path.join(outputDir, `${table.pureName}.table.yaml`), content);
|
||||
}
|
||||
|
||||
async function writeList(list, ext) {
|
||||
for (const obj of list || []) {
|
||||
await fs.writeFile(path.join(outputDir, `${obj.pureName}.${ext}.sql`), obj.createSql);
|
||||
}
|
||||
}
|
||||
|
||||
await writeList(views, 'view');
|
||||
await writeList(procedures, 'proc');
|
||||
await writeList(functions, 'func');
|
||||
await writeList(triggers, 'trigger');
|
||||
await writeList(matviews, 'matview');
|
||||
}
|
||||
|
||||
module.exports = exportDbModel;
|
||||
Reference in New Issue
Block a user