如何查询更多记录并使用Vapor进行处理?
How to query more records and process them with Vapor?
路由器在 JSON 中收到更多 ID。需要为每个 Id 执行一个查询。然后对原始数据和 return 进行一些操作。但是我得到了一些错误。以及如何管理Futures
?每个查询都会return一个独特的未来?我需要检查所有 Futures
何时完成?我是否需要使用 DispathGroup,或者如何使用?
我是这样制作的:
router.post([Page].self, at: "/releases") { (request, pages) -> [Page] in
for page in pages {
for event in page.events {
let release = Release.query(on: request).filter(\.fbId == event.id).first().flatMap { (release) -> EventLoopFuture<Release?> in
return request.future(release)
}
if let release2 = release {
event.started = release2.started
}
}
}
return pages
}
但它引发了一些错误:
使用了以下 JSON:
[
{
"id": "107120697426006",
"name": "Teszt Színház",
"events": [
{
"id": "802128100247740",
"name": "Teszt Esemény"
},
{
"id": "1378145475685730",
"name": "Teszt Esemény 2"
}
]
}
]
类似的东西应该可以工作
router.post([Page].self, at: "/releases") { (req, pages) -> Future<[Page]> in
var result: [Page] = []
return pages.map { p in
var page = p
var pageEvents: [Event] = []
return page.events.map { e in
var event = e
return Release.query(on: req).filter(\.fbId == event.id).first().map { release in
if let release = release {
event.started = release.started
}
pageEvents.append(event)
}
}.flatten(on: req).map {
page.events = pageEvents
result.append(page)
}
}.flatten(on: req).transform(to: result)
}
或者调用一次数据库会更好
import FluentSQL // for `~~` operator
router.post([Page].self, at: "/releases") { (req, pages) -> Future<[Page]> in
let eventIds = pages.map { [=11=].events }.flatMap { [=11=] }.map { [=11=].id }
return Release.query(on: req).filter(\.fbId ~~ eventIds).all().map { releases in
var result: [Page] = []
for p in pages {
var page = p
var pageEvents: [Event] = []
for e in p.events {
var event = e
if let release = releases.first(where: { [=11=].fbId == e.id }) {
event.started = release.started
}
pageEvents.append(event)
}
page.events = pageEvents
result.append(page)
}
return result
}
}
路由器在 JSON 中收到更多 ID。需要为每个 Id 执行一个查询。然后对原始数据和 return 进行一些操作。但是我得到了一些错误。以及如何管理Futures
?每个查询都会return一个独特的未来?我需要检查所有 Futures
何时完成?我是否需要使用 DispathGroup,或者如何使用?
我是这样制作的:
router.post([Page].self, at: "/releases") { (request, pages) -> [Page] in
for page in pages {
for event in page.events {
let release = Release.query(on: request).filter(\.fbId == event.id).first().flatMap { (release) -> EventLoopFuture<Release?> in
return request.future(release)
}
if let release2 = release {
event.started = release2.started
}
}
}
return pages
}
但它引发了一些错误:
使用了以下 JSON:
[
{
"id": "107120697426006",
"name": "Teszt Színház",
"events": [
{
"id": "802128100247740",
"name": "Teszt Esemény"
},
{
"id": "1378145475685730",
"name": "Teszt Esemény 2"
}
]
}
]
类似的东西应该可以工作
router.post([Page].self, at: "/releases") { (req, pages) -> Future<[Page]> in
var result: [Page] = []
return pages.map { p in
var page = p
var pageEvents: [Event] = []
return page.events.map { e in
var event = e
return Release.query(on: req).filter(\.fbId == event.id).first().map { release in
if let release = release {
event.started = release.started
}
pageEvents.append(event)
}
}.flatten(on: req).map {
page.events = pageEvents
result.append(page)
}
}.flatten(on: req).transform(to: result)
}
或者调用一次数据库会更好
import FluentSQL // for `~~` operator
router.post([Page].self, at: "/releases") { (req, pages) -> Future<[Page]> in
let eventIds = pages.map { [=11=].events }.flatMap { [=11=] }.map { [=11=].id }
return Release.query(on: req).filter(\.fbId ~~ eventIds).all().map { releases in
var result: [Page] = []
for p in pages {
var page = p
var pageEvents: [Event] = []
for e in p.events {
var event = e
if let release = releases.first(where: { [=11=].fbId == e.id }) {
event.started = release.started
}
pageEvents.append(event)
}
page.events = pageEvents
result.append(page)
}
return result
}
}