簡體   English   中英

ArangoDB 和 Foxx - 來自 GET 的多個查詢參數

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

(對於新手問題很抱歉,但在文檔中無法輕松找到)

我想要一個包含幾個模型的文檔存儲,然后在我的 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,我對潛力非常感興趣,但我無法瀏覽我看到的文檔或示例。

謝謝

這個問題同時在 Foxx 手冊端點文檔中有詳細介紹

查詢參數可以通過在 JOI 路由器定義中指定queryParam(...)來訪問,稍后在函數體中可以通過req.queryParams.yourQueryParam訪問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"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM