Node/Koa2[56]: 关注和取消关注业务
·
Yin灏
-
创建数据模型:用户关系
-
查看模板代码
-
路由和接口开发(粉丝列表,关注/取消关注,关注人列表)
-
单元测试
回顾数据模型设计
处理数据模型
src/db/model/UserRelation.js
const seq = require("../seq");
const { INTEGER } = require("../types");
const UserRelation = seq.define("userRelation", {
userId: {
type: INTEGER,
allowNull: false,
comment: "用户 id",
},
followerId: {
type: INTEGER,
allowNull: false,
comment: "被关注用户的 id",
},
});
module.exports = UserRelation;
index.js 中引入
src/db/model/index.js
//...
const User = require("./User");
const Blog = require("./Blog");
const UserRelation = require("./UserRelation");
//...
Blog.belongsTo(User, {
foreignKey: "UserId",
});
UserRelation.belongsTo(User, {
foreignKey: "followerId",
});
User.hasMany(UserRelation, {
foreignKey: "userId",
});
//...
module.exports = {
User,
Blog,
UserRelation,
};
同步数据库
node src/db/sync.js