创建迁移文件
npx sequelize migration:generate
/database/migrations/***-fava.js
'use strict'; module.exports = { up: async (queryInterface, Sequelize) => { const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize; // 创建表 await queryInterface.createTable('fava', { id: { type: INTEGER(20).UNSIGNED, primaryKey: true, autoIncrement: true }, data: { type: TEXT, allowNull: false, defaultValue: '', comment: '内容', }, type: { type: ENUM, values: ['emoticon', 'text', 'image', 'video', 'audio', 'card'], allowNull: false, defaultValue: 'text', comment: '类型' }, options: { type: TEXT, allowNull: false, defaultValue: '', comment: '其他参数', }, user_id: { type: INTEGER(20).UNSIGNED, allowNull: false, comment: '用户id', // 定义外键(重要) references: { model: 'user', // 对应表名称(数据表名称) key: 'id' // 对应表的主键 }, onUpdate: 'restrict', // 更新时操作 onDelete: 'cascade' // 删除时操作 }, created_at: DATE, updated_at: DATE }); }, down: async queryInterface => { await queryInterface.dropTable('fava'); } };
创建数据库模型 app/model/fava.js
'use strict'; const crypto = require('crypto'); module.exports = app => { const { INTEGER, STRING, DATE, ENUM, TEXT } = app.Sequelize; // 配置(重要:一定要配置详细,一定要!!!) const Fava = app.model.define('fava', { id: { type: INTEGER(20).UNSIGNED, primaryKey: true, autoIncrement: true }, data: { type: TEXT, allowNull: false, defaultValue: '', comment: '内容', }, type: { type: ENUM, values: ['emoticon', 'text', 'image', 'video', 'audio', 'card'], allowNull: false, defaultValue: 'text', comment: '类型' }, options: { type: TEXT, allowNull: false, defaultValue: '', comment: '其他参数', }, user_id: { type: INTEGER(20).UNSIGNED, allowNull: false, comment: '用户id', // 定义外键(重要) references: { model: 'user', // 对应表名称(数据表名称) key: 'id' // 对应表的主键 }, onUpdate: 'restrict', // 更新时操作 onDelete: 'cascade' // 删除时操作 }, created_at: DATE, updated_at: DATE }); return Fava; };
创建app/controller/fava.js
'use strict'; const Controller = require('egg').Controller; class FavaController extends Controller { // 创建收藏 async create() { const { ctx, app } = this; let current_user_id = ctx.authUser.id; // 参数验证 ctx.validate({ type: { type: 'string', required: true, range: { in: ['text', 'image', 'video', 'audio', 'emoticon', 'card'] }, desc: '消息类型' }, data: { type: 'string', required: true, desc: '消息内容' }, options: { type: 'string', required: true } }); let { type, data, options } = ctx.request.body; await app.model.Fava.create({ type, data, options, user_id: current_user_id }); return ctx.apiSuccess('ok'); } // 收藏列表 async list() { const { ctx, app } = this; let current_user_id = ctx.authUser.id; let page = ctx.params.page ? parseInt(ctx.params.page) : 1; let limit = ctx.query.limit ? parseInt(ctx.query.limit) : 10; let offset = (page - 1) * limit; let rows = await app.model.Fava.findAll({ where: { user_id: current_user_id }, offset, limit, order: [ ['id', 'DESC'] ] }); return ctx.apiSuccess(rows); } // 删除收藏 async destroy() { const { ctx, app } = this; let current_user_id = ctx.authUser.id; ctx.validate({ id: { type: "int", required: true } }); let { id } = ctx.request.body; await app.model.Fava.destroy({ where: { id, user_id: current_user_id } }); return ctx.apiSuccess('ok'); } } module.exports = FavaController;
router.js
// 创建收藏 router.post('/fava/create',controller.fava.create); // 收藏列表 router.get('/fava/:page',controller.fava.list); // 删除收藏 router.post('/fava/destroy',controller.fava.destroy);
感谢大家观看,我们下次见