Skip to content

Commit 22c0de8

Browse files
Updated persist methods to return previous values when updating older instances
Fixed #180
1 parent 11c79dd commit 22c0de8

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

Sources/CodableDatastore/Datastore/Datastore.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -653,14 +653,15 @@ extension Datastore where AccessMode == ReadWrite {
653653
/// - Parameters:
654654
/// - instance: The instance to persist.
655655
/// - idenfifier: The unique identifier to use to reference the item being persisted.
656-
public func persist(_ instance: CodedType, to idenfifier: IdentifierType) async throws {
656+
@discardableResult
657+
public func persist(_ instance: CodedType, to idenfifier: IdentifierType) async throws -> CodedType? {
657658
try await warmupIfNeeded()
658659

659660
let updatedDescriptor = try self.updatedDescriptor(for: instance)
660661
let versionData = try Data(self.version)
661662
let instanceData = try await self.encoder(instance)
662663

663-
try await persistence._withTransaction(
664+
return try await persistence._withTransaction(
664665
actionName: "Persist Entry",
665666
options: [.idempotent]
666667
) { transaction, _ in
@@ -869,6 +870,8 @@ extension Datastore where AccessMode == ReadWrite {
869870
datastoreKey: self.key
870871
)
871872
}
873+
874+
return existingInstance
872875
}
873876
}
874877

@@ -879,7 +882,8 @@ extension Datastore where AccessMode == ReadWrite {
879882
/// - Parameters:
880883
/// - instance: The instance to persist.
881884
/// - keypath: The keypath the identifier is located at.
882-
public func persist(_ instance: CodedType, id keypath: KeyPath<CodedType, IdentifierType>) async throws {
885+
@discardableResult
886+
public func persist(_ instance: CodedType, id keypath: KeyPath<CodedType, IdentifierType>) async throws -> CodedType? {
883887
try await persist(instance, to: instance[keyPath: keypath])
884888
}
885889

@@ -1018,15 +1022,23 @@ extension Datastore where CodedType: Identifiable, IdentifierType == CodedType.I
10181022
/// If an instance does not already exist for the specified identifier, it will be created. If an instance already exists, it will be updated.
10191023
/// - Parameter instance: The instance to persist.
10201024
@_disfavoredOverload
1021-
public func persist(_ instance: CodedType) async throws where AccessMode == ReadWrite {
1025+
@discardableResult
1026+
public func persist(_ instance: CodedType) async throws -> CodedType? where AccessMode == ReadWrite {
10221027
try await self.persist(instance, to: instance.id)
10231028
}
10241029

10251030
@_disfavoredOverload
1026-
public func delete(_ instance: CodedType) async throws where AccessMode == ReadWrite {
1031+
@discardableResult
1032+
public func delete(_ instance: CodedType) async throws -> CodedType where AccessMode == ReadWrite {
10271033
try await self.delete(instance.id)
10281034
}
10291035

1036+
@_disfavoredOverload
1037+
@discardableResult
1038+
public func deleteIfPresent(_ instance: CodedType) async throws -> CodedType? where AccessMode == ReadWrite {
1039+
try await self.deleteIfPresent(instance.id)
1040+
}
1041+
10301042
@_disfavoredOverload
10311043
public func load(_ instance: CodedType) async throws -> CodedType? {
10321044
try await self.load(instance.id)

0 commit comments

Comments
 (0)