目标是开发一个foxxservice
,它仅在json
中提供可用的用户集合。
我已经尝试过的方法:设置本地arangodb
服务器。然后阅读文档并使用arangosh
尝试命令。然后扩展foxxservice
你好世界示例以获取相同的输出。
arangosh
127.0.0.1:8529@TestDatabase> db._collections()
[
[ArangoCollection 2393976, "_appbundles" (type document, status loaded)],
[ArangoCollection 2393973, "_apps" (type document, status loaded)],
[ArangoCollection 2393961, "_aqlfunctions" (type document, status unloaded)],
[ArangoCollection 2394097, "_fishbowl" (type document, status loaded)],
[ArangoCollection 2393964, "_frontend" (type document, status loaded)],
[ArangoCollection 2393943, "_graphs" (type document, status loaded)],
[ArangoCollection 2393970, "_jobs" (type document, status loaded)],
[ArangoCollection 2393948, "_modules" (type document, status loaded)],
[ArangoCollection 2393967, "_queues" (type document, status loaded)],
[ArangoCollection 2393951, "_routing" (type document, status loaded)],
[ArangoCollection 2394131, "Relation" (type edge, status loaded)],
[ArangoCollection 2394114, "Table" (type document, status loaded)]
]
foxx服务:
'use strict';
const createRouter = require('@arangodb/foxx/router');
const db = require('@arangodb').db;
const router = createRouter();
module.context.use(router);
router.get('/hello-world', function (req, res) {
res.send('Hello World!');
const collections = db._collections();
res.json(collections);
})
.response(['application/json'], 'A generic greeting.')
.summary('Generic greeting')
.description('Prints a generic greeting.');
实际上,我期望使用相同的json。希望出于安全原因并非不可能。
但这是浏览器显示的内容:
[
{"_id":"2393976","_dbName":"TestDatabase"},
{"_id":"2393973","_dbName":"TestDatabase"},
{"_id":"2393961","_dbName":"TestDatabase"},
{"_id":"2394097","_dbName":"TestDatabase"},
{"_id":"2393964","_dbName":"TestDatabase"},
{"_id":"2393943","_dbName":"TestDatabase"},
{"_id":"2393970","_dbName":"TestDatabase"},
{"_id":"2393948","_dbName":"TestDatabase"},
{"_id":"2393967","_dbName":"TestDatabase"},
{"_id":"2393951","_dbName":"TestDatabase"},
{"_id":"2394131","_dbName":"TestDatabase"},
{"_id":"2394114","_dbName":"TestDatabase"}
]
答案 0 :(得分:0)
可能的解决方案:
private val postsDataFactory = object: DataSource.Factory<String, Post>() {
override fun create(): DataSource<String, Post> {
return ItemDataSource(application)
}
}
private val postsBoundaryCallback = object: PagedList.BoundaryCallback<Post>(){
override fun onZeroItemsLoaded() {
super.onZeroItemsLoaded()
viewModelScope.launch {
try {
val posts = postsRepo.findPosts(mutableMapOf("limit" to "1"))
database.postDao().create(posts)
} catch (e: IOException) {
Log.d("NETWORK_ERROR", e.message)
} catch (e: Exception) {
Log.d("UNCAUGHT_ERROR", e.message)
}
}
}
override fun onItemAtEndLoaded(itemAtEnd: Post) {
viewModelScope.launch {
try {
val posts = postsRepo.findPosts(mutableMapOf(
"before" to itemAtEnd.id,
"limit" to "1"
))
database.postDao().create(posts)
} catch (e: IOException) {
Log.d("NETWORK_ERROR", e.message)
} catch (e: Exception) {
Log.d("UNCAUGHT_ERROR", e.message)
}
}
}
}
private val postPageConfig = PagedList
.Config
.Builder()
.setPageSize(10)
.setInitialLoadSizeHint(10)
.build()
val postsLiveData = LivePagedListBuilder(
postsDataFactory,
postPageConfig
)
.setBoundaryCallback(postsBoundaryCallback)
.build()
生成的输出:
'use strict';
const createRouter = require('@arangodb/foxx/router');
const db = require('@arangodb').db;
const router = createRouter();
module.context.use(router);
router.get('/hello-world', function (req, res) {
res.send('Hello World!');
var collections = [];
db._collections().forEach(function(element) {
if(!element.name().startsWith("_")) {
var obj = {};
obj[element._id] = element.name();
collections.push(obj);
}
});
res.json(collections);
})
.response(['application/json'], 'A generic greeting.')
.summary('Generic greeting')
.description('Prints a generic greeting.');