【问题标题】:Where clause as string in Find/FindOne TypeORMFind/FindOne TypeORM 中的 Where 子句作为字符串
【发布时间】:2021-12-11 10:39:20
【问题描述】:

我正在搜索和阅读文档,但找不到解决方案。

有没有办法在 TypeORM 的 find/findOne 方法中将 where 参数设置为字符串?

因为,TypeORM 似乎不接受私有属性作为 find/findOne 方法中的参数,我明白为什么。

例如,这是使用的类:

import { Column } from 'typeorm';

@Entity()
export default class Client extends BaseEntity{
    ...

    @Column({ name: 'email', type: 'varchar', length: 64, unique: true })
    private _email: string;

    ...
}

这是控制器功能:

...

async function testing(request: Request, response: Response, next: NextFunction) {
    const { name, email, password } = request.body;
    
    const existingClient = await Client.findOne({ _email: email });

    console.log(existingClient);
}

...

我想创建类似的东西:

await Client.findOne({`email = testing@email.com`});

有没有办法做到这一点?

【问题讨论】:

标签: typescript typeorm


【解决方案1】:

我建议将其简单地用作public

当你真的想使用private时,你可能需要使用entitymanager:

entityManager
    .createQueryBuilder(Client, "client")
    .where("client._email = :email", { email: 'testing@email.com' })
    .getOne();

什么是以及如何添加entityManager:https://typeorm.io/#/working-with-entity-manager

【讨论】:

  • 我认为这是最好的解决方案,谢谢! :D
猜你喜欢
  • 2013-08-24
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多