gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:00:42 28 4
gpt4 key购买 nike

(对于新手问题很抱歉,但在文档中无法轻松找到)

我想要一个包含几个模型的文档存储,然后在我的 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,我对潜力非常感兴趣,但我无法浏览我看到的文档或示例。

谢谢

最佳答案

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

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

请注意,您可以使用 API - 在 Web 界面中使用 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"}

关于ArangoDB 和 Foxx - 来自 GET 的多个查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46350234/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com