mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Make TimestampedRecord a subprotocol of MutablePersistableRecord
This commit is contained in:
1 parent
c5045dbe7a
commit
86bde428bf
2 files changed
+63
-13
No files matched your search
@@ -141,17 +141,17 @@ It provides the following methods:
|
||||
- `updateWithTimestamp()` behaves like ``MutablePersistableRecord/update(_:onConflict:)``, but it also bumps the modification date.
|
||||
- `updateChangesWithTimestamp()` behaves like ``MutablePersistableRecord/updateChanges(_:onConflict:modify:)``, but it also bumps the modification date if the record is modified.
|
||||
- `touch()` only updates the modification date, just like the `touch` unix command.
|
||||
- `initializeTimestamps()` is available for `TimestampedRecord` types that customize their `willInsert` callback.
|
||||
- `TimestampedRecord` types that customize their `willInsert` callback should call `initializeTimestamps()` in their implementation.
|
||||
|
||||
```swift
|
||||
/// A type that tracks its creation and modification dates. See
|
||||
/// A record type that tracks its creation and modification dates. See
|
||||
/// <https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/recordtimestamps>
|
||||
protocol TimestampedRecord {
|
||||
protocol TimestampedRecord: MutablePersistableRecord {
|
||||
var creationDate: Date? { get set }
|
||||
var modificationDate: Date? { get set }
|
||||
}
|
||||
|
||||
extension TimestampedRecord where Self: MutablePersistableRecord {
|
||||
extension TimestampedRecord {
|
||||
/// By default, `TimestampedRecord` types set `creationDate` and
|
||||
/// `modificationDate` to the transaction date, if they are nil,
|
||||
/// before insertion.
|
||||
@@ -247,7 +247,7 @@ extension TimestampedRecord where Self: MutablePersistableRecord {
|
||||
Usage:
|
||||
|
||||
```swift
|
||||
extension Player: Codable, MutablePersistableRecord, FetchableRecord, TimestampedRecord {
|
||||
extension Player: Codable, TimestampedRecord, FetchableRecord {
|
||||
/// Update auto-incremented id upon successful insertion
|
||||
mutating func didInsert(_ inserted: InsertionSuccess) {
|
||||
id = inserted.rowID
|
||||
@@ -272,11 +272,11 @@ try dbQueue.write { db in
|
||||
$0.score = 1000
|
||||
}
|
||||
|
||||
// updateChanges() is able to always bump the modification date, while
|
||||
// only updating the changed columns.
|
||||
// updateChanges() is able to always bump the modification date, even if
|
||||
// other columns are not changed.
|
||||
try player.updateChanges(db) {
|
||||
$0.score = 1000
|
||||
$0.modificationDate = db.transactionDate
|
||||
$0.modificationDate = try db.transactionDate
|
||||
}
|
||||
|
||||
// touch() only updates the modification date in the database.
|
||||
|
||||
@@ -278,8 +278,8 @@ class TransactionDateTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_TimestampedRecord_customized_willInsert() throws {
|
||||
struct Player: Codable, MutablePersistableRecord, FetchableRecord, TimestampedRecord {
|
||||
func test_TimestampedRecord_struct_with_customized_willInsert() throws {
|
||||
struct Player: Codable, TimestampedRecord, FetchableRecord {
|
||||
var id: Int64?
|
||||
var creationDate: Date?
|
||||
var modificationDate: Date?
|
||||
@@ -324,18 +324,68 @@ class TransactionDateTests: GRDBTestCase {
|
||||
XCTAssertEqual(player.modificationDate, .distantPast)
|
||||
}
|
||||
}
|
||||
|
||||
func test_TimestampedRecord_class_with_non_mutating_willInsert() throws {
|
||||
class Player: Codable, TimestampedRecord, PersistableRecord, FetchableRecord {
|
||||
var id: Int64?
|
||||
var creationDate: Date?
|
||||
var modificationDate: Date?
|
||||
var name: String
|
||||
|
||||
init(id: Int64? = nil, creationDate: Date? = nil, modificationDate: Date? = nil, name: String) {
|
||||
self.id = id
|
||||
self.creationDate = creationDate
|
||||
self.modificationDate = modificationDate
|
||||
self.name = name
|
||||
}
|
||||
|
||||
func willInsert(_ db: Database) throws {
|
||||
// Can't call initializeTimestamps because it is mutating
|
||||
if creationDate == nil {
|
||||
creationDate = try db.transactionDate
|
||||
}
|
||||
if modificationDate == nil {
|
||||
modificationDate = try db.transactionDate
|
||||
}
|
||||
}
|
||||
|
||||
func didInsert(_ inserted: InsertionSuccess) {
|
||||
id = inserted.rowID
|
||||
}
|
||||
}
|
||||
|
||||
var currentDate = Date.distantPast
|
||||
dbConfiguration.transactionClock = .custom { _ in currentDate }
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
t.column("creationDate", .datetime).notNull()
|
||||
t.column("modificationDate", .datetime).notNull()
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
}
|
||||
|
||||
currentDate = Date.distantPast
|
||||
try dbQueue.write { db in
|
||||
let player = Player(name: "Arthur")
|
||||
try player.insert(db)
|
||||
XCTAssertEqual(player.creationDate, .distantPast)
|
||||
XCTAssertEqual(player.modificationDate, .distantPast)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The protocol in RecordTimestamps.md
|
||||
|
||||
/// A type that tracks its creation and modification dates. See
|
||||
/// A record type that tracks its creation and modification dates. See
|
||||
/// <https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/recordtimestamps>
|
||||
protocol TimestampedRecord {
|
||||
protocol TimestampedRecord: MutablePersistableRecord {
|
||||
var creationDate: Date? { get set }
|
||||
var modificationDate: Date? { get set }
|
||||
}
|
||||
|
||||
extension TimestampedRecord where Self: MutablePersistableRecord {
|
||||
extension TimestampedRecord {
|
||||
/// By default, `TimestampedRecord` types set `creationDate` and
|
||||
/// `modificationDate` to the transaction date, if they are nil,
|
||||
/// before insertion.
|
||||
|
||||
Reference in new issue
Block a user