forked from WeixinCloud/wxcloudrun-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
36 lines (30 loc) · 798 Bytes
/
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
const { Sequelize, DataTypes } = require('sequelize');
// 从环境变量中读取数据库配置
const {
MYSQL_USERNAME,
MYSQL_PASSWORD,
MYSQL_ADDRESS = ''
} = process.env
const [host, port] = MYSQL_ADDRESS.split(':')
const sequelize = new Sequelize('nodejs_demo', MYSQL_USERNAME, MYSQL_PASSWORD, {
host,
port,
dialect: 'mysql' /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
});
// 定义数据模型
const Counter = sequelize.define('Counter', {
count: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 1
}
});
// 数据库初始化方法
async function init() {
await Counter.sync({ alter: true })
}
// 导出初始化方法和模型
module.exports = {
init,
Counter
}