In Typeorm, how do you find filter for records with array relation length > 0

Viewed 587

I have an entity like this

@Entity()
export class Order {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @OneToMany(() => Transaction, (t) => t.order, {
    cascade: true,
  })
  transactions: Transaction[];
}

How do I use find() to search for all records with transactions.length > 0?

I prefer to use find() but if it is not possible should I use the query builder?

1 Answers

Perhaps you could do this:

import { IsNull, Not } from "typeorm";

await getRepository(Order).findOne({
  relations: ['transactions'],
  where: { 
    transactions: Not(IsNull())
  }
});
Related