TypeORM, add condition in `where` if value is presented and not empty string


I am using TypeOrm in my node.js project. I know to find a record from database I can do :

userRepository.find({ where: { firstName: "John" } });

It executes query:

SELECT * FROM "user"
WHERE "firstName" = 'John'

But now I need do add another filed check in "where" condition only if the value is presented. For example, I want to also check company in SQL "where" condition, but only if company value is presented.

I tried following, I wonder can I do the following by giving a default empty string '' if company doesn't present then pass it to find function's where?

const company = params.company ? params.company : '';

userRepository.find({ where: { firstName: "John", company: company } });

But it would still add "company"='' in the final SQL query which is not good. I wonder is there an existing function in TypeORM that could dynamically decide only add more condition in where if value is presented and not empty string?


You can use destruction for it. Like:

   where: {
      firstName: "John",
      ...(params?.company && { company: params.company }),

so, if params.company is undefined (or empty string) then

...(undefined && { company: undefined })

returns undefined and for the destruction it like ...{} for you.

if params.company contain some value, the

...('company' && { company: 'company' })

returns ...{company: 'company'} and destruct this for your where.


const companyTestWithValue = 'company';
const companyTestWithoutValue = '';

const whereWithValue = {
  firstName: 'John',
  ...(companyTestWithValue && { company: companyTestWithValue }),

const whereWithoutValue = {
  firstName: 'John',
  ...(companyTestWithoutValue && { company: companyTestWithoutValue }),

console.log('whereWithValue:', whereWithValue);
console.log('whereWithoutValue:', whereWithoutValue);

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at


Login to comment


TypeORM, need to add "WHERE IN (...)" in query condition & only when there is a value for it

Add " | " in string if where string value is not null or empty

Multiple where condition in typeorm

WHERE condition based on arrays in typeorm

typeOrm using condition in where clause

Query condition where a value is in a comma separated string

Is there a where condition to compare the value of the string after splitting?

Add where condition for check attribute value in XML nodes

Pandas add a new column with a string where the cell match a particular condition

Python: Copy a Row Value and Add to Another Row where a cell is empty

Where clause as string in Find/FindOne TypeOrm

python sqlite - where condition is empty

Mongo Aggregation Query - Filter Condition where string contains value

how to use WHERE condition to find specific value from string in table?

TypeORM find where clause, how to add where in multiple parameter

Add a string to a column only when the value matches a condition in pyspark

Parcelable android add empty char to the String field value

How do I add initial value to empty string?

Terraform condition not working for empty value

Add a condition with alias value

Condition for a non-empty string

Verifying a string is not empty/null in an If condition

How to add where condition to list?

How to add where in 3 condition

Angular ngIf to add selected attribute to option where value is string

Add string to a string based on condition

SQL query with WHERE condition that allows empty fields

add characters of a string to an empty string

BIGQUERY : value filter with where and condition