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=系列&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 Manual and in detail in the endpoints documentation.
可以通过在 JOI 路由器定义中指定 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"}
在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"}
(抱歉新手问题,但在文档中不容易找到)
我想要一个包含几个模型的文档存储,然后在我的 Foxx 服务中使用一些属性作为查询中的参数。 假设我有一个电影和系列剧集的数据库:
{
'type':'movie',
'year':'1997',
'director':'xxxxxxx',
...
},
{
'type':'series_episode',
'season':'1',
'episode':'3',
...
}
...
我需要能够在
上搜索当然,我想做的是拥有一个 单一路由器 来支持两者 GET /?type=movie&year=x&director=y.. GET /?type=系列&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 Manual and in detail in the endpoints documentation.
可以通过在 JOI 路由器定义中指定 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"}
在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"}