Skip to content

Commit

Permalink
Merge pull request #621 from vapor/querybuilder-copy
Browse files Browse the repository at this point in the history
Add support for copying query builders.
  • Loading branch information
tanner0101 authored Apr 10, 2019
2 parents 6c4e5bc + 3430a15 commit b915c32
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Sources/Fluent/QueryBuilder/QueryBuilder+Copy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extension QueryBuilder {
// MARK: Copy

/// Creates a copy of the current query builder. Useful for constructing two similar, but not identical, queries.
///
/// Example: two queries sorted by different criteria.
///
/// let baseQuery = try User.query(on: conn).filter(\.name == "foo")
/// let sortedByID = baseQuery.copy().sort(\.id, .ascending)
/// let sortedByName = baseQuery.copy().sort(\.name, .ascending)
///
/// - returns: A copy of the current query builder that can be manipulated independently of the original.
public func copy() -> Self {
return .init(query: self.query, on: self.connection, resultTransformer: self.resultTransformer)
}
}

0 comments on commit b915c32

Please sign in to comment.