简体   繁体   中英

How to order the GET response using loopback

I've a simple model which looks something like this:

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

When retrieving all entries, I would like to order them per default by the position field value. What would be the best way to go about this? Using a model hook? But how would I do it? I'm not sure about the implementation.

You can add a default scope to your model like so:

{
    "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"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM