K
K
keyotor2021-09-08 13:24:07
PostgreSQL
keyotor, 2021-09-08 13:24:07

How to find all matches in a table using typeorm?

Please tell me, there is a users table in which there is a username field, username comes to the backend request
and I need to find all matches in the table that are similar to the username that came from the client

async findUsers(username) {
        const users = await this.userRepository.find({where: {username}});
        return users;
    }

thus, it will only find if the exact username came from the client, but for example, there is a user with username: test in the table, and part of this username came from the client, for example, only tes, then how to find all matches that are similar?
I am using typeform.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuriy Vorobyov, 2021-09-08
@keyotor

You need to use the like operator

import { Like } from "typeorm";

const users = await this.userRepository.find({
    field_to_use_for_find: Like(`%${username}%`)
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question