灏天阁

Node/Koa2[70]:@提到我的-数据模型

· Yin灏
  • 修改数据模型:创建 AtRelation

  • 收集分析 @ 的用户

  • 获取 at 的数量

  • 开发页面和路由

  • 单元测试

创建数据模型

// src/db/model/AtRelation.js
const seq = require("../seq");
const { INTEGER, BOOLEAN } = require("../types");

const AtRelation = seq.define("atRelation", {
  userId: {
    type: INTEGER,
    allowNull: false,
    comment: "用户 Id",
  },
  blogId: {
    type: INTEGER,
    allowNull: false,
    comment: "微博 Id",
  },
  isRead: {
    type: BOOLEAN,
    allowNull: false,
    defaultValue: false,
    comment: "是否已读",
  },
});

module.exports = AtRelation;

引用

// src/db/model/index.js
const User = require("./User");
const Blog = require("./Blog");
const UserRelation = require("./UserRelation");
const AtRelation = require("./AtRelation");
Blog.belongsTo(User, {
  foreignKey: "UserId",
});
UserRelation.belongsTo(User, {
  foreignKey: "followerId",
});
User.hasMany(UserRelation, {
  foreignKey: "userId",
});
Blog.belongsTo(UserRelation, {
  foreignKey: "userId",
  targetKey: "followerId",
});

Blog.hasMany(AtRelation, {
  foreignKey: "blogId",
});

module.exports = {
  User,
  Blog,
  UserRelation,
  AtRelation,
};

同步到数据库

node src/db/sync.js

- Book Lists -