From 720b8974a4051cc15bdd22d82a6489e7a31f86f1 Mon Sep 17 00:00:00 2001 From: Dimitri Bouniol Date: Wed, 20 Mar 2024 03:25:23 -0700 Subject: [PATCH] Added deletion methods that don't throw for non-existent entries Closes #171 --- .../Datastore/Datastore.swift | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Sources/CodableDatastore/Datastore/Datastore.swift b/Sources/CodableDatastore/Datastore/Datastore.swift index 02bff25..57a3019 100644 --- a/Sources/CodableDatastore/Datastore/Datastore.swift +++ b/Sources/CodableDatastore/Datastore/Datastore.swift @@ -885,6 +885,13 @@ extension Datastore where AccessMode == ReadWrite { @discardableResult public func delete(_ idenfifier: IdentifierType) async throws -> CodedType { + guard let deletedInstance = try await deleteIfPresent(idenfifier) + else { throw DatastoreInterfaceError.instanceNotFound } + return deletedInstance + } + + @discardableResult + public func deleteIfPresent(_ idenfifier: IdentifierType) async throws -> CodedType? { try await warmupIfNeeded() return try await persistence._withTransaction( @@ -892,7 +899,17 @@ extension Datastore where AccessMode == ReadWrite { options: [.idempotent] ) { transaction, _ in /// Get a cursor to the entry within the primary index. - let existingEntry = try await transaction.primaryIndexCursor(for: idenfifier, datastoreKey: self.key) + let existingEntry: (cursor: any InstanceCursorProtocol, instanceData: Data, versionData: Data) + do { + existingEntry = try await transaction.primaryIndexCursor(for: idenfifier, datastoreKey: self.key) + } catch DatastoreInterfaceError.instanceNotFound { + return nil + } catch DatastoreInterfaceError.datastoreKeyNotFound { + /// There isn't a datastore yet, so no entries would exist either. + return nil + } catch { + throw error + } /// Delete the instance at that cursor. try await transaction.deletePrimaryIndexEntry(cursor: existingEntry.cursor, datastoreKey: self.key)