|
|
|
|
@ -44,6 +44,8 @@ open class Store { |
|
|
|
|
|
|
|
|
|
fileprivate(set) var identifier: StoreIdentifier? = nil |
|
|
|
|
|
|
|
|
|
fileprivate var _created: Bool = false |
|
|
|
|
|
|
|
|
|
public init() { |
|
|
|
|
self._createDirectory(directory: Store.storageDirectory) |
|
|
|
|
} |
|
|
|
|
@ -55,7 +57,7 @@ open class Store { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _createDirectory(directory: String) { |
|
|
|
|
FileManager.default.createDirectoryInDocuments(directoryName: directory) |
|
|
|
|
self._created = FileManager.default.createDirectoryInDocuments(directoryName: directory) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// A method to provide ids corresponding to the django storage |
|
|
|
|
@ -71,6 +73,10 @@ open class Store { |
|
|
|
|
let collection = StoredCollection<T>(synchronized: synchronized, store: self, indexed: indexed, inMemory: inMemory, sendsUpdate: sendsUpdate) |
|
|
|
|
self._collections[T.resourceName()] = collection |
|
|
|
|
|
|
|
|
|
if self._created, let identifier { |
|
|
|
|
self._migrate(collection, identifier: identifier, type: T.self) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return collection |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -186,4 +192,43 @@ open class Store { |
|
|
|
|
try await StoreCenter.main.sendDeletion(instance) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate var _validIds: [String] = [] |
|
|
|
|
|
|
|
|
|
fileprivate func _migrate<T : Storable>(_ collection: StoredCollection<T>, identifier: StoreIdentifier, type: T.Type) { |
|
|
|
|
|
|
|
|
|
self._validIds.append(identifier.value) |
|
|
|
|
|
|
|
|
|
let oldCollection: StoredCollection<T> = StoredCollection<T>(synchronized: false, store: Store.main, asynchronousIO: false) |
|
|
|
|
|
|
|
|
|
let filtered: [T] = oldCollection.items.filter { item in |
|
|
|
|
|
|
|
|
|
var propertyValue: String? = item.stringForPropertyName(identifier.parameterName) |
|
|
|
|
if propertyValue == nil { |
|
|
|
|
let values = T.relationshipNames.map { item.stringForPropertyName($0) } |
|
|
|
|
propertyValue = values.compactMap { $0 }.first |
|
|
|
|
} |
|
|
|
|
return self._validIds.first(where: { $0 == propertyValue }) != nil |
|
|
|
|
} |
|
|
|
|
self._validIds.append(contentsOf: filtered.map { $0.stringId }) |
|
|
|
|
|
|
|
|
|
try? collection.addOrUpdate(contentOfs: filtered) |
|
|
|
|
Logger.log("Migrated \(filtered.count) \(T.resourceName())") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
extension Storable { |
|
|
|
|
|
|
|
|
|
func stringForPropertyName(_ propertyName: String) -> String? { |
|
|
|
|
let mirror = Mirror(reflecting: self) |
|
|
|
|
for child in mirror.children { |
|
|
|
|
// Logger.log("child.label = \(child.label)") |
|
|
|
|
if let label = child.label, label == "_\(propertyName)" { |
|
|
|
|
return child.value as? String |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Logger.log("returns nil") |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|