-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
32 lines (26 loc) · 732 Bytes
/
db.js
File metadata and controls
32 lines (26 loc) · 732 Bytes
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
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,
};