首页 > 解决方案 > 如何在 Objection.js 中为不同类别的喜欢实现继承?

问题描述

我尝试使用 Objection.js 实现数据库设计,但我不知道如何定义具有多个互斥类别的继承关系。

有问题的案例如下:我想对帖子、评论和回复实现点赞。据我所知,“最可扩展的解决方案是只有一个“基本”表,并从中“继承”所有其他表。如另一个SO问题中所述。这个想法是将喜欢链接到一个实体,并且一个实体可以是帖子、评论或另一个实体。

User
id PK int
name string

Entity
id PK int

Like
userId int Fk >- User.id
entityId int Fk >- Entity.id

Article
id PK int Fk >- Entity.id
author int FK >- User.id
title string
text string

Comment
id PK int Fk >- Entity.id
articleId int FK >- Article.id
authorId int FK >- User.id
text string

--There can be more entities like reply, image, etc

在 Objection.js 中,您实现如下关系:

class Entity extends Model {
  static tableName = 'entities';

  static relationMappings = {
    Post: {
      relation: Model.HasOneRelation,
      modelClass: Like,
      join: {
          from: 'post.id',
          to: 'like.postId'
      }
    }
  }
}

但是,如果这样做我不知道如何定义它也可以是评论或回复。不仅是邮政。

有没有办法存档这个?如果没有,我应该使用另一个 JS 库吗?我应该更改我的架构以使其与 Objection.js 一起使用吗?

相关链接:

在数据库中实现评论和喜欢

http://vincit.github.io/objection.js/#relations

标签: javascriptpostgresqldatabase-designobjection.js

解决方案


推荐阅读