TypeORM - postgresSQL / 在数据库中保存数据

TypeORM - postgresSQL / saving data in the DB

所以,我是这个 typeORM 的新手,实际上也是 postgresSQL DB 的新手,关于 typeORM 和在 table 之间建立关系,有些事情我无法理解。

我的问题:所以,我有两个实体,User 和 Post。当您创建 post 时,我们使用 @JoinColumn 将用户(post 的创建者)存储在数据库中,当我转到用户 table 时,我可以看到该用户的名称字段(用户名),但是,在用户实体内部,我们有一个 Post 数组,但是,该字段没有出现在 postgres 数据库中,所以,当我创建关系时,@ ManyToOne 和@OneToMany,哪些数据存储在数据库中,哪些不存储?除此之外,当我获取东西时,我可以获取数组,但是,该数组存储在数据库中还是什么?我对此有点困惑,所以,现在让我向您展示代码

用户实体

import {
  Entity as TOEntity,
  Column,
  Index,
  BeforeInsert,
  OneToMany
} from "typeorm";
import bcrypt from "bcrypt";
import { IsEmail, Length } from "class-validator";
import { Exclude } from "class-transformer";

import Entity from "./Entity";
import Post from "./Post";

@TOEntity("users")
export default class User extends Entity {
  constructor(user: Partial<User>) {
    super();
    Object.assign(this, user);
  }

  @Index()
  @IsEmail(undefined, { message: "Must be a valid email address" })
  @Length(5, 255, { message: "Email is empty" })
  @Column({ unique: true })
  email: string;

  @Index()
  @Length(3, 200, { message: "Must be at leat 3 characters long" })
  @Column({ unique: true })
  username: string;

  @Exclude()
  @Length(6, 200, { message: "Must be at leat 3 characters long" })
  @Column()
  password: string;

  @OneToMany(() => Post, post => post.user)
  posts: Post[];

  @BeforeInsert()
  async hashedPassword() {
    this.password = await bcrypt.hash(this.password, 6);
  }
}

Post实体

import {
  Entity as TOEntity,
  Column,
  Index,
  BeforeInsert,
  ManyToOne,
  JoinColumn,
  OneToMany
} from "typeorm";

import Entity from "./Entity";
import User from "./User";
import { makeid, slugify } from "../util/helpers";
import Sub from "./Sub";
import Comment from "./Comment";

@TOEntity("posts")
export default class Post extends Entity {
  constructor(post: Partial<Post>) {
    super();
    Object.assign(this, post);
  }

  @Index()
  @Column()
  identifier: string; // 7 Character Id

  @Column()
  title: string;

  @Index()
  @Column()
  slug: string;

  @Column({ nullable: true, type: "text" })
  body: string;

  @Column()
  subName: string;

  @ManyToOne(() => User, user => user.posts)
  @JoinColumn({ name: "username", referencedColumnName: "username" })
  user: User;

  @ManyToOne(() => Sub, sub => sub.posts)
  @JoinColumn({ name: "subName", referencedColumnName: "name" })
  sub: Sub;

  @OneToMany(() => Comment, comment => comment.post)
  comments: Comment[];

  @BeforeInsert()
  makeIdAndSlug() {
    this.identifier = makeid(7);
    this.slug = slugify(this.title);
  }
}

User 实体在 DB

中如何显示为 table

因此,如您所见,没有名称为 posts 的字段(这很奇怪,因为正如我已经说过的,如果我可以获取该数据,那么如果我看不到该数据在哪里它在数据库中)

现在,让我向您展示Post实体

我想了解的内容:所以,我们有 table 之间的关系,知道,我试图搜索一些东西以了解这一点,但我找不到任何东西,所以,如果你能帮我解决这个烂摊子,我真的很感激,所以,谢谢你的时间!

让我们通过这一部分,一点一点地理解:

@ManyToOne(() => User, user => user.posts)
@JoinColumn({ name: "username", referencedColumnName: "username" })
user: User;

1。 @ManyToOne(() => User, user => user.posts):

  • @ManyToOne:此注释告诉 typeORM Post 实体将具有多对一关系。从 postgres DB 的角度来看,这意味着 posts table 将有一个新列(外键)指向其他 table.[=46= 中的记录]

  • () => User:这叫做定义目标关系。这有助于 typeORM 理解关系的目标是 User 实体。对于 postgres DB,这意味着 posts table 中的外键将引用 users 数据库中的一行

  • user => user.posts:这叫做逆关系。这告诉 typeORM User 实体中关系的相关 属性 是 posts。从 postgres DB 的角度来看,这没有任何意义。只要有外键引用,就可以保持两个table的关系。

2。 @JoinColumn({ name: "username", referencedColumnName: "username" }):

  • @JoinColumn:在这种场景下,这个注解帮助typeORM理解poststable中外键列的名字和被引用的名字users table

    中的列
  • name: "username":这是 posts table 中的列的名称,它将唯一标识 users 中的记录 table

  • referencedColumnName: "username":这是 users table 中的列的名称,它将被外键 username 引用posts table.


inside User entity, we have an array of Posts, but, that field doesn't appear in the postgres DB

帖子数组用于 typeORM 到 return 你的链接帖子数组。 postgres 数据库不需要它来包含关系。

when i create a relation, @ManyToOne and @OneToMany, what data stores in the DB and which don't

您使用 @Column 装饰的任何 属性 都将原样出现在 table 中。对于关系,只会保存外键。例如,当您保存 Post 实体时,它将仅保存该实体中的相关列 + username 外键。

when i fetch stuff, i can fetch the array, but, does that array is store in the DB or what ?

当您查询 User 实体时,typeorm 使用注释将 users table 与 posts table 和 return 连接起来posts 与您搜索的用户。但是在数据库中,它将usersposts数据保存在各自的table中,并使用username外键来保持它们之间的关系。


我希望这可以帮助您了解发生了什么。干杯!!!