简体   繁体   English

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

[英]ArangoDB and Foxx - multiple query parameters from GET

(sorry for the newbie question, but can't find it easily in documentation) (对于新手问题很抱歉,但在文档中无法轻松找到)

I want to have a document store with a few models, and then use some of the attributes as parameters in queries, in my Foxx services.我想要一个包含几个模型的文档存储,然后在我的 Foxx 服务中使用一些属性作为查询中的参数。 Say I have a db for movies and series episodes:假设我有一个电影和系列剧集的数据库:

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

I need to be able to search on我需要能够搜索

Of course what I would like to do is to have a single router that would support both GET /?type=movie&year=x&director=y.. GET /?type=series&season=x&episode=y Is that possible?当然,我想要做的是有一个路由器同时支持 GET /?type=movie&year=x&director=y.. GET /?type=series&season=x&episode=y 这可能吗? Easy to do?容易做吗?

I couldn't find out, so I started thinking that I have to have different routers for each of the types, like this:我找不到,所以我开始想我必须为每种类型使用不同的路由器,如下所示:

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
                })
})

That would be heavy work to maintain.这将是一项繁重的维护工作。 Ideally I would just update the models and use one single router.理想情况下,我只会更新模型并使用一个路由器。

Even for this last option I had a question: How do i pass more than one parameters to the query?即使对于最后一个选项,我也有一个问题:如何将多个参数传递给查询? I can't find the syntax.我找不到语法。

Any help is appreciated.任何帮助表示赞赏。 I am learning ArangoDB and I am very interested in the potential, but I can't navigate around the documentation I saw or examples.我正在学习 ArangoDB,我对潜力非常感兴趣,但我无法浏览我看到的文档或示例。

Thank you谢谢

This question is meanwhile covered in the Foxx Manual and in detail in the endpoints documentation .这个问题同时在 Foxx 手册端点文档中有详细介绍

Query parameters can accessed by specifying queryParam(...) s to the JOI-router definition, and later on in the function body they can be accessed via req.queryParams.yourQueryParam .查询参数可以通过在 JOI 路由器定义中指定queryParam(...)来访问,稍后在函数体中可以通过req.queryParams.yourQueryParam访问req.queryParams.yourQueryParam

Please note that you can use the API -Tab in the webinterface to use swagger to explore your API interactively.请注意,您可以使用 Web 界面中的API -Tab 来使用 swagger 以交互方式探索您的 API。

A very simple Foxx service accepting two query parameters could look like this:接受两个查询参数的非常简单的 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.');

An invocation could look like this:调用可能如下所示:

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 parameters could be achieved like this:在 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.');

And an invocation could be done like this:调用可以这样完成:

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

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

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