Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

javascript - TypeORM nestjs-选择使用相同存储库的联接表中的用户(TypeORM nestjs - Select user that is in a join table using the same repository)

This is more of an architecture problem.

(这更多是架构问题。)

I can solve this issue by accessing two repositories (you will understand why in a bit) and join them in code.

(我可以通过访问两个存储库(稍后您会理解为什么)并将它们加入代码中来解决此问题。)

I have two models User and Customer .

(我有两个模型UserCustomer 。)

User has many customers that he handles, and customer has many users that handles them, therefore we create a join table named Representative which represents the join of user and customer.

(用户有许多要处理的客户,而客户有许多要处理的用户,因此我们创建了一个名为“ Representative的联接表,该表Representative用户和顾客的联接。)

@Entity('representatives')
export class RepresentativeEntity {

  @PrimaryGeneratedColumn()
  id: string;

  @ManyToOne(type => User, user => user.customers)
  user: User;

  @ManyToOne(type => Customer, customer => customer.users)
  customer: Customer;

  @CreateDateColumn()
  creationDate: Date;
}

My application is build with NestJS with the provider-service-repository pattern where the provider uses services to perform logic and every service has many repositories (no other services) and every repository works by its own.

(我的应用程序是使用带有提供者-服务-存储库模式的NestJS构建的,其中,提供者使用服务来执行逻辑,并且每个服务都有许多存储库(没有其他服务),并且每个存储库都可以独立工作。)

Now I prefer not to use 2 repositories on the service layer and then join the data or pass the other repository what IDs to select.

(现在,我不想在服务层上不使用2个存储库,然后加入数据或将其他ID传递给其他存储库。)

How can I create a query that receives filters, one of the filters is get all customers that are handled by array of user ids.

(我如何创建一个接收过滤器的查询,过滤器之一是获取由用户ID数组处理的所有客户。)

How can I do this without using the repository of Representative and just use one query builder?

(我如何在不使用“ Representative ”存储库而仅使用一个查询生成器的情况下做到这一点?)

    const builder: SelectQueryBuilder<Customer> = this.createQueryBuilder().select();

    // Freetext filters
    if (filters.freetext) {
        builder.where(`first_name LIKE :firstName`, {firstName: `%${filters.freetext}%`});
        builder.orWhere(`last_name LIKE :lastName`, {lastName: `%${filters.freetext}%`});
        builder.orWhere(`email LIKE %:email%`, {email: `%${filters.freetext}%`});
    }

    // // User assigned filter
    if (filters.assignedToUserIds) {
     // todo get all customers that are handled by the given user ids ...
    }
  ask by Ben Beri translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...