Skip to content

Commit

Permalink
Merge pull request #42 from noppoMan/add-collection-type
Browse files Browse the repository at this point in the history
Add CollectionType
  • Loading branch information
noppoMan authored Feb 20, 2017
2 parents 762bd97 + d337f7d commit 758026a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/SwiftKnex/Knex+Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

extension Knex {

public func fetch<T: CollectionType>(trx: Connection? = nil) throws -> T? {
guard let rows = try execute(.select, trx).asResultSet() else {
return nil
}
return try T(rows: rows)
}

public func fetch<T: Entity>(trx: Connection? = nil) throws -> [T] {
let rows = try execute(.select, trx).asResultSet()
return try rows?.map { try T(row: $0) } ?? []
Expand Down
16 changes: 16 additions & 0 deletions Sources/SwiftKnex/Protocols/Collection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Collection.swift
// SwiftJNChatApp
//
// Created by Yuki Takei on 2017/02/20.
//
//

public protocol CollectionSerializable {
func serialize() throws -> [[String: Any]]
}

public protocol CollectionType {
init(rows: ResultSet) throws
}

File renamed without changes.

0 comments on commit 758026a

Please sign in to comment.