From e702efae3e65ac8f081f9f97f1446dbcd44590d7 Mon Sep 17 00:00:00 2001 From: Dimitri Bouniol Date: Sun, 31 Mar 2024 02:01:36 -0700 Subject: [PATCH] Updated persist methods to return previous values when updating older instances Fixed #180 --- .../Datastore/Datastore.swift | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Sources/CodableDatastore/Datastore/Datastore.swift b/Sources/CodableDatastore/Datastore/Datastore.swift index b514641..1cb9678 100644 --- a/Sources/CodableDatastore/Datastore/Datastore.swift +++ b/Sources/CodableDatastore/Datastore/Datastore.swift @@ -653,14 +653,15 @@ extension Datastore where AccessMode == ReadWrite { /// - Parameters: /// - instance: The instance to persist. /// - idenfifier: The unique identifier to use to reference the item being persisted. - public func persist(_ instance: CodedType, to idenfifier: IdentifierType) async throws { + @discardableResult + public func persist(_ instance: CodedType, to idenfifier: IdentifierType) async throws -> CodedType? { try await warmupIfNeeded() let updatedDescriptor = try self.updatedDescriptor(for: instance) let versionData = try Data(self.version) let instanceData = try await self.encoder(instance) - try await persistence._withTransaction( + return try await persistence._withTransaction( actionName: "Persist Entry", options: [.idempotent] ) { transaction, _ in @@ -869,6 +870,8 @@ extension Datastore where AccessMode == ReadWrite { datastoreKey: self.key ) } + + return existingInstance } } @@ -879,7 +882,8 @@ extension Datastore where AccessMode == ReadWrite { /// - Parameters: /// - instance: The instance to persist. /// - keypath: The keypath the identifier is located at. - public func persist(_ instance: CodedType, id keypath: KeyPath) async throws { + @discardableResult + public func persist(_ instance: CodedType, id keypath: KeyPath) async throws -> CodedType? { try await persist(instance, to: instance[keyPath: keypath]) } @@ -1018,15 +1022,23 @@ extension Datastore where CodedType: Identifiable, IdentifierType == CodedType.I /// If an instance does not already exist for the specified identifier, it will be created. If an instance already exists, it will be updated. /// - Parameter instance: The instance to persist. @_disfavoredOverload - public func persist(_ instance: CodedType) async throws where AccessMode == ReadWrite { + @discardableResult + public func persist(_ instance: CodedType) async throws -> CodedType? where AccessMode == ReadWrite { try await self.persist(instance, to: instance.id) } @_disfavoredOverload - public func delete(_ instance: CodedType) async throws where AccessMode == ReadWrite { + @discardableResult + public func delete(_ instance: CodedType) async throws -> CodedType where AccessMode == ReadWrite { try await self.delete(instance.id) } + @_disfavoredOverload + @discardableResult + public func deleteIfPresent(_ instance: CodedType) async throws -> CodedType? where AccessMode == ReadWrite { + try await self.deleteIfPresent(instance.id) + } + @_disfavoredOverload public func load(_ instance: CodedType) async throws -> CodedType? { try await self.load(instance.id)