Skip to content

Instantly share code, notes, and snippets.

@SantoshCode
Last active April 16, 2021 14:17
Show Gist options
  • Save SantoshCode/66028c0bdf5573d43019a9a0860d50eb to your computer and use it in GitHub Desktop.
Save SantoshCode/66028c0bdf5573d43019a9a0860d50eb to your computer and use it in GitHub Desktop.
Creating search items endpoint | NestJs, Typeorm

Endpoint to search keyword in the database.

async function getSearchedData (search:string){
  try {
    const searchData = return await this.connection
                                     .getRepository(UserEntity)
                                     .createQueryBuilder("user")
                                     .leftJoinAndSelect("user.roles", "roles")
                                     .where(
                                        "user.full_name LIKE :search OR user.username LIKE :search OR user.email LIKE :search",
                                        { search: `%${search}%` }
                                     )
                                     .getMany();
      return searchData
  } catch (error) {
     throw new InternalServerErrorException();
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment