Home > Software engineering >  TypeORM, condition for two database columns at the same row
TypeORM, condition for two database columns at the same row

Time:11-17

I'm currently working on project with NestJS and I just wanna ask if there is a way to set condition for two database columns at the same row in typeORM in where option

ex: I have two columns in database postpondedDate and createdAt... at first I'll check if postpondedDate is not null, if it's not I'll use MoreThan(new Date()), if it's null I'll go to check the value of createdAt with the same option MoreThan(new Date())

here is an example code:

async handleCron(){
   const activeStudents = await this.invoiceService.find({
      select: ['id', 'student', 'postpondedDate', 'createdAt'],
      relations: ['student'],
      where: {
         // if postponded date !== null I'll use
         // postpondedDate: MoreThan(new Date()),

         // if postponded date === null dont check this column and go to createdAt
         // createdAt: MoreThan(new Date()),
      },
   });
}

Thanks in advance

CodePudding user response:

where: [
    {postponded: MoreThan(new Date())},
    {postponded:null, createdAt: MoreThan(new Date())},
  }]

That's simply an OR: postponded is higher OR (postponded is null AND created is higher)

  • Related