-
Notifications
You must be signed in to change notification settings - Fork 4
/
gene.js
51 lines (46 loc) · 1.4 KB
/
gene.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
var _ = require('lodash');
const SeqAuto = require('sequelize-auto')
const path = require('path')
const fs = require('fs')
const config = require('./server/config');
const tables = process.argv.slice(2)
const dir = 'server'
if (tables.length === 0) {
throw new Error('No table specified\nUsage: node generate_model table1 table2 table3\n')
}
const auto = new SeqAuto(config.database, config.username, config.password, {
host: config.host,
port: config.port,
dialect: config.dialect,
directory: path.resolve(__dirname, `${dir}/model-base`),
additional: {
timestamps: false
},
tables: tables
})
const modelStrArr = []
for (let table of tables) {
let name = table.includes("t_") ? table.substring(table.indexOf("t_")+1) : table
console.log(name)
modelStrArr.push({
name: name,
tpl: `
const table='${table}'
const obj=require('../model-base/abase')(table)
exports.obj = obj`
})
}
auto.run(err => {
if (err) {
throw err
}
for (let modelObj of modelStrArr) {
console.log(modelObj)
let file = path.resolve(__dirname, `${dir}/model`) + '/' + _.camelCase(modelObj.name) + '.js'
if (!fs.existsSync(file)) {
fs.writeFileSync(file, modelObj.tpl)
}
}
console.log('\n\nProcess completed successfully.')
console.log('You may need to add these Foreign Keys mannually:\n', auto.foreignKeys)
})