Typeorm 带有子条件的预加载

huangapple go评论110阅读模式
英文:

Typeorm eager load with a condition for the children

问题

I have two entities, one parent and one child. There is a OneToMany relation with eager enabled.

When I get the parent entity I would like to load only children with the property deleted set to false. For now, it's returning all children.

Here are my files:

@Entity()
export class Parent {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ default: false })
  deleted: boolean;

  @OneToMany(() => Child, (child) => child.parent, {
    cascade: true,
    eager: true,
  })
  children: Child[];
}
@Entity()
export class Child {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ default: false })
  deleted: boolean;

  @ManyToOne(() => Parent, (parent) => parent.children)
  parent: Parent;
}
@Injectable()
export class ParentsService {
  constructor(
    @InjectRepository(Parent)
    private parentRepository: Repository<Parent>,
  ) {}

  findAll(): Promise<Parent[]> {
    return this.parentRepository.find({ where: { deleted: false } });
  }
}

I tried to change my findAll like this, but then if my parent has one child deleted, the parent won't be retrieved:

  findAll(): Promise<Parent[]> {
    return this.parentRepository.find({ where: { deleted: false, children: { deleted: false } } });
  }
英文:

I have two entities, one parent and one child. There is a OneToMany relation with eager enabled.

When I get the parent entity I would like to load only children with the property deleted to false. For now it's returning all children.

Here are my files:

@Entity()
export class Parent {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ default: false })
  deleted: boolean;

  @OneToMany(() =&gt; Child, (child) =&gt; child.parent, {
    cascade: true,
    eager: true,
  })
  children: Child[];
}
@Entity()
export class Child{
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ default: false })
  deleted: boolean;

  @ManyToOne(() =&gt; Parent, (parent) =&gt; parent.children)
  parent: Parent;
}
@Injectable()
export class ParentsService {
  constructor(
    @InjectRepository(Parent)
    private parentRepository: Repository&lt;Parent&gt;,
  ) {}

  findAll(): Promise&lt;Parent[]&gt; {
    return this.parentRepository.find({ where: { deleted: false } });
  }
}

I tried to change my findAll like this, but then if my parent as one child deleted, the parent wont be retrieved :

  findAll(): Promise&lt;Parent[]&gt; {
    return this.parentRepository.find({ where: { deleted: false, children:{deleted:false} } });
  }

答案1

得分: 1

I guess, the best way to do that, will be to use @DeleteDateColumn(), as soft-delete is already inside typeorm.

@Entity()
export class Child {
    @PrimaryGeneratedColumn()
    id: number;

    @DeleteDateColumn()
    deletedAt: Date;

    @ManyToOne(() => Parent, (parent) => parent.children)
    parent: Parent;
}

You can soft-remove your entity

this.parentRepository.softRemove(child);

And you don't need any where clause.

findAll(): Promise<Parent[]> {
    return this.parentRepository.find();
}

To find with deleted

this.parentRepository.find({ withDeleted: true });
英文:

I guess, the best way to do that, will be to use @DeleteDateColumn(), as soft-delete is already inside typeorm.

@Entity()
export class Child {
    @PrimaryGeneratedColumn()
    id: number;

    @DeleteDateColumn()
    deletedAt: Date;

    @ManyToOne(() =&gt; Parent, (parent) =&gt; parent.children)
    parent: Parent;
}

You can soft-remove your entity

this.parentRepository.softRemove(child);

And you don't need any where clause.

findAll(): Promise&lt;Parent[]&gt; {
    return this.parentRepository.find();
  }

To find with deleted

this.parentRepository.find({ withDeleted: true });

huangapple
  • 本文由 发表于 2023年4月16日 23:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定