Repository Public License
1import Foundation2import NavigatorCore34/// Profiles. Only `.standard` profiles are stored; a ghost profile is rejected and remembered in the registry.5public struct ProfileStore: Sendable {6 let context: StoreContext78 /// Inserts or updates a profile. Throws `PersistenceError.ghostWriteRejected` for a ghost profile.9 public func save(_ profile: Profile) async throws {10 guard !profile.isGhost else {11 context.ghosts.declare(profile: profile.id)12 throw PersistenceError.ghostWriteRejected(entity: "profile \(profile.id)")13 }14 try await context.transaction { try profile.save($0) }15 }1617 public func profile(_ id: ProfileID) async throws -> Profile? {18 try await context.read { try $0.queryOne("SELECT * FROM profiles WHERE id = ?", [id]).map(Profile.init(row:)) }19 }2021 public func all() async throws -> [Profile] {22 try await context.read {23 try $0.query("SELECT * FROM profiles ORDER BY created_at, id").map(Profile.init(row:))24 }25 }2627 /// Deletes the profile and everything that references it. Prefer `DataLifecycle.deleteProfile`, which also28 /// removes notes, canvases and agent runs of its spaces and reports the archive files to delete.29 public func delete(_ id: ProfileID) async throws {30 try await context.transaction { _ = try $0.execute("DELETE FROM profiles WHERE id = ?", [id]) }31 }32}3334public struct SpaceStore: Sendable {35 let context: StoreContext3637 public func save(_ space: OrbitSpace) async throws {38 try await context.transaction { connection in39 if context.ghosts.contains(space.id.raw) {40 throw PersistenceError.ghostWriteRejected(entity: "space \(space.id)")41 }42 try context.requireProfile(space.profileID, connection)43 try space.save(connection)44 }45 }4647 public func space(_ id: SpaceID) async throws -> OrbitSpace? {48 try await context.read { try $0.queryOne("SELECT * FROM spaces WHERE id = ?", [id]).map(OrbitSpace.init(row:)) }49 }5051 /// Spaces of a profile (or of all profiles), in display order.52 public func spaces(profile: ProfileID? = nil, includeArchived: Bool = true) async throws -> [OrbitSpace] {53 try await context.read { connection in54 try connection.query("""55 SELECT * FROM spaces56 WHERE (?1 IS NULL OR profile_id = ?1) AND (?2 = 1 OR is_archived = 0)57 ORDER BY profile_id, sort_order, created_at, id58 """, [profile, includeArchived]).map(OrbitSpace.init(row:))59 }60 }6162 /// Rewrites `sort_order` to match the given sequence.63 public func reorder(_ ids: [SpaceID]) async throws {64 try await context.transaction { connection in65 for (index, id) in ids.enumerated() {66 try connection.execute("UPDATE spaces SET sort_order = ?, updated_at = ? WHERE id = ?",67 [index, Date.now, id])68 }69 }70 }7172 public func setArchived(_ id: SpaceID, _ archived: Bool, at date: Date = .now) async throws {73 try await context.transaction {74 _ = try $0.execute("UPDATE spaces SET is_archived = ?, updated_at = ? WHERE id = ?", [archived, date, id])75 }76 }7778 /// Deletes the space with its windows, tabs and groups. Notes, canvases and agent runs keep existing unlinked.79 public func delete(_ id: SpaceID) async throws {80 try await context.transaction { _ = try $0.execute("DELETE FROM spaces WHERE id = ?", [id]) }81 }82}8384public struct TabGroupStore: Sendable {85 let context: StoreContext8687 public func save(_ group: TabGroup) async throws {88 try await context.transaction { connection in89 try context.requireSpace(group.spaceID, connection)90 try group.save(connection)91 }92 }9394 public func group(_ id: TabGroupID) async throws -> TabGroup? {95 try await context.read {96 try $0.queryOne("SELECT * FROM tab_groups WHERE id = ?", [id]).map(TabGroup.init(row:))97 }98 }99100 public func groups(space: SpaceID) async throws -> [TabGroup] {101 try await context.read {102 try $0.query("SELECT * FROM tab_groups WHERE space_id = ? ORDER BY sort_order, id", [space])103 .map(TabGroup.init(row:))104 }105 }106107 public func reorder(_ ids: [TabGroupID]) async throws {108 try await context.transaction { connection in109 for (index, id) in ids.enumerated() {110 try connection.execute("UPDATE tab_groups SET sort_order = ? WHERE id = ?", [index, id])111 }112 }113 }114115 /// Deletes the group; its tabs stay open and simply lose their group.116 public func delete(_ id: TabGroupID) async throws {117 try await context.transaction { _ = try $0.execute("DELETE FROM tab_groups WHERE id = ?", [id]) }118 }119}120