Skip to content

Commit

Permalink
feat: delete all (#6)
Browse files Browse the repository at this point in the history
* feat: move things around

* feat: add a delete all
  • Loading branch information
reddavis authored Mar 9, 2023
1 parent 96bb378 commit a929848
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
13 changes: 0 additions & 13 deletions Sources/Papyrus/Errors/QueryError.swift

This file was deleted.

10 changes: 0 additions & 10 deletions Sources/Papyrus/Errors/SetupError.swift

This file was deleted.

32 changes: 31 additions & 1 deletion Sources/Papyrus/PapyrusStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -222,6 +223,10 @@ public struct PapyrusStore: Sendable {
await self.delete(objectIdentifiers: identifiers)
}

public func deleteAll<T: Papyrus>(_ type: T.Type) async throws {
try self.fileManager.removeItem(at: self.directoryURL(for: type))
}

private func delete<ID, T: Papyrus>(
objectIdentifiers: [ID: T.Type]
) async where ID: LosslessStringConvertible & Sendable {
Expand Down Expand Up @@ -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)
}
}
23 changes: 8 additions & 15 deletions Tests/PapyrusTests/PapyrusStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a929848

Please sign in to comment.