diff --git a/Sources/Papyrus/Errors/QueryError.swift b/Sources/Papyrus/Errors/QueryError.swift deleted file mode 100644 index 3b297a2..0000000 --- a/Sources/Papyrus/Errors/QueryError.swift +++ /dev/null @@ -1,13 +0,0 @@ -import Foundation - -public extension PapyrusStore { - /// `PapyrusStore` query error. - enum QueryError: Error { - - /// Object not found - case notFound - - /// Invalid schema - case invalidSchema(details: Error) - } -} diff --git a/Sources/Papyrus/Errors/SetupError.swift b/Sources/Papyrus/Errors/SetupError.swift deleted file mode 100644 index e5ac19f..0000000 --- a/Sources/Papyrus/Errors/SetupError.swift +++ /dev/null @@ -1,10 +0,0 @@ -import Foundation - -extension PapyrusStore { - /// Information about errors during `PapyrusStore` setup. - enum SetupError: Error { - /// Unable to create directory. - /// A file already exists at the provided location. - case fileExistsInDirectoryURL(URL) - } -} diff --git a/Sources/Papyrus/PapyrusStore.swift b/Sources/Papyrus/PapyrusStore.swift index 7a5b26f..1527a95 100644 --- a/Sources/Papyrus/PapyrusStore.swift +++ b/Sources/Papyrus/PapyrusStore.swift @@ -159,7 +159,8 @@ public struct PapyrusStore: Sendable { do { try self.createDirectoryIfNeeded(for: object.typeDescription) let data = try self.encoder.encode(object) - try data.write(to: self.fileURL(for: object.typeDescription, filename: filename)) + let url = self.fileURL(for: object.typeDescription, filename: filename) + try data.write(to: url) self.logger.debug("Saved: \(object.typeDescription) [Filename: \(filename)]") } catch { self.logger.fault("Failed to save: \(error)") @@ -222,6 +223,10 @@ public struct PapyrusStore: Sendable { await self.delete(objectIdentifiers: identifiers) } + public func deleteAll(_ type: T.Type) async throws { + try self.fileManager.removeItem(at: self.directoryURL(for: type)) + } + private func delete( objectIdentifiers: [ID: T.Type] ) async where ID: LosslessStringConvertible & Sendable { @@ -327,3 +332,28 @@ public struct PapyrusStore: Sendable { } } } + +// MARK: Setup error + +extension PapyrusStore { + /// Information about errors during `PapyrusStore` setup. + public enum SetupError: Error { + /// Unable to create directory. + /// A file already exists at the provided location. + case fileExistsInDirectoryURL(URL) + } +} + +// MARK: Query error + +extension PapyrusStore { + /// `PapyrusStore` query error. + public enum QueryError: Error { + + /// Object not found + case notFound + + /// Invalid schema + case invalidSchema(details: Error) + } +} diff --git a/Tests/PapyrusTests/PapyrusStoreTests.swift b/Tests/PapyrusTests/PapyrusStoreTests.swift index 23fae67..dd4c7ea 100644 --- a/Tests/PapyrusTests/PapyrusStoreTests.swift +++ b/Tests/PapyrusTests/PapyrusStoreTests.swift @@ -205,21 +205,14 @@ final class PapyrusStoreTests: XCTestCase { } catch { } } -// func testUpdatesReceivedOnDeleting() async { -// let expectation = self.expectation(description: "Received values") -// expectation.expectedFulfillmentCount = 3 -// -// self.store.objects(type: ExampleB.self) -// .publisher() -// .sink { _ in expectation.fulfill() } -// .store(in: &self.cancellables) -// -// let object = ExampleB(id: UUID().uuidString) -// await self.store.save(object) -// await self.store.delete(object) -// -// await self.waitForExpectations(timeout: 5.0) -// } + func test_deleteAll() async throws { + await self.store.save(ExampleB(id: "1")) + await self.store.save(ExampleB(id: "2")) + try await store.deleteAll(ExampleB.self) + + let results = await self.store.objects(type: ExampleB.self).execute() + XCTAssertTrue(results.isEmpty) + } // MARK: Merging