Fix grdb create

This commit is contained in:
世界
2024-09-20 22:49:51 +08:00
parent 10c64390c3
commit de84957b21
3 changed files with 47 additions and 48 deletions
+14 -14
View File
@@ -4,37 +4,37 @@ import GRDB
public enum ProfileManager {
public nonisolated static func create(_ profile: Profile) async throws {
profile.order = try await nextOrder()
try await Database.sharedWriter().write { db in
try await Database.sharedWriter.write { db in
try profile.insert(db, onConflict: .fail)
}
}
public nonisolated static func get(_ profileID: Int64) async throws -> Profile? {
try await Database.sharedWriter().read { db in
try await Database.sharedWriter.read { db in
try Profile.fetchOne(db, id: profileID)
}
}
public nonisolated static func get(by profileName: String) async throws -> Profile? {
try await Database.sharedWriter().read { db in
try await Database.sharedWriter.read { db in
try Profile.filter(Column("name") == profileName).fetchOne(db)
}
}
public nonisolated static func delete(_ profile: Profile) async throws {
_ = try await Database.sharedWriter().write { db in
_ = try await Database.sharedWriter.write { db in
try profile.delete(db)
}
}
public nonisolated static func delete(by id: Int64) async throws {
_ = try await Database.sharedWriter().write { db in
_ = try await Database.sharedWriter.write { db in
try Profile.deleteOne(db, id: id)
}
}
public nonisolated static func delete(_ profileList: [Profile]) async throws -> Int {
try await Database.sharedWriter().write { db in
try await Database.sharedWriter.write { db in
try Profile.deleteAll(db, keys: profileList.map {
["id": $0.id!]
})
@@ -42,20 +42,20 @@ public enum ProfileManager {
}
public nonisolated static func delete(by id: [Int64]) async throws -> Int {
try await Database.sharedWriter().write { db in
try await Database.sharedWriter.write { db in
try Profile.deleteAll(db, ids: id)
}
}
public nonisolated static func update(_ profile: Profile) async throws {
_ = try await Database.sharedWriter().write { db in
_ = try await Database.sharedWriter.write { db in
try profile.updateChanges(db)
}
}
public nonisolated static func update(_ profileList: [Profile]) async throws {
// TODO: batch update
try await Database.sharedWriter().write { db in
try await Database.sharedWriter.write { db in
for profile in profileList {
try profile.updateChanges(db)
}
@@ -63,25 +63,25 @@ public enum ProfileManager {
}
public nonisolated static func list() async throws -> [Profile] {
try await Database.sharedWriter().read { db in
try await Database.sharedWriter.read { db in
try Profile.all().order(Column("order").asc).fetchAll(db)
}
}
public nonisolated static func listRemote() async throws -> [Profile] {
try await Database.sharedWriter().read { db in
try await Database.sharedWriter.read { db in
try Profile.filter(Column("type") == ProfileType.remote.rawValue).order(Column("order").asc).fetchAll(db)
}
}
public nonisolated static func listAutoUpdateEnabled() async throws -> [Profile] {
try await Database.sharedWriter().read { db in
try await Database.sharedWriter.read { db in
try Profile.filter(Column("autoUpdate") == true).order(Column("order").asc).fetchAll(db)
}
}
public nonisolated static func nextID() async throws -> Int64 {
try await Database.sharedWriter().read { db in
try await Database.sharedWriter.read { db in
if let lastProfile = try Profile.select(Column("id")).order(Column("id").desc).fetchOne(db) {
return lastProfile.id! + 1
} else {
@@ -91,7 +91,7 @@ public enum ProfileManager {
}
private nonisolated static func nextOrder() async throws -> UInt32 {
try await Database.sharedWriter().read { db in
try await Database.sharedWriter.read { db in
try UInt32(Profile.fetchCount(db))
}
}