From d337f7d75907b183889c0ec21155c32df16119c5 Mon Sep 17 00:00:00 2001 From: noppoman Date: Tue, 21 Feb 2017 02:08:20 +0900 Subject: [PATCH] Add CollectionType --- Sources/SwiftKnex/Knex+Query.swift | 7 +++++++ Sources/SwiftKnex/Protocols/Collection.swift | 16 ++++++++++++++++ .../SwiftKnex/{Entity => Protocols}/Entity.swift | 0 3 files changed, 23 insertions(+) create mode 100644 Sources/SwiftKnex/Protocols/Collection.swift rename Sources/SwiftKnex/{Entity => Protocols}/Entity.swift (100%) diff --git a/Sources/SwiftKnex/Knex+Query.swift b/Sources/SwiftKnex/Knex+Query.swift index f9f6eac..7f0770b 100644 --- a/Sources/SwiftKnex/Knex+Query.swift +++ b/Sources/SwiftKnex/Knex+Query.swift @@ -8,6 +8,13 @@ extension Knex { + public func fetch(trx: Connection? = nil) throws -> T? { + guard let rows = try execute(.select, trx).asResultSet() else { + return nil + } + return try T(rows: rows) + } + public func fetch(trx: Connection? = nil) throws -> [T] { let rows = try execute(.select, trx).asResultSet() return try rows?.map { try T(row: $0) } ?? [] diff --git a/Sources/SwiftKnex/Protocols/Collection.swift b/Sources/SwiftKnex/Protocols/Collection.swift new file mode 100644 index 0000000..15d897c --- /dev/null +++ b/Sources/SwiftKnex/Protocols/Collection.swift @@ -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 +} + diff --git a/Sources/SwiftKnex/Entity/Entity.swift b/Sources/SwiftKnex/Protocols/Entity.swift similarity index 100% rename from Sources/SwiftKnex/Entity/Entity.swift rename to Sources/SwiftKnex/Protocols/Entity.swift