-
Notifications
You must be signed in to change notification settings - Fork 68
/
db.js
54 lines (47 loc) · 1.24 KB
/
db.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
"use strict";
const md5 = require("md5");
const Sequelize = require("sequelize");
const config = require("./config.json");
// get config
const { type, username, password, database, host, port, pool } = config.db;
// get timezone
const { timezone } = config.app;
// sequelize init
const sequelize = new Sequelize(database, username, password, {
host: host,
dialect: type,
port: port,
pool: pool,
define: {
freezeTableName: true,
timestamps: false
},
timezone,
logging: false
});
const Post = require("./app/models/post.js")(sequelize, Sequelize);
const User = require("./app/models/user.js")(sequelize, Sequelize);
sequelize.sync().then(_ => {
// init data
User.findOne({}).then(user => {
if (!user)
{
return sequelize.transaction(transaction => {
return User.create({
username: "root",
password: md5("123456")
}, { transaction });
});
}
else
{
return "inited";
}
}).then(result => {
}).catch(error => {
});
});
module.exports.Post = Post;
module.exports.User = User;
module.exports.sequelize = sequelize;
module.exports.Sequelize = Sequelize;