如何在Vapor 3中从JSON响应中保存父子关系

问题描述 投票:0回答:1

我正在使用Vapor 3开发REST API。此API使用另一个API来创建内容,以后将由应用程序使用。因此,我创建了一个从此API(联盟和季节)获取内容的函数,并将其存储在我的MySQL数据库中。 API的响应还包含我也想存储的嵌套对象(如果可能的话)都在同一请求中。这是API响应:

{
"data": [
         {
             "id": 271,
             "name": "Superliga",
             "current_season_id": 16020,
             "season": {
                 "data": {
                     "id": 16020,
                     "name": "2019/2020",
                     "league_id": 271,
                 }
             }
        }
    ]
}

这里是模型:

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)
    }
}

这里是执行请求并存储在数据库中的功能。

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)
            }
        })
    })
}

问题将是:是否可以保存父子关系?我是否必须使用解码/编码功能手动进行?我确实实现了编码/解码并创建了League,但是不知道如何创建Season,以及在进行league.create(orUpdate: true, on: connection)]时如何保存所有内容

将提供任何帮助。

我正在使用Vapor 3开发REST API。此API使用另一个API创建内容,以后将由应用程序使用。因此,我创建了一个从此API提取内容的函数(联盟和...

swift vapor vapor-fluent
1个回答
0
投票

正如我所见,您可以先解码APIModel,然后像这样将两个对象保存在扁平化循环中

© www.soinside.com 2019 - 2024. All rights reserved.