Skip to content
Snippets Groups Projects
Commit d70c0920 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Update DBManager

parent e137a160
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!100Messenger - restore from backup
This commit is part of merge request !100. Comments created here will be created in the context of that merge request.
import Foundation
import XXModels
public struct DBManager {
......@@ -8,7 +9,12 @@ public struct DBManager {
}
extension DBManager {
public static func live() -> DBManager {
public static func live(
url: URL = FileManager.default
.urls(for: .applicationSupportDirectory, in: .userDomainMask)
.first!
.appendingPathComponent("database")
) -> DBManager {
class Container {
var db: Database?
}
......@@ -17,9 +23,9 @@ extension DBManager {
return DBManager(
hasDB: .init { container.db != nil },
makeDB: .live(setDB: { container.db = $0 }),
makeDB: .live(url: url, setDB: { container.db = $0 }),
getDB: .live(getDB: { container.db }),
removeDB: .live(getDB: { container.db }, unsetDB: { container.db = nil })
removeDB: .live(url: url, getDB: { container.db }, unsetDB: { container.db = nil })
)
}
}
......
......@@ -13,18 +13,14 @@ public struct DBManagerMakeDB {
extension DBManagerMakeDB {
public static func live(
url: URL,
setDB: @escaping (Database) -> Void
) -> DBManagerMakeDB {
DBManagerMakeDB {
let dbDirectoryURL = FileManager.default
.urls(for: .applicationSupportDirectory, in: .userDomainMask)
.first!
.appendingPathComponent("database")
try? FileManager.default
.createDirectory(at: dbDirectoryURL, withIntermediateDirectories: true)
.createDirectory(at: url, withIntermediateDirectories: true)
let dbFilePath = dbDirectoryURL
let dbFilePath = url
.appendingPathComponent("db")
.appendingPathExtension("sqlite")
.path
......
......@@ -13,12 +13,18 @@ public struct DBManagerRemoveDB {
extension DBManagerRemoveDB {
public static func live(
url: URL,
getDB: @escaping () -> Database?,
unsetDB: @escaping () -> Void
) -> DBManagerRemoveDB {
DBManagerRemoveDB {
try getDB()?.drop()
let db = getDB()
unsetDB()
try db?.drop()
let fm = FileManager.default
if fm.fileExists(atPath: url.path) {
try fm.removeItem(atPath: url.path)
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment