question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can to search column with case insensitive with findAndCount

See original GitHub issue

Issue type:

[ x ] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ x ] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ‘x’ ] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

const [results, total] = await this.userRepository.findAndCount({
    where: {
        name: Like(`%${options.keywords}%`),
        status: Equal(options.status)
    },
    order: { id: "DESC" },
    take: options.limit,
    skip: options.skip,
})

How can to search the name column with case insensitive in findAndCount api. I saw another solution is use query builder like below , so how to achieve this in findAndCount. Thanks you so much.

const posts = await repository.createQueryBuilder()
     .where("LOWER(title) = LOWER(:title)", { title })
     .getMany();

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

32reactions
n1kalaicommented, Nov 23, 2020

this is what you need exactly

 const [result, total] = await this.findAndCount(
    {
  where: { title: Raw(alias => `LOWER(${alias}) Like '%${value}%'`) },

take: take,
  skip: skip
}

);
  return {
    data: result,
    count: total
  }
9reactions
wyqydsyqcommented, Jul 8, 2020

Just wondering, would it not be possible for TypeORM to support a pattern like this?

    const title = 'Foo'
    this.myRepository.find({
      where: {
        'LOWER(title)': Lower(title)
      },
    })

Where string properties that are not included in the model’s properties don’t get magically resolved to the associated model property column and are instead used literally in the query e.g.

SELECT * FROM `myModel` WHERE LOWER(title) = LOWER('Foo')

This requires the developer to ensure it matches up to actual database columns without any type safety against the model’s defined properties, but it’s still a nicer API than constructing queries manually with QueryBuilder where you don’t get any type safety on column names anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeOrm Postgres case sensitive on where clause value
I 'm having case-sensitivity issues with my select queries. This is my NestJs controller Search method: search(payload: SearchPayload): ...
Read more >
Case-Insensitive Search in SQL - Use The Index, Luke
Ignoring the case in a where clause is very simple. You can, for example, convert both sides of the comparison to all caps...
Read more >
Model Querying - Basics - Sequelize
Simple SELECT queries​. You can read the whole table from the database with the findAll method: // Find all users
Read more >
How to do case-insensitive and accent-insensitive search in ...
Accent-insensitive search; Sort rows regardless of case or accents; Define search and sorting rules for a column; Ignore case or diacritics ...
Read more >
How to Check Case-Sensitivity in SQL Server - Webucator
SQL Server is, by default, case insensitive; however, it is possible to create a case-sensitive SQL Server database and even to make specific...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found