Vapor / Fluent:过滤嵌套请求
Vapor / Fluent: filtering nested requests
我有一个 Fluent 查询,如下所示:
var uQuery = User.query(on: req.db)
.filter(\.$id == user.id!)
.with(\.$playerTournaments) {[=10=].with(\.$tournamentGames)}
.all()
一个玩家锦标赛有多场比赛,其中包含比赛。在这种情况下,我不关心比赛,所以 $tournamentGames 允许我直接获得比赛的比赛。
但我想要的是通过@Parent 属性 $player 过滤 $tournamentGames 返回的游戏,以便只返回该玩家的游戏。我怎样才能做到这一点?我假设有一个语法
.with(\.$playerTournaments) {[=11=].with(\.$tournamentGames) { [=11=].filter(\.$id == user.id!}}
但我一直没弄明白...
编辑:对象之间的关系
User
定义多对多锦标赛:
through: TournamentPlayerPivot.self,
from: \.$player,
to: \.$tournament)
var playerTournaments: [Tournament]```
Tournament
与 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 中的关系,要么使用连接。联接将更加高效,因为它们只会 return 您想要的,并且数据库旨在尽可能高效地执行它们(至少 SQL 数据库)。
我有一个 Fluent 查询,如下所示:
var uQuery = User.query(on: req.db)
.filter(\.$id == user.id!)
.with(\.$playerTournaments) {[=10=].with(\.$tournamentGames)}
.all()
一个玩家锦标赛有多场比赛,其中包含比赛。在这种情况下,我不关心比赛,所以 $tournamentGames 允许我直接获得比赛的比赛。
但我想要的是通过@Parent 属性 $player 过滤 $tournamentGames 返回的游戏,以便只返回该玩家的游戏。我怎样才能做到这一点?我假设有一个语法
.with(\.$playerTournaments) {[=11=].with(\.$tournamentGames) { [=11=].filter(\.$id == user.id!}}
但我一直没弄明白...
编辑:对象之间的关系
User
定义多对多锦标赛:
through: TournamentPlayerPivot.self,
from: \.$player,
to: \.$tournament)
var playerTournaments: [Tournament]```
Tournament
与 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 中的关系,要么使用连接。联接将更加高效,因为它们只会 return 您想要的,并且数据库旨在尽可能高效地执行它们(至少 SQL 数据库)。