Skip to content

Commit

Permalink
Add CollectionType
Browse files Browse the repository at this point in the history
  • Loading branch information
noppoMan committed Feb 20, 2017
1 parent 762bd97 commit d337f7d
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 d337f7d

Please sign in to comment.