Vapor/Future newbie here, who's fighting with the type system.
I'd like to do some processing on query results:
For example:
func myIndex(_ req: Request) throws -> Future<[Todo]> {
return Todo.query(on: req)
.all()
.flatMap { allToDos in
allToDos.flatMap { toDo in
// change toDoch
return toDo.save(on: req).flatMap { $0 }
}
}
}
So what's the correct pattern for this kind of thing?
Use flatten
func changeAllTodos(_ req: Request) throws -> Future<HTTPStatus> {
return Todo.query(on: req).all().flatMap { allToDos in
return allToDos.map { toDo in
// change toDoch
return toDo.save(on: req).transform(to: ())
}.flatten(on: req).transform(to: .ok)
}
}