How to find entire string using typeORM, Nestjs, SQL Server, just like LIKE clause in mysql

huangapple go评论62阅读模式
英文:

How to find entire string using typeORM, Nestjs, SQL Server, just like LIKE clause in mysql

问题

我有一个名为Beta的数据库和一个名为Keys的表。在该表中,我有一个列名为licenseKey: string。我想要从licenseKey的一部分找到整个licenseKey

就像下面的SQL查询一样:

SELECT * 
FROM KEYS 
WHERE licenseKey LIKE "%XYZ%"

我在我的控制器文件中有以下代码:

@Get('getByLicenseKey/:licenseKey')
async getByLicenseKey(@Param('licenseKey') licenseKey: string) {
    console.log('dsaadfad');
    return this.licenseKeyService.getKeyByLicenseKey(licenseKey);
}

服务文件

async getKeyByLicenseKey(licenseKey: string): Promise<keys[]> {
    return await this.keyRepo.find({ licenseKey: Like(`${licenseKey}`) });
}
英文:

I have database called Beta and table called Keys. In that table I have a column licenseKey: string. I want to find the entire licenseKey from the portion of licenseKey.

Just like SQL below query:

SELECT * 
FROM KEYS 
WHERE licenseKey LIKE &quot;%XYZ%&quot;

I have following code in my controller file:

        @Get(&#39;getByLicenseKey/:licenseKey&#39;)
   async getByLicenseKey(@Param(&#39;licenseKey&#39;) licenseKey: string) {
        console.log(&#39;dsaadfad&#39;);
        return this.licenseKeyService.getKeyByLicenseKey(licenseKey);
    }

Service file

async getKeyByLicenseKey(licenseKey: string): Promise&lt;keys[]&gt; {
        return await this.keyRepo.find({ licenseKey: Like(`${licenseKey}`) });
}

答案1

得分: 0

TypeORM提供了开箱即用的Like函数。来自它们的文档的示例:

要在NestJS和SQL Server中使用TypeORM执行部分字符串搜索,您可以利用TypeORM提供的Like运算符。以下是如何修改您的代码:

import { Like } from 'typeorm';

async getKeyByLicenseKey(licenseKey: string): Promise<Key[]> {
  return await this.keyRepo.find({ licenseKey: Like(`%${licenseKey}%`) });
}

确保从TypeORM导入Like运算符。在服务文件中,您可以使用Like运算符与%通配符来搜索列中的licenseKey值。这将检索Keys表中包含指定licenseKey值的许可密钥列的所有记录。

英文:

TypeORM provides out of the box Like function. Example from their docs:

To perform a partial string search using TypeORM with NestJS and SQL Server, you can utilize the Like operator provided by TypeORM. Here's how you can modify your code:

import { Like } from &#39;typeorm&#39;;

async getKeyByLicenseKey(licenseKey: string): Promise&lt;Key[]&gt; {
  return await this.keyRepo.find({ licenseKey: Like(`%${licenseKey}%`) });
}

Make sure you import the Like operator from TypeORM. In the service file, you can use the Like operator with the % wildcard to search for the licenseKey value within the column. This will retrieve all records from the Keys table where the licenseKey column contains the specified licenseKey value as a substring.

答案2

得分: 0

这对我来说有效。

async getKeyByLicenseKey(licenseKey: string): Promise<keys[]> {
    return this.keyRepo.find({
        where: { licenseKey: Like(`%${licenseKey}%`) }
    })
}
英文:

This works for me.

async getKeyByLicenseKey(licenseKey: string): Promise&lt;keys[]&gt; {
    return this.keyRepo.find({
        where:{licenseKey: Like(`%${licenseKey}%`)}
    })
}

huangapple
  • 本文由 发表于 2023年7月17日 12:42:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定