From 3a5a358724c23cef67f294553201837834a6d30b Mon Sep 17 00:00:00 2001 From: Rene Castro Date: Sat, 11 Mar 2023 00:13:06 -0600 Subject: [PATCH 1/2] added databaseName provider as optional parameter on DataStore plugin initializer --- .../AWSDataStorePlugin/AWSDataStorePlugin.swift | 14 +++++++++++--- .../AWSDataStorePlugin/Storage/StorageEngine.swift | 6 +++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin.swift b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin.swift index c44216f8f5..64ffe00e8b 100644 --- a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin.swift +++ b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin.swift @@ -43,6 +43,9 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin { /// The DataStore configuration let dataStoreConfiguration: DataStoreConfiguration + + /// The database name provider + let databaseNameProvider: DatabaseNameProvider? /// A queue that regulates the execution of operations. This will be instantiated during initalization phase, /// and is clearable by `reset()`. This is implicitly unwrapped to be destroyed when resetting. @@ -76,15 +79,17 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin { /// No-argument init that uses defaults for all providers public init(modelRegistration: AmplifyModelRegistration, - configuration dataStoreConfiguration: DataStoreConfiguration = .default) { + configuration dataStoreConfiguration: DataStoreConfiguration = .default, + databaseNameProvider: DatabaseNameProvider? = nil) { self.modelRegistration = modelRegistration self.dataStoreConfiguration = dataStoreConfiguration + self.databaseNameProvider = databaseNameProvider self.isSyncEnabled = false self.operationQueue = OperationQueue() self.validAPIPluginKey = "awsAPIPlugin" self.validAuthPluginKey = "awsCognitoAuthPlugin" self.storageEngineBehaviorFactory = - StorageEngine.init(isSyncEnabled:dataStoreConfiguration:validAPIPluginKey:validAuthPluginKey:modelRegistryVersion:userDefault:) + StorageEngine.init(isSyncEnabled:dataStoreConfiguration:databaseNameProvider:validAPIPluginKey:validAuthPluginKey:modelRegistryVersion:userDefault:) self.dataStorePublisher = DataStorePublisher() self.dispatchedModelSyncedEvents = [:] } @@ -92,6 +97,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin { /// Internal initializer for testing init(modelRegistration: AmplifyModelRegistration, configuration dataStoreConfiguration: DataStoreConfiguration = .default, + databaseNameProvider: DatabaseNameProvider? = nil, storageEngineBehaviorFactory: StorageEngineBehaviorFactory? = nil, dataStorePublisher: ModelSubcriptionBehavior, operationQueue: OperationQueue = OperationQueue(), @@ -99,10 +105,11 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin { validAuthPluginKey: String) { self.modelRegistration = modelRegistration self.dataStoreConfiguration = dataStoreConfiguration + self.databaseNameProvider = databaseNameProvider self.operationQueue = operationQueue self.isSyncEnabled = false self.storageEngineBehaviorFactory = storageEngineBehaviorFactory ?? - StorageEngine.init(isSyncEnabled:dataStoreConfiguration:validAPIPluginKey:validAuthPluginKey:modelRegistryVersion:userDefault:) + StorageEngine.init(isSyncEnabled:dataStoreConfiguration:databaseNameProvider:validAPIPluginKey:validAuthPluginKey:modelRegistryVersion:userDefault:) self.dataStorePublisher = dataStorePublisher self.dispatchedModelSyncedEvents = [:] self.validAPIPluginKey = validAPIPluginKey @@ -178,6 +185,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin { storageEngine = try storageEngineBehaviorFactory(isSyncEnabled, dataStoreConfiguration, + databaseNameProvider, validAPIPluginKey, validAuthPluginKey, modelRegistration.version, diff --git a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/StorageEngine.swift b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/StorageEngine.swift index ceeb5f92be..e14c679961 100644 --- a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/StorageEngine.swift +++ b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/StorageEngine.swift @@ -13,11 +13,14 @@ import AWSPluginsCore typealias StorageEngineBehaviorFactory = (Bool, DataStoreConfiguration, + DatabaseNameProvider?, String, String, String, UserDefaults) throws -> StorageEngineBehavior +public typealias DatabaseNameProvider = () -> String + // swiftlint:disable type_body_length final class StorageEngine: StorageEngineBehavior { // TODO: Make this private once we get a mutation flow that passes the type of mutation as needed @@ -89,13 +92,14 @@ final class StorageEngine: StorageEngineBehavior { convenience init(isSyncEnabled: Bool, dataStoreConfiguration: DataStoreConfiguration, + databaseNameProvider: DatabaseNameProvider?, validAPIPluginKey: String = "awsAPIPlugin", validAuthPluginKey: String = "awsCognitoAuthPlugin", modelRegistryVersion: String, userDefault: UserDefaults = UserDefaults.standard) throws { let key = kCFBundleNameKey as String - let databaseName = Bundle.main.object(forInfoDictionaryKey: key) as? String ?? "app" + let databaseName = databaseNameProvider?() ?? Bundle.main.object(forInfoDictionaryKey: key) as? String ?? "app" let storageAdapter = try SQLiteStorageEngineAdapter(version: modelRegistryVersion, databaseName: databaseName) From 7e9dfca81317ac8139841f096674f4ceb5dca192 Mon Sep 17 00:00:00 2001 From: BalaramCV Date: Sat, 11 Mar 2023 11:59:59 -0600 Subject: [PATCH 2/2] updated changes to latest main branch --- .../AWSDataStorePlugin/AWSDataStorePlugin.swift | 12 +++++++++++- .../AWSDataStorePlugin/Storage/StorageEngine.swift | 6 +++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin.swift b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin.swift index 4d59086a37..955c72cb6b 100644 --- a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin.swift +++ b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/AWSDataStorePlugin.swift @@ -37,6 +37,9 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin { /// The DataStore configuration var configuration: InternalDatastoreConfiguration + + /// The database name provider + let databaseNameProvider: DatabaseNameProvider? var storageEngine: StorageEngineBehavior! @@ -63,18 +66,21 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin { /// No-argument init that uses defaults for all providers public init(modelRegistration: AmplifyModelRegistration, - configuration dataStoreConfiguration: DataStoreConfiguration = .default) { + configuration dataStoreConfiguration: DataStoreConfiguration = .default, + databaseNameProvider: DatabaseNameProvider? = nil) { self.modelRegistration = modelRegistration self.configuration = InternalDatastoreConfiguration( isSyncEnabled: false, validAPIPluginKey: "awsAPIPlugin", validAuthPluginKey: "awsCognitoAuthPlugin", pluginConfiguration: dataStoreConfiguration) + self.databaseNameProvider = databaseNameProvider self.storageEngineBehaviorFactory = StorageEngine.init( isSyncEnabled: dataStoreConfiguration: + databaseNameProvider: validAPIPluginKey: validAuthPluginKey: modelRegistryVersion: @@ -87,6 +93,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin { /// Internal initializer for testing init(modelRegistration: AmplifyModelRegistration, configuration dataStoreConfiguration: DataStoreConfiguration = .default, + databaseNameProvider: DatabaseNameProvider? = nil, storageEngineBehaviorFactory: StorageEngineBehaviorFactory? = nil, dataStorePublisher: ModelSubcriptionBehavior, operationQueue: OperationQueue = OperationQueue(), @@ -98,11 +105,13 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin { validAPIPluginKey: validAPIPluginKey, validAuthPluginKey: validAuthPluginKey, pluginConfiguration: dataStoreConfiguration) + self.databaseNameProvider = databaseNameProvider self.storageEngineBehaviorFactory = storageEngineBehaviorFactory ?? StorageEngine.init( isSyncEnabled: dataStoreConfiguration: + databaseNameProvider: validAPIPluginKey: validAuthPluginKey: modelRegistryVersion: @@ -178,6 +187,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin { storageEngine = try storageEngineBehaviorFactory( configuration.isSyncEnabled, dataStoreConfiguration, + databaseNameProvider, configuration.validAPIPluginKey, configuration.validAuthPluginKey, modelRegistration.version, diff --git a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/StorageEngine.swift b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/StorageEngine.swift index ceeb5f92be..e14c679961 100644 --- a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/StorageEngine.swift +++ b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/StorageEngine.swift @@ -13,11 +13,14 @@ import AWSPluginsCore typealias StorageEngineBehaviorFactory = (Bool, DataStoreConfiguration, + DatabaseNameProvider?, String, String, String, UserDefaults) throws -> StorageEngineBehavior +public typealias DatabaseNameProvider = () -> String + // swiftlint:disable type_body_length final class StorageEngine: StorageEngineBehavior { // TODO: Make this private once we get a mutation flow that passes the type of mutation as needed @@ -89,13 +92,14 @@ final class StorageEngine: StorageEngineBehavior { convenience init(isSyncEnabled: Bool, dataStoreConfiguration: DataStoreConfiguration, + databaseNameProvider: DatabaseNameProvider?, validAPIPluginKey: String = "awsAPIPlugin", validAuthPluginKey: String = "awsCognitoAuthPlugin", modelRegistryVersion: String, userDefault: UserDefaults = UserDefaults.standard) throws { let key = kCFBundleNameKey as String - let databaseName = Bundle.main.object(forInfoDictionaryKey: key) as? String ?? "app" + let databaseName = databaseNameProvider?() ?? Bundle.main.object(forInfoDictionaryKey: key) as? String ?? "app" let storageAdapter = try SQLiteStorageEngineAdapter(version: modelRegistryVersion, databaseName: databaseName)