如何使用环回命令 GET 响应

How to order the GET response using loopback

我有一个看起来像这样的简单模型:

{
  "name": "reason",
  "plural": "reasons",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "position": {
      "type": "number",
      "required": true
    }
}

检索所有条目时,我想默认按位置字段值对它们进行排序。最好的方法是什么?使用模型挂钩?但我该怎么做呢?我不确定实施情况。

您可以像这样向您的模型添加 default scope

{
    "name":        "reason",
    "plural":      "reasons",
    "base":        "PersistedModel",
    "idInjection": true,
    "options":     {
        "validateUpsert": true
    },
    "properties":  {
        "title":    {
            "type":     "string",
            "required": true
        },
        "position": {
            "type":     "number",
            "required": true
        }
    },
    "scope":       {
        "order": "position"
    }
}