ArangoDB和Foxx - 来自GET的多个查询参数

问题描述 投票:3回答:1

(抱歉新手问题,但在文档中找不到)

我希望有一个包含几个模型的文档存储,然后在我的Foxx服务中使用一些属性作为查询中的参数。说我有一个电影和系列剧集的数据库:

{
    'type':'movie',
    'year':'1997',
    'director':'xxxxxxx',
    ...
},
{
    'type':'series_episode',
    'season':'1',
    'episode':'3',
    ...
}
...

我需要能够搜索

当然我想做的是拥有一个支持GET /?type = movie year = x director = y的路由器.GET /?type = series season = x episode = y这可能吗?容易吗?

我找不到,所以我开始认为我必须为每种类型设置不同的路由器,如下所示:

router.get('/movies', function (req, res) {
        const data = db._query('FOR entry IN mystore FILTER entry.type == @type, entry.year == @year RETURN entry ', 
        {'type':'movie', .....});
                res.json({
                    result: data
                })
});


router.get('/series', function (req, res) {
        const data = db._query('FOR entry IN mystore FILTER entry.type == @type, entry.season == @season, entry.episode == @episode, RETURN entry ', 
        {'type':'series', .....});
                res.json({
                    result: data
                })
})

这将是繁重的工作维持。理想情况下,我只是更新模型并使用一个路由器。

即使对于最后一个选项,我也有一个问题:如何将多个参数传递给查询?我找不到语法。

任何帮助表示赞赏。我正在学习ArangoDB,我对这种潜力很感兴趣,但我无法浏览我看到的文档或示例。

谢谢

arangodb foxx
1个回答
1
投票

这个问题is meanwhile covered in the Foxx Manualin detail in the endpoints documentation

可以通过将queryParam(...)s指定到JOI路由器定义来访问查询参数,稍后在函数体中可以通过req.queryParams.yourQueryParam访问它们。

请注意,您可以使用Web界面中的API-Tab来使用swagger以交互方式探索API。

接受两个查询参数的非常简单的Foxx服务可能如下所示:

'use strict';

const joi = require('joi');

const router = require('@arangodb/foxx/router')();
module.context.use(router);
router.get('/hello/', function (req, res) {
    res.send(JSON.stringify({hello: `world of FirstName: ${req.queryParams.fname} LastName: ${req.queryParams.lname}`}));
})
.queryParam('fname', joi.string().required(), 'First Name to greet.')
.queryParam('lname', joi.string().required(), 'Last Name to greet.')
.response(['text/plain'], 'A personalized greeting.')
.summary('Personalized greeting')
.description('Prints a personalized greeting.');

调用可能如下所示:

curl -X GET "http://127.0.0.1:8529/_db/_system/myfoxx/hello?fname=Joe&lname=Smith"
...
{"hello":"world of FirstName: Joe LastName: Smith"}

在Path中,参数可以像这样实现:

'use strict';

const joi = require('joi');

const router = require('@arangodb/foxx/router')();
module.context.use(router);
router.get('/hello/:fname/:lname', function (req, res) {
    res.send(JSON.stringify({hello: `world of FirstName: ${req.pathParams.fname} LastName: ${req.pathParams.lname}`}));
})
.pathParam('fname', joi.string().required(), 'First Name to greet.')
.pathParam('lname', joi.string().required(), 'Last Name to greet.')
.response(['text/plain'], 'A personalized greeting.')
.summary('Personalized greeting')
.description('Prints a personalized greeting.');

并且可以像这样进行调用:

curl -X GET "http://127.0.0.1:8529/_db/_system/myfoxx/hello/Joe/Smith" 
...
{"hello":"world of FirstName: Joe LastName: Smith"}
© www.soinside.com 2019 - 2024. All rights reserved.