TypeOrm-自定义多对多不拉关系数据

时间:2020-04-15 18:36:22

标签: typeorm

因为我不想使用级联来更新联接表并且我想要自定义列,所以我创建了一个自定义的多对多关系。但是,当我查询该关系时,它仅提供联接表中的值,而不提取关系数据。

用户

@Entity('user')
export class User {
  @PrimaryColumn()
  id: string;

  @OneToMany(
    () => UserArtistFollowing,
    (userArtistFollowing) => userArtistFollowing.user
  )
  following: UserArtistFollowing[];
}

艺术家


@Entity('artist')
export class Artist {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @OneToMany(
    () => UserArtistFollowing,
    (userArtistFollowing) => userArtistFollowing.artist
  )
  usersFollowing: UserArtistFollowing[];

}

UserArtist关注


@Entity('userArtistFollowing')
export class UserArtistFollowing {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  userId!: string;

  @Column()
  artistId!: string;

  @ManyToOne(
    () => User,
    (user) => user.following
  )
  user!: User;

  @ManyToOne(
    () => Artist,
    (artist) => artist.usersFollowing
  )
  artist!: Artist;

  @CreateDateColumn()
  createdAt!: Date;

  @UpdateDateColumn()
  updatedAt!: Date;
}

查询

    const user = await getManager()
      .getRepository(Models.User)
      .findOne({
        where: { id: id },
        relations: ['following'],
      });

因此该查询仅提供id,userId,artistId,而不提供对象的Artist数组(这是我需要的数据)。

有想法吗?

2 个答案:

答案 0 :(得分:1)

经过进一步的测试,事实证明您可以将find->关系与这类自定义的多对多关系一起使用。您只需要为嵌套关系指定点分符号即可。

      const user = await getManager()
        .getRepository(Models.User)
        .findOne({
          where: { id },
          relations: [
// you have to specify the join table first (following) to retrieve the columns in the join table
            'following',
// then you use dotted notation to get the relation from the join table
            'following.artist',
// another example of a deeply nested relation
            'favourites',
            'favourites.song',
            'favourites.song.supportingArtists',
            'favourites.song.supportingArtists.artist',
          ],
        });

您还可以将join与嵌套的leftJoinAndSelect一起使用,但更为繁琐。

    const user = await getManager()
      .getRepository(Models.User)
      .findOne({
        where: { id },
        join: {
          alias: 'user',
          leftJoinAndSelect: {
            following: 'user.following',
            artists: 'following.artist',
          },
        },
      });

这是更新的实体

UserArtist关注

@Entity('userArtistFollowing')
export class UserArtistFollowing {
  @PrimaryColumn()
  userId: string;

  @PrimaryColumn()
  artistId: string;

  @ManyToOne(
    () => User,
    (user) => user.following
  )
  user!: User;

  @ManyToOne(
    () => Artist,
    (artist) => artist.usersFollowing
  )
  artist!: Artist;

  @CreateDateColumn()
  createdAt!: Date;

  @UpdateDateColumn()
  updatedAt!: Date;
}

艺术家

@Entity('artist')
export class Artist {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @OneToMany(
    () => UserArtistFollowing,
    (userArtistFollowing) => userArtistFollowing.artist
  )
  usersFollowing: UserArtistFollowing[];
}

用户

@Entity('user')
export class User {
  @PrimaryColumn()
  id: string;

  @OneToMany(
    () => UserArtistFollowing,
    (userArtistFollowing) => userArtistFollowing.user
  )
  following: UserArtistFollowing[];
}

答案 1 :(得分:-1)

如果您始终希望艺术家模型完全水合,则可以尝试在RelationOptions中设置eager: true

https://typeorm.io/#/eager-and-lazy-relations/eager-relations

如果您不想要那样,那么您的解决方案就很有意义。