所以,我对 typeORM 和 postgres DB 有点陌生,我发现有些东西让我有点困惑,但是,在告诉您我的问题之前,让我向您展示一些代码
所以,这是一个名为 Vote 的实体
import {
Entity as TOEntity,
Column,
JoinColumn,
Index,
BeforeInsert,
ManyToOne
} from "typeorm";
import Entity from "./Entity";
import User from "./User";
import Post from "./Post";
import Comment from "./Comment";
@TOEntity("votes")
export default class Vote extends Entity {
constructor(vote: Partial<Vote>) {
super();
Object.assign(this, vote);
}
@Column()
value: number;
@ManyToOne(() => User)
@JoinColumn({ name: "username", referencedColumnName: "username" })
user: User;
@Column()
username: string;
@ManyToOne(() => Post)
post: Post;
@ManyToOne(() => Comment)
comment: Comment;
}
这个叫做评论
import {
Entity as TOEntity,
Column,
ManyToOne,
OneToMany,
JoinColumn,
BeforeInsert,
Index
} from "typeorm";
import Entity from "./Entity";
import User from "./User";
import Post from "./Post";
import Vote from "./Vote";
import { makeid } from "../util/helpers";
@TOEntity("comments")
export default class Comment extends Entity {
constructor(comment: Partial<Comment>) {
super();
Object.assign(this, comment);
}
@Index()
@Column()
identifier: string;
@Column()
body: string;
@Column()
username: string;
@ManyToOne(() => User)
@JoinColumn({ name: "username", referencedColumnName: "username" })
user: User;
@ManyToOne(() => Post, post => post.comments, { nullable: false })
post: Post;
@OneToMany(() => Vote, vote => vote.comment)
votes: Vote[];
protected userVote: number;
setUserVote(user: User) {
const index = this.votes.findIndex(v => v.username === user.username);
this.userVote = index > -1 ? this.votes[index].value : 0;
}
@BeforeInsert()
makeIdAndSlug() {
this.identifier = makeid(8);
}
}
问题:所以,我有投票和评论之间的关系,问题是在投票实体中我们有这行代码@ManyToOne(() => Comment) comment: Comment;
在评论实体中,我们还有另一行代码 @OneToMany(() => Vote, vote => vote.comment) votes: Vote[];
目标:现在,为什么在 Vote 实体中我们只放置 () => 评论,插入 () => comment.votes ?我需要了解它及其功能,如果可以帮助我,我会感谢你!
感谢您的时间