Skip to content

Instantly share code, notes, and snippets.

@Cellane
Last active August 25, 2017 18:04
Show Gist options
  • Save Cellane/7e54fd450cfaed7f7acb8737bb7482b2 to your computer and use it in GitHub Desktop.
Save Cellane/7e54fd450cfaed7f7acb8737bb7482b2 to your computer and use it in GitHub Desktop.
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))
}
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)
}
}
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