Skip to content

Commit 720b897

Browse files
Added deletion methods that don't throw for non-existent entries
Closes #171
1 parent 21343f3 commit 720b897

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Sources/CodableDatastore/Datastore/Datastore.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,14 +885,31 @@ extension Datastore where AccessMode == ReadWrite {
885885

886886
@discardableResult
887887
public func delete(_ idenfifier: IdentifierType) async throws -> CodedType {
888+
guard let deletedInstance = try await deleteIfPresent(idenfifier)
889+
else { throw DatastoreInterfaceError.instanceNotFound }
890+
return deletedInstance
891+
}
892+
893+
@discardableResult
894+
public func deleteIfPresent(_ idenfifier: IdentifierType) async throws -> CodedType? {
888895
try await warmupIfNeeded()
889896

890897
return try await persistence._withTransaction(
891898
actionName: "Delete Entry",
892899
options: [.idempotent]
893900
) { transaction, _ in
894901
/// Get a cursor to the entry within the primary index.
895-
let existingEntry = try await transaction.primaryIndexCursor(for: idenfifier, datastoreKey: self.key)
902+
let existingEntry: (cursor: any InstanceCursorProtocol, instanceData: Data, versionData: Data)
903+
do {
904+
existingEntry = try await transaction.primaryIndexCursor(for: idenfifier, datastoreKey: self.key)
905+
} catch DatastoreInterfaceError.instanceNotFound {
906+
return nil
907+
} catch DatastoreInterfaceError.datastoreKeyNotFound {
908+
/// There isn't a datastore yet, so no entries would exist either.
909+
return nil
910+
} catch {
911+
throw error
912+
}
896913

897914
/// Delete the instance at that cursor.
898915
try await transaction.deletePrimaryIndexEntry(cursor: existingEntry.cursor, datastoreKey: self.key)

0 commit comments

Comments
 (0)