💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ### egg.js 数据库迁移 文档:[https://eggjs.org/zh-cn/tutorials/sequelize.html](https://eggjs.org/zh-cn/tutorials/sequelize.html) ### sequelize 数据库迁移命令 | 命令 | 含义 | | --- | --- | | sequelize db:migrate | 运行迁移文件 | | sequelize db:migrate:status | 列出所有迁移的状态 | | sequelize db:migrate:undo | 隔离数据库:迁移:撤消 | | sequelize db:migrate:undo:all | 还原所有运行的迁移 | | sequelize db:create | 创建由配置指定的数据库 | | sequelize db:drop | 删除由配置指定的数据库 | ***** ### 配置 1. 安装并配置[egg-sequelize](https://github.com/eggjs/egg-sequelize)插件(它会辅助我们将定义好的 Model 对象加载到 app 和 ctx 上)和[mysql2](https://github.com/sidorares/node-mysql2)模块: ~~~ npm install --save egg-sequelize mysql2 ~~~ 2. 在`config/plugin.js`中引入 egg-sequelize 插件 ~~~ exports.sequelize = { enable: true, package: 'egg-sequelize', }; ~~~ 3. 在`config/config.default.js`中编写 sequelize 配置 ![](https://box.kancloud.cn/ebe834d0833947bcc5f327828e8598d5_259x236.png) ``` config.sequelize = { dialect: 'mysql', host: '127.0.0.1', password: 'xxxx', port: 3306, database: 'friends', timezone: '+8:00', define: { freezeTableName: true } }; ``` 4. sequelize 提供了[sequelize-cli](https://github.com/sequelize/cli)工具来实现[Migrations](http://docs.sequelizejs.com/manual/tutorial/migrations.html),我们也可以在 egg 项目中引入 sequelize-cli。 ~~~ npm install --save-dev sequelize-cli ~~~ 5. 在 egg 项目中,我们希望将所有数据库 Migrations 相关的内容都放在`database`目录下,所以我们在项目根目录下新建一个`.sequelizerc`配置文件: ~~~ 'use strict'; const path = require('path'); module.exports = { config: path.join(__dirname, 'database/config.json'), 'migrations-path': path.join(__dirname, 'database/migrations'), 'seeders-path': path.join(__dirname, 'database/seeders'), 'models-path': path.join(__dirname, 'app/model'), }; ~~~ 6. 初始化 Migrations 配置文件和目录 ~~~ npx sequelize init:config npx sequelize init:migrations npx sequelize init:models ~~~ 7. 执行完后会生成`database/config.json`文件和`database/migrations`目录,我们修改一下`database/config.json`中的内容,将其改成我们项目中使用的数据库配置: ~~~ { "development": { "username": "root", "password": null, "database": "egg-sequelize-doc-default", "host": "127.0.0.1", "dialect": "mysql", "operatorsAliases": false } } ~~~ 8. 创建数据库 `npx sequelize db:create` ***** ### 创建数据迁移表 ~~~ npx sequelize migration:generate --name=init-users ~~~ 1.执行完命令后,会在database / migrations / 目录下生成数据表迁移文件,然后定义 ~~~ 'use strict'; module.exports = { up: async (queryInterface, Sequelize) => { const { INTEGER, STRING, DATE, ENUM } = Sequelize; // 创建表 await queryInterface.createTable('users', { id: { type: INTEGER(20).UNSIGNED, primaryKey: true, autoIncrement: true }, username: { type: STRING(30), allowNull: false, defaultValue: '', comment: '用户名称', unique: true}, email: { type: STRING(160), allowNull: false, defaultValue: '', comment: '用户邮箱', unique: true }, password: { type: STRING(200), allowNull: false, defaultValue: '' }, avatarUrl: { type: STRING(200), allowNull: true, defaultValue: '' }, mobile: { type: STRING(20), allowNull: false, defaultValue: '', comment: '用户手机', unique: true }, prifix: { type: STRING(32), allowNull: false, defaultValue: '' }, abstract: { type: STRING(255), allowNull: true, defaultValue: '' }, gender: { type: ENUM, values: ['男','女','保密'], allowNull: true, defaultValue: '男', comment: '用户性别'}, createdAt: DATE, updatedAt: DATE }, { engine: 'MYISAM' }); // 添加索引 queryInterface.addIndex('users', ['gender']) }, down: async queryInterface => { await queryInterface.dropTable('users') } }; ~~~ ***** ### 创建数据模型 app / model / user.js ![](https://box.kancloud.cn/2767c9761ef72ec7108cb68b5512c56e_936x297.png) >[danger] 引入日期处理类库 Moment.js > npm install moment --save ~~~ 'use strict'; const moment = require('moment') module.exports = app => { const { STRING, INTEGER, DATE } = app.Sequelize; const User = app.model.define('users', { id: { type: INTEGER(20).UNSIGNED, primaryKey: true, autoIncrement: true }, username: { type: STRING(30), allowNull: false, defaultValue: '', comment: '用户名称', unique: true}, email: { type: STRING(160), allowNull: false, defaultValue: '', comment: '用户邮箱', unique: true }, password: { type: STRING(200), allowNull: false, defaultValue: '' }, avatarUrl: { type: STRING(200), allowNull: true, defaultValue: '' }, mobile: { type: STRING(20), allowNull: false, defaultValue: '', comment: '用户手机', unique: true }, prifix: { type: STRING(32), allowNull: false, defaultValue: '' }, abstract: { type: STRING(255), allowNull: true, defaultValue: '' }, gender: { type: ENUM, values: ['男','女','保密'], allowNull: true, defaultValue: '男', comment: '用户性别'}, createdAt: {type: DATE, get() {return moment(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm:ss')}}, updatedAt: {type: DATE, get() {return moment(this.getDataValue('updatedAt')).format('YYYY-MM-DD HH:mm:ss')}} }); return User; }; ~~~