使用 type

编程入门 行业动态 更新时间:2024-10-24 20:16:31
本文介绍了使用 type-graphql typeorm 和 dataloader 处理一对多的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出使用 type-graphql 和 typeorm 和 postgresql db(使用 apollo 服务器和 express)处理一对多关系的最佳方法.我有一个与课程表具有一对多关系的用户表.我目前处理此问题的方法是使用 @RelationId 字段创建一列 userCourseIds,然后使用 @FieldResolver 和 dataloader 批量获取属于该用户的课程.我的问题是,对于 @RelationId 字段,无论我是否实际查询 userCourses,都会进行一个单独的查询来获取关系 ID.有没有办法解决这个问题,它不会产生额外的查询,或者有没有更好的方法来处理一对多关系?

I'm trying to figure out the best way to handle a one-to-many relationship using type-graphql and typeorm with a postgresql db (using apollo server with express). I have a user table which has a one-to-many relation with a courses table. The way I am currently handling this is to use the @RelationId field to create a column of userCourseIds and then using @FieldResolver with dataloader to batch fetch the courses that belong to that user(s). My issue is that with the @RelationId field, a separate query is made to get the relationids whether or not I actually query for userCourses. Is there a way to handle this where it won't make that extra query or is there a better way to go about handling one-to-many relationships?

关系的用户端:

@OneToMany(() => Course, (course) => course.creator, { cascade: true })
userCourses: Course[];
@RelationId((user: User) => user.userCourses)
userCourseIds: string;

关系的课程方面:

@Field()
@Column()
creatorId: string;

@ManyToOne(() => User, (user) => user.userCourses)
creator: User;

userCourses FieldResolver:

userCourses FieldResolver:

@FieldResolver(() => [Course], { nullable: true })
async userCourses(@Root() user: User, @Ctx() { courseLoader }: MyContext) {
  const userCourses = await courseLoader.loadMany(user.userCourseIds);
  return userCourses;
}

推荐答案

userCourses 字段可以使用 @TypeormLoader 装饰器编写如下所示://github/slaypni/type-graphql-dataloader" rel="nofollow noreferrer">type-graphql-datalaoader.

userCourses field can be written like below using @TypeormLoader decorator provided by type-graphql-datalaoader.

@ObjectType()
@Entity()
class User {
  ...

  @Field((type) => [Course])
  @OneToMany(() => Course, (course) => course.creator, { cascade: true })
  @TypeormLoader((course: Course) => course.creatorId, { selfKey: true })
  userCourses: Course[];
}

@ObjectType()
@Entity()
class Course {
  ...

  @ManyToOne(() => User, (user) => user.userCourses)
  creator: User;

  @RelationId((course: Course) => course.creator)
  creatorId: string;
}

由于省略了 userCourseIds,因此不会再发出其他查询.虽然 creatorId@RelationId 存在,但它不会发出额外的查询,因为实体有自己的值.

Additional queries will not be issued anymore since userCourseIds is omitted. Although creatorId with @RelationId exists, it will not issue extra queries since the entity has the value by its own.

@TypeormLoader 不再需要参数.因此 @RelationId 可以完全省略,如果愿意的话.

@TypeormLoader no longer needs arguments. Therefore @RelationId can be completely omitted, if preferred.

这篇关于使用 type-graphql typeorm 和 dataloader 处理一对多的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-27 01:53:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/674901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!