Strongloop api非常适合开箱即用。但是我目前正在尝试进行一些自定义。我正在尝试向category
模型添加一个远程方法,该方法与端点数据一起返回以下内容:perPage
,total
,paging
(详细信息如下所示) 。对于paging
,before
和after
是基于gameId
的参数。如果值为limit
或大于5
,则null
将默认为5
。什么是追加前面提到的结果的最佳方式?
例如,http://localhost:3000/api/Categories/1004/games/mature?before=1000053
将返回所有gameId
小于1000053
且before
所有gameIds
大于1000053
的所有 Category.findById(id, {}, function(err, category){
if (err) return callback(err);
//set limit
if (limit && limit > 5){
limit = 5;
}else if(limit === undefined){
limit = 5;
}
//set after cursor
Games.find({
"where": {
categoryId: id,
mature: true,
gameId: {gt: after}
},
"limit": limit
}, function(err, gameArr) {
if (err) return callback(err);
callback(null, gameArr);
});
});
。
公共/模型/ category.js
/Categories/1004/games/mature
{
“perPage”: 5, //value from limit
“total”: 5, //total of return items from array
"data": [
... Endpoint data is here
],
"paging": {
"cursors": {
"after": 1000057, //last item in array
"before": 1000053 //first item in array
},
"previous": "http://localhost:3000/api/Categories/1004/games/mature?before=1000053" //url for previous
"next": "http://localhost:3000/api/Categories/1004/games/mature?after=1000057" //url for after
}
}
{{1}}
答案 0 :(得分:2)
这里的问题是你使用Category模型作为入口点,当你看起来应该使用Game模型的内置查询方法时带有过滤器和限制和偏移,完全停止。这将允许您分页,而无需添加所有这些复杂性来支持您的前/后概念。这是不必要的。 如果您的关系设置正确,那么您在游戏中分页的所有内容都是游戏模型查询!
例如,您似乎需要在特定类别中成熟的分页游戏列表,对吧?要使用这些条件获得5个游戏的结果,您需要发送一个json REST API调用,如下所示:
http://0.0.0.0:3000/api/Games?filter=%7B%20%22where%22%3A%20%7B%22mature%22%3Atrue%2C%20%22categoryId%22%3A%201004%7D%2C%20%22offset%22%3A%200%2C%20%22limit%22%3A%205%20%7D
由于它的编码很难阅读,但底层过滤器的构造如下:
{ "where": {"mature":true, "categoryId": 1004}, "offset": 0, "limit": 5 }
这将返回以下数据集,即 1004 categoryId中的前5个游戏,也是成熟的:
[
{
"mature": true,
"gameName": "CODBlacOps3",
"categoryId": 1004,
"gameId": 1000053,
"description": "Published by Activision",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Evolve",
"categoryId": 1004,
"gameId": 1000054,
"description": "Published by Turtle Rock Studios",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Battlefield4",
"categoryId": 1004,
"gameId": 1000055,
"description": "Published by EA Digital Illusions",
"publishedDate": "2013-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Rainbow6",
"categoryId": 1004,
"gameId": 1000056,
"description": "Published by EUbisoft",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Destiny",
"categoryId": 1004,
"gameId": 1000057,
"description": "Published by Bungie",
"publishedDate": "2014-01-01T00:00:00.000Z"
}
]
要获得5个游戏的下一页,我们只需将偏移量更改为5,这会告诉数据库跳过前5个,因为我们现在在第2页上:< / p>
{ "where": {"mature":true, "categoryId": 1004}, "offset": 5, "limit": 5 }
返回以下结果:
[
{
"mature": true,
"gameName": "Wolfenstein",
"categoryId": 1004,
"gameId": 1000058,
"description": "Published by Bethesda",
"publishedDate": "2014-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "StarWarsBattleFront",
"categoryId": 1004,
"gameId": 1000059,
"description": "Published by EA DICE",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Test1",
"categoryId": 1004,
"gameId": 1000060,
"description": "Published by Test1",
"publishedDate": "2015-12-19T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Test2",
"categoryId": 1004,
"gameId": 1000061,
"description": "Published by Test2",
"publishedDate": "2015-12-19T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Test3",
"categoryId": 1004,
"gameId": 1000062,
"description": "Published by Test3",
"publishedDate": "2015-12-19T00:00:00.000Z"
}
]
默认情况下,游戏按照game_id的顺序排列。没有自定义远程方法和嵌套的find()!
如果您想按字母顺序排列列表,还可以添加order
过滤器,并将其设置为"order": "gameName ASC"
:
{ "where": {"mature":true, "categoryId": 1004}, "offset": 0, "limit": 5, "order": "gameName ASC" }
这将返回一个新的(第1页因为我将偏移设置为0)游戏数组,按gameName
按字母顺序排列:
[
{
"mature": true,
"gameName": "Battlefield4",
"categoryId": 1004,
"gameId": 1000055,
"description": "Published by EA Digital Illusions",
"publishedDate": "2013-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "CODBlacOps3",
"categoryId": 1004,
"gameId": 1000053,
"description": "Published by Activision",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Destiny",
"categoryId": 1004,
"gameId": 1000057,
"description": "Published by Bungie",
"publishedDate": "2014-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Evolve",
"categoryId": 1004,
"gameId": 1000054,
"description": "Published by Turtle Rock Studios",
"publishedDate": "2015-01-01T00:00:00.000Z"
},
{
"mature": true,
"gameName": "Rainbow6",
"categoryId": 1004,
"gameId": 1000056,
"description": "Published by EUbisoft",
"publishedDate": "2015-01-01T00:00:00.000Z"
}
]
我能够如此快速地获得此结果的方式是使用API Explorer。我分叉你的项目,更改了数据库连接值以匹配我的本地服务器,启动它然后转到http://0.0.0.0/explorer/#!/Games/Games_find
,然后在游戏模型上使用不同的filter
值:
答案 1 :(得分:2)
要向响应添加结果,请首先更改Category.mature远程方法返回给对象的响应类型:
Category.remoteMethod('mature', {
accepts: [
{arg: 'id', type: 'number', required: true},
{arg: 'limit',type: 'number',required: false},
{arg: 'after',type: 'number',required: false},
{arg: 'before',type: 'number',required: false}
],
// mixing ':id' into the rest url allows $owner to be determined and used for access control
http: {
path: '/:id/games/mature',
verb: 'get'
},
returns: {
arg: 'games',
type: 'object' // will be whatever you pass back as 2nd arg to callback()
}
});
然后只需将您想要的值添加到新游戏对象中,使用现有的gamesArr作为data
的值,并将值传入限制值,值之前和之后传入响应对象中作为成功回调的第二个参数:
callback(null, {
"perPage": limit,
"total": gameArray.length,
"data": gameArray,
"paging": {
"cursors": {
"after": gameArray[gameArray.length-1].game_id, // last game_id in result
"before": gameArray[0].game_id // first game_id in result
},
"previous": "http://localhost:3000/api/Categories/1004/games/mature?before=" + gameArray[0].game_id,
"next": "http://localhost:3000/api/Categories/1004/games/mature?after=" + gameArray[gameArray.length-1].game_id
}
})
正如我提到的那些过多复杂代码的Games.find()调用,你也不应该删掉该对象的3或4个副本。您可以在逻辑中包装对象的构造,然后进行单个callback()调用以简化远程方法的I / O管理并编写更少的代码。减少到单个Games.find()调用将使发送此结果更容易。
另请注意卷曲的撇号字符和制表符与空格缩进(选择一个并坚持使用,不要混合),在构建和组织代码时更容易帮助。