A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.
C++
// SwiftDataConnectionRepository.swift
// Gridex
//
// SwiftData implementation of ConnectionRepository.
import Foundation
import SwiftData
final class SwiftDataConnectionRepository: ConnectionRepository, @unchecked Sendable {
private let modelContainer: ModelContainer
init(modelContainer: ModelContainer) {
self.modelContainer = modelContainer
}
@MainActor
func fetchAll() async throws -> [ConnectionConfig] {
let context = modelContainer.mainContext
let descriptor = FetchDescriptor<SavedConnectionEntity>(
sortBy: [SortDescriptor(\.sortOrder)]
)
let entities = try context.fetch(descriptor)
return entities.map { $0.toConfig() }
}
@MainActor
func fetchByID(_ id: UUID) async throws -> ConnectionConfig? {
let context = modelContainer.mainContext
var descriptor = FetchDescriptor<SavedConnectionEntity>(
predicate: #Predicate { $0.id == id }
)
descriptor.fetchLimit = 1
return try context.fetch(descriptor).first?.toConfig()
}
@MainActor
func fetchByGroup(_ group: String) async throws -> [ConnectionConfig] {
let context = modelContainer.mainContext
let descriptor = FetchDescriptor<SavedConnectionEntity>(
predicate: #Predicate { $0.group == group },
sortBy: [SortDescriptor(\.sortOrder)]
)
return try context.fetch(descriptor).map { $0.toConfig() }
}
@MainActor
func save(_ config: ConnectionConfig) async throws {
let context = modelContainer.mainContext
let entity = SavedConnectionEntity(
id: config.id,
name: config.name,
databaseType: config.databaseType.rawValue,
host: config.host,
port: config.port,
database: config.database,
username: config.username,
sslEnabled: config.sslEnabled,
sslMode: config.sslMode?.rawValue,
sslKeyPath: config.sslKeyPath,
sslCertPath: config.sslCertPath,
sslCACertPath: config.sslCACertPath,
sshEnabled: config.sshConfig != nil,
sshHost: config.sshConfig?.host,
sshPort: config.sshConfig?.port,
sshUsername: config.sshConfig?.username,
sshAuthMethod: config.sshConfig?.authMethod.rawValue,
sshKeyPath: config.sshConfig?.keyPath,
colorTag: config.colorTag?.rawValue,
group: config.group,
filePath: config.filePath,
mcpMode: config.mcpMode.rawValue,
mongoOptionsJSON: Self.encodeMongoOptions(config.mongoOptions)
)
context.insert(entity)
try context.save()
}
@MainActor
func update(_ config: ConnectionConfig) async throws {
let context = modelContainer.mainContext
let id = config.id
var descriptor = FetchDescriptor<SavedConnectionEntity>(
predicate: #Predicate { $0.id == id }
)
descriptor.fetchLimit = 1
guard let entity = try context.fetch(descriptor).first else { return }
entity.name = config.name
entity.databaseType = config.databaseType.rawValue
entity.host = config.host
entity.port = config.port
entity.database = config.database
entity.username = config.username
entity.sslEnabled = config.sslEnabled
entity.sslMode = config.sslMode?.rawValue
entity.sslKeyPath = config.sslKeyPath
entity.sslCertPath = config.sslCertPath
entity.sslCACertPath = config.sslCACertPath
entity.colorTag = config.colorTag?.rawValue
entity.group = config.group
entity.filePath = config.filePath
entity.sshEnabled = config.sshConfig != nil
entity.sshHost = config.sshConfig?.host
entity.sshPort = config.sshConfig?.port
entity.sshUsername = config.sshConfig?.username
entity.sshAuthMethod = config.sshConfig?.authMethod.rawValue
entity.sshKeyPath = config.sshConfig?.keyPath
entity.mcpMode = config.mcpMode.rawValue
entity.mongoOptionsJSON = Self.encodeMongoOptions(config.mongoOptions)
try context.save()
}
private static let mongoOptionsEncoder = JSONEncoder()
/// Encode MongoDB options as a JSON string for SwiftData storage. Returns nil
/// when the dictionary is absent or empty so the column stays unused for non-Mongo rows.
private static func encodeMongoOptions(_ options: [String: String]?) -> String? {
guard let options, !options.isEmpty,
let data = try? mongoOptionsEncoder.encode(options),
let str = String(data: data, encoding: .utf8)
else { return nil }
return str
}
@MainActor
func delete(_ id: UUID) async throws {
let context = modelContainer.mainContext
let descriptor = FetchDescriptor<SavedConnectionEntity>(
predicate: #Predicate { $0.id == id }
)
if let entity = try context.fetch(descriptor).first {
context.delete(entity)
try context.save()
}
}
@MainActor
func updateLastConnected(_ id: UUID, date: Date) async throws {
let context = modelContainer.mainContext
var descriptor = FetchDescriptor<SavedConnectionEntity>(
predicate: #Predicate { $0.id == id }
)
descriptor.fetchLimit = 1
if let entity = try context.fetch(descriptor).first {
entity.lastConnectedAt = date
try context.save()
}
}
@MainActor
func reorder(ids: [UUID]) async throws {
// TODO: Update sortOrder for each entity
}
}