我们可以像在 MongoDb 的方法中那样在 typeorm 的 findOne 中使用投影仪吗?

Can we have projectors in typeorm's findOne as we can have in MongoDb's method?

我只是好奇我们是否可以在typeorm的findOne方法中提供投影仪?我正在使用 postgres 数据库。

我需要的只是 table 中提供的电子邮件的 ID。我得到的是用户的所有详细信息。

repository.findOne({email: 'abc@abc.com'})

这给了我所有的细节。但是我只想提取该特定用户的 ID

我们可以将投影仪传递给上述查询吗?例如

repository.findOne({email: 'email'},{id:1})

是的,有可能。您需要将选项传递给 findOne 函数,这样您的代码将如下所示:

async getOne(email: string): Partial<RepositoryEntity> {
  const foundRow = repository.findOne({
    where: {
      email: email
    },
    select: 'id'
  });
  return foundRow;
}

async getOne(email: string): Partial<RepositoryEntity> {
  const foundRow = repository.findOne(
   { email: email },
   { select: 'id' }
  );
  return foundRow;
}

你可以看到定义类型的源代码here