安装 egg-sequelize

  1. yarn add egg-sequelize mysql2

初始化项目后,配置 egg-sequelize
sequelize技巧 https://zhuanlan.zhihu.com/p/91195711

config.sequelize配置

egg-sequelize配置文档 https://github.com/eggjs/examples/tree/master/sequelize

config/plugin.js

  1. exports.sequelize = {
  2. enable: true,
  3. package: 'egg-sequelize',
  4. }

config/config.default.js

  1. config.sequelize = {
  2. dialect: 'mysql', // 数据库类型
  3. host: 'localhost',
  4. port: 3306, // port一定要写成字符串,可能报错
  5. database: 'qixiu',
  6. username: 'root', // user
  7. password: 'root',
  8. // 配置数据库时间为北京时间,V6版本配置
  9. timezone: '+08:00',
  10. dialectOptions: {
  11. useUTC: false // for reading from database
  12. },
  13. define: {
  14. timestamps: false, // 自动添加时间戳
  15. freezeTableName: true, // 使用原始表名称,不需要额外的处理
  16. },
  17. };
  • dialect:数据库类型
  • deifne
    • timestamps: false 不要自动添加时间戳
    • freezeTableName: true:使用原始的表名

timezone时间配置

返回时间配置,egg-sequelize在读取时间时,会返回UTC格式,配置如下
https://github.com/sequelize/sequelize/issues/854
https://stackoverflow.com/questions/47367893/sequelize-reads-datetime-in-utc-only
image.png

v5版本配置

  1. exports.sequelize = {
  2. dialect: 'mysql',
  3. timezone: '+08:00' ,
  4. dialectOptions: {
  5. useUTC: false, //for reading from database
  6. dateStrings: true,
  7. typeCast(field, next) {
  8. // for reading from database
  9. return (field.type === "DATETIME") ? field.string() : next();
  10. }
  11. }
  12. }

Model 模型

src/app/model
sequelize中,将每个表都当做一个模型
ctx.model 调用模型

user.js

  1. 'use strict';
  2. module.exports = app => {
  3. const { STRING, INTEGER } = app.Sequelize;
  4. // 创建表
  5. const User = app.model.define('user', {
  6. id: { type: INTEGER, primaryKey: true, autoIncrement: true },
  7. name: STRING(20),
  8. password: STRING(1000),
  9. });
  10. return User;
  11. };
  • app就是 egg应用实例

Service

  1. 'use strict';
  2. const { Service } = require('egg');
  3. const dbName = 'blog';
  4. class UserService extends Service {
  5. async list() {
  6. const { ctx } = this;
  7. }
  8. }

Controller

  1. 'use strict';
  2. const { Controller } = require('egg');
  3. class UserController extends Controller {
  4. // 查询数据
  5. async findAll() {
  6. const { ctx } = this;
  7. const res = await ctx.model.User.findAll();
  8. const res = await ctx.model.User.findAll({
  9. where: {id : 23},
  10. limit: 1,
  11. offset: 10
  12. });
  13. return {
  14. code: 0,
  15. data: res,
  16. }
  17. }
  18. // 查找一条数据
  19. async findById() {
  20. const { ctx } = this;
  21. const res = await ctx.model.User.findByPk(ctx.query.id);
  22. return {
  23. code: 0,
  24. data: res,
  25. }
  26. }
  27. // 添加一条数据
  28. async create() {
  29. const { ctx } = this;
  30. const res = await ctx.model.User.create(ctx.request.body);
  31. return {
  32. code: 0,
  33. data: res,
  34. }
  35. }
  36. // 编辑数据前,先判断数据是否存在
  37. async update() {
  38. const { ctx } = this;
  39. const user = await ctx.model.User.findByPk(ctx.request.body.id);
  40. if(!user) {
  41. ctx.body = {
  42. code: 404,
  43. errMsg: '用户不存在',
  44. }
  45. return;
  46. }
  47. const res = await user.update(ctx.request.body);
  48. return {
  49. code: 0,
  50. data: res,
  51. }
  52. }
  53. // 删除数据前,也要判断数据是否存在
  54. async delete() {
  55. const { ctx } = this;
  56. const {id} = ctx.request.body;
  57. const user = await ctx.model.User.findByPk(id);
  58. if(!user) {
  59. ctx.body = {
  60. code: 404,
  61. errMsg: '用户不存在'
  62. }
  63. return;
  64. }
  65. // 删除数据
  66. const res = user.destroy(id)
  67. return {
  68. code: 204,
  69. }
  70. }
  71. }