【问题标题】:ArangoDB and Foxx - multiple query parameters from GETArangoDB 和 Foxx - 来自 GET 的多个查询参数
【发布时间】:2017-09-21 17:48:28
【问题描述】:

(对不起新手问题,但在文档中无法轻松找到)

我想要一个包含几个模型的文档存储,然后在我的 Foxx 服务中使用一些属性作为查询中的参数。 假设我有一个用于电影和连续剧集的数据库:

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

我需要能够搜索

当然,我想做的是有一个单一路由器来支持两者 获取 /?type=movie&year=x&director=y.. 获取 /?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】:

    这个问题is meanwhile covered in the Foxx Manual和in detail in the endpoints documentation。

    查询参数可以通过在JOI-router定义中指定queryParam(...)s来访问,稍后在函数体中可以通过req.queryParams.yourQueryParam访问。

    请注意,您可以使用网络界面中的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"}
    

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

    【讨论】:

    • 谢谢,这回答了这个问题。还有一件事——我的目标是在过滤器中使用这些参数。如何支持可选参数?例如我想过滤名字或姓氏` router.get('/people', function (req, res) { const keys = db._query(aql' FOR entry IN ${foxxColl} FILTER entry.firstName == $ {req.queryParams.fname} FILTER entry.lastName == ${req.queryParams.lname} RETURN entry '); res.send(keys); }) .queryParam('fname', joi.string().optional( ), 'First Name.') .queryParam('lname', joi.string().optional(), 'Last Name.') ...`
    • @costateixeira 您可以执行FILTER !${req.queryParams.fname} || entry.firstName == ${req.queryParams.fname} 之类的操作(即显式检查参数是否已设置),也可以使用 aql 辅助方法构建过滤器(嵌套 aql 模板并使用 aql.join ): arangodb.com/docs/stable/…
    • 感谢@AlanPlum,但如果我这样做FILTER !${req.queryParams.fname} || entry.firstName == ${req.queryParams.fname},我会在'|| 附近遇到意外或操作员entry.firstName == \n\t\tFILTER ...' 在位置 3:12(解析时)-我猜如果参数为空,则该行在语法上不正确..?如果我能让它工作,aql.join 会很好。有没有关于如何有条件地定义过滤器的例子?再次,对不起菜鸟问题。谢谢!
    • @costateixeira 啊,对不起,我看错了。未定义的值将被忽略。尝试将值默认为null。例如。 const {fname = null} = req.queryParams; 然后使用 fname 而不是 req.queryParams.fname。我认为,使用 joi joi.string().optional().default(null) 也应该可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多