-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
54 lines (42 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const fs = require('fs');
const path = require('path');
const bluebird = require('bluebird');
const {program} = require('commander');
const config = require('config');
const pLimit = require('p-limit');
bluebird.longStackTraces();
global.Promise = bluebird;
program
.name(' ')
.usage(
`
Force sync database schema (drop tables then create).
node . sync`);
program
.command('sync')
.description('sync database schema');
program.parse(process.argv);
async function sync() {
const LOCAL_DATABASE_PATH = path.join(__dirname, config.DATABASE_PATH);
const {connectDatabase} = require('./src/backend/common/database');
const limit = pLimit(1);
fs.rmSync(LOCAL_DATABASE_PATH, {force: true});
connectDatabase({isLogSQL: true});
const models = require('./src/backend/models/data');
await Promise.all(
Object.values(models.sequelize.models).map(model => limit(() => model.sync({force: true}))),
);
}
async function execute() {
const {args} = program;
if (args[0] === 'sync') {
return sync();
}
return program.help();
}
execute()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});