From 1f294b28a264a365a2cc677f6e81779df5b340df Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Mon, 20 May 2019 13:50:56 +0100 Subject: [PATCH 01/10] First commit for date encoding property --- Sources/SwiftKueryORM/DatabaseDecoder.swift | 32 ++++++++++++++++++--- Sources/SwiftKueryORM/DatabaseEncoder.swift | 22 ++++++++++++-- Sources/SwiftKueryORM/Model.swift | 26 ++++++++++++----- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/Sources/SwiftKueryORM/DatabaseDecoder.swift b/Sources/SwiftKueryORM/DatabaseDecoder.swift index 0835065..30dfae0 100644 --- a/Sources/SwiftKueryORM/DatabaseDecoder.swift +++ b/Sources/SwiftKueryORM/DatabaseDecoder.swift @@ -23,7 +23,8 @@ open class DatabaseDecoder { fileprivate let decoder = _DatabaseDecoder() /// Decode from a dictionary [String: Any] to a Decodable type - open func decode(_ type: T.Type, _ values: [String : Any?]) throws -> T { + open func decode(_ type: T.Type, _ values: [String : Any?], dateEncodingStrategy: DateEncodingFormat) throws -> T { + decoder.dateEncodingStrategy = dateEncodingStrategy decoder.values = values return try T(from: decoder) } @@ -33,6 +34,8 @@ open class DatabaseDecoder { public var userInfo: [CodingUserInfoKey:Any] = [:] public var values = [String:Any?]() + public var dateEncodingStrategy: DateEncodingFormat = .double + fileprivate init(at codingPath: [CodingKey] = []){ self.codingPath = codingPath } @@ -192,9 +195,30 @@ open class DatabaseDecoder { let uuid = UUID(uuidString: castValue) return try castedValue(uuid, type, key) } else if type is Date.Type && value != nil { - let castValue = try castedValue(value, Double.self, key) - let date = Date(timeIntervalSinceReferenceDate: castValue) - return try castedValue(date, type, key) + switch decoder.dateEncodingStrategy { + case .double: + let castValue = try castedValue(value, Double.self, key) + let date = Date(timeIntervalSinceReferenceDate: castValue) + return try castedValue(date, type, key) + case .datetime, .timestamp: + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" + let castValue = try castedValue(value, String.self, key) + let date = dateFormatter.date(from: castValue) + return try castedValue(date, type, key) + case .date: + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd" + let castValue = try castedValue(value, String.self, key) + let date = dateFormatter.date(from: castValue) + return try castedValue(date, type, key) + case .time: + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "HH:mm:ss" + let castValue = try castedValue(value, String.self, key) + let date = dateFormatter.date(from: castValue) + return try castedValue(date, type, key) + } } else { throw RequestError(.ormDatabaseDecodingError, reason: "Unsupported type: \(String(describing: type)) for value: \(String(describing: value))") } diff --git a/Sources/SwiftKueryORM/DatabaseEncoder.swift b/Sources/SwiftKueryORM/DatabaseEncoder.swift index cc3d540..8359654 100644 --- a/Sources/SwiftKueryORM/DatabaseEncoder.swift +++ b/Sources/SwiftKueryORM/DatabaseEncoder.swift @@ -23,7 +23,8 @@ open class DatabaseEncoder { private var databaseEncoder = _DatabaseEncoder() /// Encode a Encodable type to a dictionary [String: Any] - open func encode(_ value: T) throws -> [String: Any] { + open func encode(_ value: T, dateEncodingStrategy: DateEncodingFormat) throws -> [String: Any] { + databaseEncoder.dateEncodingStrategy = dateEncodingStrategy try value.encode(to: databaseEncoder) return databaseEncoder.values } @@ -34,6 +35,8 @@ fileprivate class _DatabaseEncoder: Encoder { public var values: [String: Any] = [:] + public var dateEncodingStrategy: DateEncodingFormat = .double + public var userInfo: [CodingUserInfoKey: Any] = [:] public func container(keyedBy: Key.Type) -> KeyedEncodingContainer { let container = _DatabaseKeyedEncodingContainer(encoder: self, codingPath: codingPath) @@ -65,7 +68,22 @@ fileprivate struct _DatabaseKeyedEncodingContainer : KeyedEncoding } else if let uuidValue = value as? UUID { encoder.values[key.stringValue] = uuidValue.uuidString } else if let dateValue = value as? Date { - encoder.values[key.stringValue] = dateValue.timeIntervalSinceReferenceDate + switch encoder.dateEncodingStrategy { + case .double: + encoder.values[key.stringValue] = dateValue.timeIntervalSinceReferenceDate + case .datetime, .timestamp: + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" + encoder.values[key.stringValue] = dateFormatter.string(from: dateValue) + case .date: + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd" + encoder.values[key.stringValue] = dateFormatter.string(from: dateValue) + case .time: + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "HH:mm:ss" + encoder.values[key.stringValue] = dateFormatter.string(from: dateValue) + } } else if value is [Any] { throw RequestError(.ormDatabaseEncodingError, reason: "Encoding an array is not currently supported") } else if value is [AnyHashable: Any] { diff --git a/Sources/SwiftKueryORM/Model.swift b/Sources/SwiftKueryORM/Model.swift index 2ef0fb5..3566dcc 100644 --- a/Sources/SwiftKueryORM/Model.swift +++ b/Sources/SwiftKueryORM/Model.swift @@ -19,6 +19,13 @@ import KituraContracts import Foundation import Dispatch +public enum DateEncodingFormat { + case time + case date + case timestamp, datetime + case double +} + /// Protocol Model conforming to Codable defining the available operations public protocol Model: Codable { /// Defines the tableName in the Database @@ -34,6 +41,9 @@ public protocol Model: Codable { /// Defines the keypath to the Models id field static var idKeypath: IDKeyPath {get} + /// Defines the date encoding strategy for the Model. Defaults to .double + static var dateEncodingStrategy: DateEncodingFormat { get } + /// Call to create the table in the database synchronously static func createTableSync(using db: Database?) throws -> Bool @@ -120,6 +130,8 @@ public extension Model { static var idKeypath: IDKeyPath { return nil } + static var dateEncodingStrategy: DateEncodingFormat { return .double } + private static func executeTask(using db: Database? = nil, task: @escaping ((Connection?, QueryError?) -> ())) { guard let database = db ?? Database.default else { @@ -233,7 +245,7 @@ public extension Model { var values: [String : Any] do { table = try Self.getTable() - values = try DatabaseEncoder().encode(self) + values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingStrategy) } catch let error { onCompletion(nil, Self.convertError(error)) return @@ -252,7 +264,7 @@ public extension Model { var values: [String : Any] do { table = try Self.getTable() - values = try DatabaseEncoder().encode(self) + values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingStrategy) } catch let error { onCompletion(nil, nil, Self.convertError(error)) return @@ -270,7 +282,7 @@ public extension Model { var values: [String: Any] do { table = try Self.getTable() - values = try DatabaseEncoder().encode(self) + values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingStrategy) } catch let error { onCompletion(nil, Self.convertError(error)) return @@ -500,7 +512,7 @@ public extension Model { var decodedModel: Self do { - decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue) + decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue, dateEncodingStrategy: Self.dateEncodingStrategy) } catch { onCompletion(nil, Self.convertError(error)) return @@ -563,7 +575,7 @@ public extension Model { var decodedModel: Self do { - decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue) + decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue, dateEncodingStrategy: Self.dateEncodingStrategy) } catch { onCompletion(nil, nil, Self.convertError(error)) return @@ -619,7 +631,7 @@ public extension Model { for dictionary in dictionariesTitleToValue { var decodedModel: Self do { - decodedModel = try DatabaseDecoder().decode(Self.self, dictionary) + decodedModel = try DatabaseDecoder().decode(Self.self, dictionary, dateEncodingStrategy: Self.dateEncodingStrategy) } catch { onCompletion(nil, Self.convertError(error)) return @@ -684,7 +696,7 @@ public extension Model { for dictionary in dictionariesTitleToValue { var decodedModel: Self do { - decodedModel = try DatabaseDecoder().decode(Self.self, dictionary) + decodedModel = try DatabaseDecoder().decode(Self.self, dictionary, dateEncodingStrategy: Self.dateEncodingStrategy) } catch let error { onCompletion(nil, Self.convertError(error)) return From e87200ea736be8b5803b1ddb5a0a8539aa12d04b Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Tue, 21 May 2019 10:43:56 +0100 Subject: [PATCH 02/10] Handle Date and String formats from plugin --- Sources/SwiftKueryORM/DatabaseDecoder.swift | 42 +++++++++++++-------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/Sources/SwiftKueryORM/DatabaseDecoder.swift b/Sources/SwiftKueryORM/DatabaseDecoder.swift index 30dfae0..73c44b7 100644 --- a/Sources/SwiftKueryORM/DatabaseDecoder.swift +++ b/Sources/SwiftKueryORM/DatabaseDecoder.swift @@ -201,23 +201,35 @@ open class DatabaseDecoder { let date = Date(timeIntervalSinceReferenceDate: castValue) return try castedValue(date, type, key) case .datetime, .timestamp: - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" - let castValue = try castedValue(value, String.self, key) - let date = dateFormatter.date(from: castValue) - return try castedValue(date, type, key) + if let dateValue = value as? Date { + return try castedValue(dateValue, type.self, key) + } else { + let castValue = try castedValue(value, String.self, key) + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" + let date = dateFormatter.date(from: castValue) + return try castedValue(date, type.self, key) + } case .date: - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "yyyy-MM-dd" - let castValue = try castedValue(value, String.self, key) - let date = dateFormatter.date(from: castValue) - return try castedValue(date, type, key) + if let dateValue = value as? Date { + return try castedValue(dateValue, type.self, key) + } else { + let castValue = try castedValue(value, String.self, key) + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd" + let date = dateFormatter.date(from: castValue) + return try castedValue(date, type.self, key) + } case .time: - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "HH:mm:ss" - let castValue = try castedValue(value, String.self, key) - let date = dateFormatter.date(from: castValue) - return try castedValue(date, type, key) + if let dateValue = value as? Date { + return try castedValue(dateValue, type.self, key) + } else { + let castValue = try castedValue(value, String.self, key) + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "HH:mm:ss" + let date = dateFormatter.date(from: castValue) + return try castedValue(date, type.self, key) + } } } else { throw RequestError(.ormDatabaseDecodingError, reason: "Unsupported type: \(String(describing: type)) for value: \(String(describing: value))") From 9a94bf8983491d9cd1544d1e7e2084c9e262a4d8 Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Tue, 21 May 2019 11:39:41 +0100 Subject: [PATCH 03/10] Use date encoding startegy when creating table from model --- Sources/SwiftKueryORM/DatabaseDecoder.swift | 2 +- Sources/SwiftKueryORM/DatabaseEncoder.swift | 2 +- Sources/SwiftKueryORM/Model.swift | 4 ++-- Sources/SwiftKueryORM/TableInfo.swift | 21 ++++++++++++++++----- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Sources/SwiftKueryORM/DatabaseDecoder.swift b/Sources/SwiftKueryORM/DatabaseDecoder.swift index 73c44b7..b5c0464 100644 --- a/Sources/SwiftKueryORM/DatabaseDecoder.swift +++ b/Sources/SwiftKueryORM/DatabaseDecoder.swift @@ -200,7 +200,7 @@ open class DatabaseDecoder { let castValue = try castedValue(value, Double.self, key) let date = Date(timeIntervalSinceReferenceDate: castValue) return try castedValue(date, type, key) - case .datetime, .timestamp: + case .timestamp: if let dateValue = value as? Date { return try castedValue(dateValue, type.self, key) } else { diff --git a/Sources/SwiftKueryORM/DatabaseEncoder.swift b/Sources/SwiftKueryORM/DatabaseEncoder.swift index 8359654..765f7d3 100644 --- a/Sources/SwiftKueryORM/DatabaseEncoder.swift +++ b/Sources/SwiftKueryORM/DatabaseEncoder.swift @@ -71,7 +71,7 @@ fileprivate struct _DatabaseKeyedEncodingContainer : KeyedEncoding switch encoder.dateEncodingStrategy { case .double: encoder.values[key.stringValue] = dateValue.timeIntervalSinceReferenceDate - case .datetime, .timestamp: + case .timestamp: let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" encoder.values[key.stringValue] = dateFormatter.string(from: dateValue) diff --git a/Sources/SwiftKueryORM/Model.swift b/Sources/SwiftKueryORM/Model.swift index 3566dcc..a4e7d5f 100644 --- a/Sources/SwiftKueryORM/Model.swift +++ b/Sources/SwiftKueryORM/Model.swift @@ -22,7 +22,7 @@ import Dispatch public enum DateEncodingFormat { case time case date - case timestamp, datetime + case timestamp case double } @@ -766,7 +766,7 @@ public extension Model { static func getTable() throws -> Table { let idKeyPathSet: Bool = Self.idKeypath != nil - return try Database.tableInfo.getTable((Self.idColumnName, Self.idColumnType, idKeyPathSet), Self.tableName, for: Self.self) + return try Database.tableInfo.getTable((Self.idColumnName, Self.idColumnType, idKeyPathSet), Self.tableName, for: Self.self, with: Self.dateEncodingStrategy) } /** diff --git a/Sources/SwiftKueryORM/TableInfo.swift b/Sources/SwiftKueryORM/TableInfo.swift index 8385d78..7d89da2 100644 --- a/Sources/SwiftKueryORM/TableInfo.swift +++ b/Sources/SwiftKueryORM/TableInfo.swift @@ -29,11 +29,11 @@ public class TableInfo { private var codableMapQueue = DispatchQueue(label: "codableMap.queue", attributes: .concurrent) /// Get the table for a model - func getTable(_ idColumn: (name: String, type: SQLDataType.Type, idKeyPathSet: Bool), _ tableName: String, for type: T.Type) throws -> Table { - return try getInfo(idColumn, tableName, type).table + func getTable(_ idColumn: (name: String, type: SQLDataType.Type, idKeyPathSet: Bool), _ tableName: String, for type: T.Type, with dateEncodingFormat: DateEncodingFormat) throws -> Table { + return try getInfo(idColumn, tableName, type, dateEncodingFormat).table } - func getInfo(_ idColumn: (name: String, type: SQLDataType.Type, idKeyPathSet: Bool), _ tableName: String, _ type: T.Type) throws -> (info: TypeInfo, table: Table) { + func getInfo(_ idColumn: (name: String, type: SQLDataType.Type, idKeyPathSet: Bool), _ tableName: String, _ type: T.Type, _ dateEncodingFormat: DateEncodingFormat) throws -> (info: TypeInfo, table: Table) { let typeString = "\(type)" var result: (TypeInfo, Table)? = nil // Read from codableMap when no concurrent write is occurring @@ -46,7 +46,7 @@ public class TableInfo { try codableMapQueue.sync(flags: .barrier) { let typeInfo = try TypeDecoder.decode(type) - result = (info: typeInfo, table: try constructTable(idColumn, tableName, typeInfo)) + result = (info: typeInfo, table: try constructTable(idColumn, tableName, typeInfo, dateEncodingFormat)) codableMap[typeString] = result } @@ -57,7 +57,7 @@ public class TableInfo { } /// Construct the table for a Model - func constructTable(_ idColumn: (name: String, type: SQLDataType.Type, idKeyPathSet: Bool), _ tableName: String, _ typeInfo: TypeInfo) throws -> Table { + func constructTable(_ idColumn: (name: String, type: SQLDataType.Type, idKeyPathSet: Bool), _ tableName: String, _ typeInfo: TypeInfo, _ dateEncodingFormat: DateEncodingFormat) throws -> Table { var columns: [Column] = [] var idColumnIsSet = false switch typeInfo { @@ -73,6 +73,17 @@ public class TableInfo { switch keyedTypeInfo { case .single(_ as UUID.Type, _): valueType = UUID.self + case .single(_ as Date.Type, _): + switch dateEncodingFormat { + case .double: + valueType = Double.self + case .timestamp: + valueType = Timestamp.self + case .date: + valueType = SQLDate.self + case .time: + valueType = Time.self + } case .single(_, let singleType): valueType = singleType if valueType is Int.Type { From 0c36a9b26c8a6bf0a1397b66ff42b41b6640edcb Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Tue, 21 May 2019 12:52:20 +0100 Subject: [PATCH 04/10] Update name for date encoding property --- Sources/SwiftKueryORM/Model.swift | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/SwiftKueryORM/Model.swift b/Sources/SwiftKueryORM/Model.swift index a4e7d5f..9352634 100644 --- a/Sources/SwiftKueryORM/Model.swift +++ b/Sources/SwiftKueryORM/Model.swift @@ -42,7 +42,7 @@ public protocol Model: Codable { static var idKeypath: IDKeyPath {get} /// Defines the date encoding strategy for the Model. Defaults to .double - static var dateEncodingStrategy: DateEncodingFormat { get } + static var dateEncodingFormat: DateEncodingFormat { get } /// Call to create the table in the database synchronously static func createTableSync(using db: Database?) throws -> Bool @@ -130,7 +130,7 @@ public extension Model { static var idKeypath: IDKeyPath { return nil } - static var dateEncodingStrategy: DateEncodingFormat { return .double } + static var dateEncodingFormat: DateEncodingFormat { return .double } private static func executeTask(using db: Database? = nil, task: @escaping ((Connection?, QueryError?) -> ())) { guard let database = db ?? Database.default else { @@ -245,7 +245,7 @@ public extension Model { var values: [String : Any] do { table = try Self.getTable() - values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingStrategy) + values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingFormat) } catch let error { onCompletion(nil, Self.convertError(error)) return @@ -264,7 +264,7 @@ public extension Model { var values: [String : Any] do { table = try Self.getTable() - values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingStrategy) + values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingFormat) } catch let error { onCompletion(nil, nil, Self.convertError(error)) return @@ -282,7 +282,7 @@ public extension Model { var values: [String: Any] do { table = try Self.getTable() - values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingStrategy) + values = try DatabaseEncoder().encode(self, dateEncodingStrategy: Self.dateEncodingFormat) } catch let error { onCompletion(nil, Self.convertError(error)) return @@ -512,7 +512,7 @@ public extension Model { var decodedModel: Self do { - decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue, dateEncodingStrategy: Self.dateEncodingStrategy) + decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue, dateEncodingStrategy: Self.dateEncodingFormat) } catch { onCompletion(nil, Self.convertError(error)) return @@ -575,7 +575,7 @@ public extension Model { var decodedModel: Self do { - decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue, dateEncodingStrategy: Self.dateEncodingStrategy) + decodedModel = try DatabaseDecoder().decode(Self.self, dictionaryTitleToValue, dateEncodingStrategy: Self.dateEncodingFormat) } catch { onCompletion(nil, nil, Self.convertError(error)) return @@ -631,7 +631,7 @@ public extension Model { for dictionary in dictionariesTitleToValue { var decodedModel: Self do { - decodedModel = try DatabaseDecoder().decode(Self.self, dictionary, dateEncodingStrategy: Self.dateEncodingStrategy) + decodedModel = try DatabaseDecoder().decode(Self.self, dictionary, dateEncodingStrategy: Self.dateEncodingFormat) } catch { onCompletion(nil, Self.convertError(error)) return @@ -696,7 +696,7 @@ public extension Model { for dictionary in dictionariesTitleToValue { var decodedModel: Self do { - decodedModel = try DatabaseDecoder().decode(Self.self, dictionary, dateEncodingStrategy: Self.dateEncodingStrategy) + decodedModel = try DatabaseDecoder().decode(Self.self, dictionary, dateEncodingStrategy: Self.dateEncodingFormat) } catch let error { onCompletion(nil, Self.convertError(error)) return @@ -766,7 +766,7 @@ public extension Model { static func getTable() throws -> Table { let idKeyPathSet: Bool = Self.idKeypath != nil - return try Database.tableInfo.getTable((Self.idColumnName, Self.idColumnType, idKeyPathSet), Self.tableName, for: Self.self, with: Self.dateEncodingStrategy) + return try Database.tableInfo.getTable((Self.idColumnName, Self.idColumnType, idKeyPathSet), Self.tableName, for: Self.self, with: Self.dateEncodingFormat) } /** From f8273885796a195b35b1dbdc9f9d0e5fab67df70 Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Tue, 21 May 2019 12:58:11 +0100 Subject: [PATCH 05/10] Add comments --- Sources/SwiftKueryORM/Model.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftKueryORM/Model.swift b/Sources/SwiftKueryORM/Model.swift index 9352634..2466cd7 100644 --- a/Sources/SwiftKueryORM/Model.swift +++ b/Sources/SwiftKueryORM/Model.swift @@ -19,6 +19,7 @@ import KituraContracts import Foundation import Dispatch +/// Defines the supported formats for persisiting properties of type `Date`. public enum DateEncodingFormat { case time case date @@ -41,7 +42,7 @@ public protocol Model: Codable { /// Defines the keypath to the Models id field static var idKeypath: IDKeyPath {get} - /// Defines the date encoding strategy for the Model. Defaults to .double + /// Defines the format in which `Date` properties of the `Model` will be written to the Database. Defaults to .double static var dateEncodingFormat: DateEncodingFormat { get } /// Call to create the table in the database synchronously From 50ef9b3b63479579324ac59ba07fbc69b30ff6c2 Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Tue, 21 May 2019 13:03:51 +0100 Subject: [PATCH 06/10] Add comments --- Sources/SwiftKueryORM/Model.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/SwiftKueryORM/Model.swift b/Sources/SwiftKueryORM/Model.swift index 2466cd7..18f4c0d 100644 --- a/Sources/SwiftKueryORM/Model.swift +++ b/Sources/SwiftKueryORM/Model.swift @@ -21,9 +21,13 @@ import Dispatch /// Defines the supported formats for persisiting properties of type `Date`. public enum DateEncodingFormat { + // time - Corresponds to the `time` column type case time + // date - Corresponds to the `date` column type case date + // timestamp - Corresponds to the `timestamp` column type. case timestamp + // double - This is the default encoding type and corresponds to Swifts encoding of `Date`. case double } From 1b33f5293d41447a011f4f650d29285f94a22a8b Mon Sep 17 00:00:00 2001 From: kilnerm <38245247+kilnerm@users.noreply.github.com> Date: Tue, 21 May 2019 15:01:22 +0100 Subject: [PATCH 07/10] Update README.md --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 03f6a31..339b402 100644 --- a/README.md +++ b/README.md @@ -394,6 +394,27 @@ specificPerson.save() { (savedPerson, error) in **NOTE** - When using manual or optional ID properties, you should be prepared to handle violation of unique identifier constraints. These can occur if you attempt to save a model with an ID that already exists, or in the case of Postgres, if the auto-incremented value collides with an ID that was previously inserted explicitly. +## Alternative encoding for `Date` properties + +By default any property on your Model that is declared as a `Date` will be encoded and decoded as a `Double`. + +You can change this behaviour by overriding the default value of the property `dateEncodingStrategy`. The dateEncodingStrategy will apply to all Date properties on your Model. + +The example below declares a model which will have its Date properties endowed and decoded as a timestamp” + +```swift + +struct Person: Model { + + static var dateEncodingStrategy: DateEncodingFormat = .timestamp + + var firstname: String + var surname: String + var age: Int + var dob: Date +} +``` + ## List of plugins * [PostgreSQL](https://github.com/IBM-Swift/Swift-Kuery-PostgreSQL) From 936540355860bd02125d978b52086d45bd7a10b5 Mon Sep 17 00:00:00 2001 From: kilnerm <38245247+kilnerm@users.noreply.github.com> Date: Tue, 21 May 2019 15:02:10 +0100 Subject: [PATCH 08/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 339b402..e233203 100644 --- a/README.md +++ b/README.md @@ -400,7 +400,7 @@ By default any property on your Model that is declared as a `Date` will be encod You can change this behaviour by overriding the default value of the property `dateEncodingStrategy`. The dateEncodingStrategy will apply to all Date properties on your Model. -The example below declares a model which will have its Date properties endowed and decoded as a timestamp” +The example below defines a model which will have its Date properties encoded and decoded as a timestamp” ```swift From 7bed56be04a52be49db187106899a40da837d01b Mon Sep 17 00:00:00 2001 From: kilnerm <38245247+kilnerm@users.noreply.github.com> Date: Thu, 30 May 2019 12:08:02 +0100 Subject: [PATCH 09/10] Fix README typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e233203..e82d2e5 100644 --- a/README.md +++ b/README.md @@ -400,7 +400,7 @@ By default any property on your Model that is declared as a `Date` will be encod You can change this behaviour by overriding the default value of the property `dateEncodingStrategy`. The dateEncodingStrategy will apply to all Date properties on your Model. -The example below defines a model which will have its Date properties encoded and decoded as a timestamp” +The example below defines a model which will have its Date properties encoded and decoded as a timestamp: ```swift From 83f19a65ef7674845ea2faa7094451830d1a1704 Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Thu, 30 May 2019 13:38:57 +0100 Subject: [PATCH 10/10] Update doc comments --- Sources/SwiftKueryORM/Model.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftKueryORM/Model.swift b/Sources/SwiftKueryORM/Model.swift index 18f4c0d..bd77ca8 100644 --- a/Sources/SwiftKueryORM/Model.swift +++ b/Sources/SwiftKueryORM/Model.swift @@ -19,15 +19,15 @@ import KituraContracts import Foundation import Dispatch -/// Defines the supported formats for persisiting properties of type `Date`. +/// The DateEncodingFormat enumeration defines the supported formats for persisiting properties of type `Date`. public enum DateEncodingFormat { - // time - Corresponds to the `time` column type + /// time - Corresponds to the `time` column type case time - // date - Corresponds to the `date` column type + /// date - Corresponds to the `date` column type case date - // timestamp - Corresponds to the `timestamp` column type. + /// timestamp - Corresponds to the `timestamp` column type. case timestamp - // double - This is the default encoding type and corresponds to Swifts encoding of `Date`. + /// double - This is the default encoding type and corresponds to Swifts encoding of `Date`. case double }