Reputation: 1044
I am developing a REST API using Vapor 3. This API uses another API to create content that will later be consumed by an app. So I created a function that fetches content from this API (Leagues and Seasons) and store them on my MySQL DB. The response from the API also contains nested objects that I would want to store too, if it possible all in the same request. Here is the API response:
{
"data": [
{
"id": 271,
"name": "Superliga",
"current_season_id": 16020,
"season": {
"data": {
"id": 16020,
"name": "2019/2020",
"league_id": 271,
}
}
}
]
}
Here are the model:
final class League: MySQLModel {
var id: League.ID?
var name: String
var current_season_id: Season.ID
var currentSeason: Parent<League, Season> {
return parent(\League.current_season_id)
}
}
final class Season: MySQLModel {
var id: Season.ID?
var name: String
var league_id: League.ID
var league: Parent<Season, League> {
return parent(\.league_id)
}
}
Here is the function performing the request and storing on the DB.
func getLeagues(using context: CommandContext) throws -> EventLoopFuture<Void> {
guard let url = URL(string: "SOME_API_URL") else { return .done(on: context.container) }
let client = try context.container.client()
return client.get(url).flatMap({ (response) -> EventLoopFuture<Void> in // do the request
let leagues = response.content.get([League].self, at: "data") // get the array of EventLoopFuture<[League]>
return context.container.requestPooledConnection(to: .mysql).flatMap({ (connection) -> EventLoopFuture<Void> in // connecto to DB
let savedLeagues = leagues.flatMap(to: [League].self, { (flattenLeagues) -> EventLoopFuture<[League]> in
return flattenLeagues.map { (league) -> EventLoopFuture<League> in
return league.create(orUpdate: true, on: connection) // save on the DB
}.flatten(on: context.container)
})
return savedLeagues.flatMap { (_) -> EventLoopFuture<Void> in
return .done(on: context.container)
}
})
})
}
The question would be: it is possible to save a Parent-Child relation? Do I have to do it manually using decode/encode functions?
I did implemented encode/decode and created the League, but don't know how to create te Season and how all can be saved when doing league.create(orUpdate: true, on: connection)
Any help would be appriciated.
Upvotes: 0
Views: 777
Reputation: 5656
As I see it you could decode an APIModel
first, then save two objects inside flatten loop like this
struct APILeague: Content {
let id: League.ID
let name: String
let current_season_id: Season.ID
struct _Season: Codable {
let data: Season
}
let season: _Season
}
final class League: MySQLModel {
var id: League.ID?
var name: String
var current_season_id: Season.ID
var currentSeason: Parent<League, Season> {
return parent(\League.current_season_id)
}
init (_ data: APILeague) {
self.id = data.id
self.name = data.name
self.current_season_id = data.current_season_id
}
}
func getLeagues(using context: CommandContext) throws -> Future<Void> {
guard let url = URL(string: "SOME_API_URL") else { return .done(on: context.container) }
let client = try context.container.client()
return client.get(url).flatMap { response in // do the request
return response.content.get([APILeague].self, at: "data").flatMap { leagues in
return context.container.requestPooledConnection(to: .mysql).flatMap { connection in // connecto to DB
let operations = leagues.map { league in
return League(league).create(orUpdate: true, on: connection).flatMap { _ in
return league.season.data.create(orUpdate: true, on: connection).transform(to: ()) // transforming to Void
}
}
return operations.flatten(on: context.container)flatMap {
return .done(on: context.container)
}
}
}
}
}
Upvotes: 1