Last active
August 25, 2017 18:04
-
-
Save Cellane/7e54fd450cfaed7f7acb8737bb7482b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func joinQuery<T: Entity & SoftDeletable, U: Entity>(for entity: T.Type, joined: U.Type) throws -> Query<T> { | |
return try entity | |
.makeQuery() | |
.withSoftDeleted() | |
.join(Pivot<U, T>.self) | |
.join(Join(kind: .inner, base: Pivot<U, T>.self, joined: U.self, baseKey: Pivot<U, T>.leftIdKey, joinedKey: U.idKey)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension QueryRepresentable where Self: ExecutorRepresentable { | |
public func join<Joined: Entity, Over: Entity>( | |
kind: Join.Kind = .inner, | |
_ joined: Joined.Type, | |
over: Over.Type, | |
baseKey: String = E.idKey, | |
leftPivotKey: String = E.foreignIdKey, | |
rightPivotKey: String = Joined.foreignIdKey, | |
joinedKey: String = Joined.idKey | |
) throws -> Query<Self.E> { | |
let leftJoin = Join( | |
kind: kind, | |
base: E.self, | |
joined: Pivot<Joined, E>.self, | |
baseKey: baseKey, | |
joinedKey: leftPivotKey | |
) | |
let rightJoin = Join( | |
kind: kind, | |
base: Pivot<Joined, E>.self, | |
joined: Joined.self, | |
baseKey: rightPivotKey, | |
joinedKey: joinedKey | |
) | |
return try self.join(leftJoin).join(rightJoin) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let textArticles = try TextArticle | |
.makeQuery() | |
.join(Environment.self, over: Pivot<TextArticle, Environment>.self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment