简体   繁体   English

更改回送中的默认检索顺序

[英]Change the default order of retrieval in loopback

I have a loopback api that retrieves some objects. 我有一个回送api,可以检索一些对象。 These objects has a property createdAt . 这些对象的属性为createdAt I want to change the order of the objects that are retrieved by the api based on this property. 我想基于此属性更改api检索的对象的顺序。 What is the best way to achieve this?? 实现此目标的最佳方法是什么?

You can use the order filter in several ways, on your : 您可以通过以下几种方式使用订单过滤器:

REST API : REST API:

This is for one property and you can choose ASC or DESC : 这是一个属性,您可以选择ASC或DESC:

filter[order]=createdAt <ASC|DESC>

This is for more than one property : 这是针对多个属性的:

filter[order][0]=createdAt <ASC|DESC>&filter[order][1][updatedAt]=<ASC|DESC>...

NODE API NODE API

model.find({
  order: 'createdAt DESC',
});

HERE the official documentation of the explanation above. 这里是上述解释的官方文件。

MODEL DEFINITION JSON 模型定义JSON

You can use the scope property in your JSON model file and I recommend it. 您可以在JSON模型文件中使用scope属性,我建议您使用它。

"scope": {
    "order": "createdAt ASC",
    "where": {
      "field": "something" // you can use the where filter also ...
    }
  },

Check this official documentation for more information about SCOPES 查看此官方文档以获取有关SCOPES的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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