英文:
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 "%XYZ%"
I have following code in my controller file:
@Get('getByLicenseKey/:licenseKey')
async getByLicenseKey(@Param('licenseKey') licenseKey: string) {
console.log('dsaadfad');
return this.licenseKeyService.getKeyByLicenseKey(licenseKey);
}
Service file
async getKeyByLicenseKey(licenseKey: string): Promise<keys[]> {
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 'typeorm';
async getKeyByLicenseKey(licenseKey: string): Promise<Key[]> {
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<keys[]> {
return this.keyRepo.find({
where:{licenseKey: Like(`%${licenseKey}%`)}
})
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论