Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have IS NULL condition in TypeORM find options?

In my queries I'm using TypeORM find option. How can I have IS NULL condition in the where clause?

like image 787
user4092086 Avatar asked Oct 22 '17 22:10

user4092086


3 Answers

Another way is you can use IsNull() function, for example:

import { IsNull } from "typeorm";
return await getRepository(User).findOne({
    where: { 
      username: IsNull()
    }
});
like image 88
hungneox Avatar answered Nov 09 '22 11:11

hungneox


If someone is looking for NOT NULL, it would be like this:

import { IsNull, Not } from "typeorm";

return await getRepository(User).findOne({
    where: { 
      username: Not(IsNull())
    }
});

like image 93
Carlos Vallejo Avatar answered Nov 09 '22 10:11

Carlos Vallejo


You can use QueryBuilder for this purpose:

const users = await userRepository.createQueryBuilder("user")
     .where("user.name IS NULL")
     .getMany();
like image 15
pleerock Avatar answered Nov 09 '22 09:11

pleerock