如何用typeor进行排序?

rej*_*ejs 1 node.js typeorm nestjs

该数据库包含以下数据:

[
  {id: 1, username: "test", amount: 2},
  {id: 2, username: "example", amount: 0},
  {id: 3, username: "hello", amount: 1},
  {id: 4, username: "fsa", amount: 0},
  {id: 5, username: "aas", amount: 10},
]
Run Code Online (Sandbox Code Playgroud)

现在我需要使用 typeorm 查询来检索数据,但我需要根据 amount 字段对其进行排序,并且限制只有 3 个用户,所以我应该得到这个:

 [
      {id: 5, username: "aas", amount: 10},
      {id: 1, username: "test", amount: 2},
      {id: 3, username: "hello", amount: 1},
    ]
Run Code Online (Sandbox Code Playgroud)

如何进行该查询?

Sor*_*ari 6

使用find以下解决方案的方法有效。查看 TypeORM 的文档以查找选项以获取更多信息和其他选项。

userRepository.find({
        take: 3,
        order: {
            amount: "ASC" // "DESC"
        }
    }
    
Run Code Online (Sandbox Code Playgroud)