簡體   English   中英

Vapor / Fluent:過濾嵌套請求

[英]Vapor / Fluent: filtering nested requests

我有一個 Fluent 查詢,如下所示:

            var uQuery = User.query(on: req.db)
                .filter(\.$id == user.id!)
                .with(\.$playerTournaments) {$0.with(\.$tournamentGames)}
                .all()

玩家錦標賽有多個比賽,其中包含比賽。 在這種情況下,我不關心比賽,所以 $tournamentGames 允許我直接獲得比賽的比賽。

但我想要的是通過@Parent 屬性 $player 過濾 $tournamentGames 返回的游戲,以便只返回該玩家的游戲。 我怎樣才能做到這一點? 我假設有一個語法

.with(\.$playerTournaments) {$0.with(\.$tournamentGames) { $0.filter(\.$id == user.id!}}

但我還沒弄明白……

編輯:對象之間的關系

User使用錦標賽定義多對多:

      through: TournamentPlayerPivot.self,
      from: \.$player,
      to: \.$tournament)
    var playerTournaments: [Tournament]```

Tournament與 Game 之間存在互惠關系,但與Game沒有關系:

      through: TournamentGamePivot.self,
      from: \.$tournament,
      to: \.$game)
    var tournamentGames: [Game]```

 Tournament is @Parent to ```Match```, which in turn parents ```Game```. Match manages a bunch of non-user-facing stuff to do with determining when tournaments end, but are in the way here.

A ```Game``` has a $player field, which contains the uuid of the User assigned to play it. And a Tournament has an $owner, guaranteed to to be a player/User. Both are one to one.

More questions, happy to answer.

您有兩個選擇 - 您要么急切加載所有數據並使用數組過濾器過濾 Swift 中的關系,要么使用連接。 聯接將更加高效,因為它們只會返回您想要的內容,並且數據庫旨在盡可能高效地執行它們(至少 SQL 個數據庫)。

加入的文檔是https://docs.vapor.codes/4.0/fluent/query/#join

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM