How to introduce sequlize in the eggjs
The first step to install egg-sequelize plug-in project
在项目目录 cmd 中输入 npm i egg-sequelize --save
The second part is installed mysql2 plug-in project
在项目目录 cmd 中输入 npm i mysql2 --save
Open egg-sequelize plugin app / config / plugin.js, the following code is inserted
exports.sequelize = {
enable: true,
package: 'egg-sequelize',
}
Fill in the database link in app / config / confing.default.js configuration information
config.sequelize = {
dialect: 'mysql', //数据库类型
database: 'abc' , //数据库名称
host: '127.0.0.1', //数据库ip地址
port: '3306', //数据库端口
username: 'root', //数据库用户名
password: 'root' //数据库密码
}
Defined model
1. Create a new folder in the app model file folder placed model
2. Create a file named after the table name in the model js file, such as user tables on the establishment of a user.js
`` `User.js code
'use strict';
App = = module.exports> {
const {STRING, app.Sequelize = INTEGER}; // Get sequelize corresponding data type mappings
// 1.3 https://itbilu.com/nodejs/npm/V1PExztfb.html in more detail data types
the User app.model.define = const ( 'User', {
UID: {
type: INTEGER,
primaryKey: to true, // primary key
},
the uname: STRING,
upwd: STRING,
ADD_TIME: INTEGER,
}, {
// Default freezeTableName is false If false, then automatically added to the table name in the plural s
freezeTableName: true,
// timestamps default value is true, a true solid create_time and automatically adds the two fields update_time
timestamps: false,
});
return User;
};
```