2023-01-28 17:48:52 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
require('dotenv').config();
|
|
|
|
|
|
|
|
global.API_PACKAGE = path.dirname(path.dirname(require.resolve('dbgate-api')));
|
|
|
|
global.PLUGINS_DIR = process.env.DEVMODE
|
|
|
|
? path.join(path.dirname(path.dirname(global.API_PACKAGE)), 'plugins')
|
|
|
|
: path.dirname(global.API_PACKAGE);
|
|
|
|
global.IS_NPM_DIST = true;
|
2024-10-01 08:38:35 +00:00
|
|
|
global.IS_DB_MODEL = true;
|
2023-01-28 17:48:52 +00:00
|
|
|
|
|
|
|
const program = require('commander');
|
|
|
|
const dbgateApi = require('dbgate-api');
|
2023-01-28 19:14:44 +00:00
|
|
|
const { createLogger } = require('pinomin');
|
2024-10-01 10:15:22 +00:00
|
|
|
const { extractErrorLogData } = require('dbgate-tools');
|
2023-01-28 19:14:44 +00:00
|
|
|
|
|
|
|
const logger = createLogger('dbmodel');
|
|
|
|
|
|
|
|
async function runAndExit(promise) {
|
|
|
|
try {
|
|
|
|
await promise;
|
|
|
|
logger.info('Success');
|
|
|
|
process.exit();
|
|
|
|
} catch (err) {
|
2024-10-01 10:15:22 +00:00
|
|
|
logger.error(extractErrorLogData(err), 'Processing failed');
|
2023-01-28 19:14:44 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2023-01-28 17:48:52 +00:00
|
|
|
|
2024-06-21 13:22:32 +00:00
|
|
|
program.version(dbgateApi.currentVersion.version);
|
|
|
|
|
2023-01-28 17:48:52 +00:00
|
|
|
program
|
|
|
|
.option('-s, --server <server>', 'server host')
|
|
|
|
.option('-u, --user <user>', 'user name')
|
|
|
|
.option('-p, --password <password>', 'password')
|
|
|
|
.option('-d, --database <database>', 'database name')
|
|
|
|
.option('--auto-index-foreign-keys', 'automatically adds indexes to all foreign keys')
|
|
|
|
.option(
|
|
|
|
'--load-data-condition <condition>',
|
|
|
|
'regex, which table data will be loaded and stored in model (in load command)'
|
|
|
|
)
|
2024-06-21 14:15:56 +00:00
|
|
|
.option('-e, --engine <engine>', 'engine name, eg. mysql@dbgate-plugin-mysql')
|
|
|
|
.option('--commonjs', 'Creates CommonJS module');
|
2023-01-28 17:48:52 +00:00
|
|
|
|
|
|
|
program
|
2023-01-28 19:14:44 +00:00
|
|
|
.command('deploy <modelFolder>')
|
2023-01-28 17:48:52 +00:00
|
|
|
.description('Deploys model to database')
|
2023-01-28 19:14:44 +00:00
|
|
|
.action(modelFolder => {
|
|
|
|
const { engine, server, user, password, database } = program.opts();
|
|
|
|
// const hooks = [];
|
|
|
|
// if (program.autoIndexForeignKeys) hooks.push(dbmodel.hooks.autoIndexForeignKeys);
|
|
|
|
|
|
|
|
runAndExit(
|
|
|
|
dbgateApi.deployDb({
|
2023-01-28 17:48:52 +00:00
|
|
|
connection: {
|
|
|
|
engine,
|
|
|
|
server,
|
|
|
|
user,
|
|
|
|
password,
|
|
|
|
database,
|
|
|
|
},
|
2023-01-28 19:14:44 +00:00
|
|
|
modelFolder,
|
2023-01-28 17:48:52 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-01-28 19:14:44 +00:00
|
|
|
// runAndExit(
|
|
|
|
// dbmodel.deploy({
|
|
|
|
// connection: {
|
|
|
|
// engine,
|
|
|
|
// server,
|
|
|
|
// user,
|
|
|
|
// password,
|
|
|
|
// database,
|
|
|
|
// },
|
|
|
|
// hooks,
|
|
|
|
// projectDir,
|
|
|
|
// })
|
|
|
|
// );
|
|
|
|
|
2023-01-28 17:48:52 +00:00
|
|
|
program
|
|
|
|
.command('load <outputDir>')
|
|
|
|
.description('Loads model from database')
|
|
|
|
.action(outputDir => {
|
|
|
|
const { engine, server, user, password, database } = program.opts();
|
|
|
|
// const loadDataCondition = program.loadDataCondition
|
|
|
|
// ? table => table.name.match(new RegExp(program.loadDataCondition, 'i'))
|
|
|
|
// : null;
|
|
|
|
// const hooks = [];
|
|
|
|
|
2023-01-28 19:14:44 +00:00
|
|
|
runAndExit(
|
2023-01-28 17:48:52 +00:00
|
|
|
dbgateApi.loadDatabase({
|
|
|
|
connection: {
|
|
|
|
engine,
|
|
|
|
server,
|
|
|
|
user,
|
|
|
|
password,
|
|
|
|
database,
|
|
|
|
},
|
|
|
|
outputDir,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
program
|
2023-01-28 19:14:44 +00:00
|
|
|
.command('build <modelFolder> <outputFile>')
|
2023-01-28 17:48:52 +00:00
|
|
|
.description('Builds single SQL script from project')
|
2023-01-28 19:14:44 +00:00
|
|
|
.action((modelFolder, outputFile) => {
|
|
|
|
const { engine } = program.opts();
|
|
|
|
// const hooks = [];
|
|
|
|
runAndExit(
|
|
|
|
dbgateApi.generateModelSql({
|
|
|
|
// client,
|
|
|
|
// hooks,
|
|
|
|
modelFolder,
|
2023-01-28 17:48:52 +00:00
|
|
|
outputFile,
|
2023-01-28 19:14:44 +00:00
|
|
|
engine,
|
2023-01-28 17:48:52 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-06-21 14:15:56 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-01-28 17:48:52 +00:00
|
|
|
program.parse(process.argv);
|