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

user842225

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?

PZBird

You can use destruction for it. Like:

userRepository.find({
   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.

Example:

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 [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

How to add where in 3 condition

How to add where condition to list?

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

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

Parcelable android add empty char to the String field value

Condition for a non-empty string

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

Add where condition for check attribute value in XML nodes

How do I add initial value to empty string?

Add a condition with alias value

SQL query with WHERE condition that allows empty fields

python sqlite - where condition is empty

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

WHERE condition based on arrays in typeorm

Terraform condition not working for empty value

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

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

Query condition where a value is in a comma separated string

Where clause as string in Find/FindOne TypeOrm

Add string to a string based on condition

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

Multiple where condition in typeorm

typeOrm using condition in where clause

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

Mongo Aggregation Query - Filter Condition where string contains value

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

How to Add a certain condition in where clause on the basis of some value

Passing undefined value in WHERE condition behaves as true/matching condition in typeorm