Skip to content

Commit

Permalink
Added deletion methods that don't throw for non-existent entries
Browse files Browse the repository at this point in the history
Closes #171
  • Loading branch information
dimitribouniol committed Mar 20, 2024
1 parent 21343f3 commit 720b897
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Sources/CodableDatastore/Datastore/Datastore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -885,14 +885,31 @@ 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(
actionName: "Delete Entry",
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)
Expand Down

0 comments on commit 720b897

Please sign in to comment.