Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datastore): Add DataBase Name Provider as optional parameter on DataStore plugin initializer #2808

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {

/// The DataStore configuration
var configuration: InternalDatastoreConfiguration

/// The database name provider
let databaseNameProvider: DatabaseNameProvider?

var storageEngine: StorageEngineBehavior!

Expand All @@ -57,18 +60,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:
Expand All @@ -81,6 +87,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(),
Expand All @@ -92,11 +99,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:
Expand Down Expand Up @@ -173,6 +182,7 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
storageEngine = try storageEngineBehaviorFactory(
configuration.isSyncEnabled,
dataStoreConfiguration,
databaseNameProvider,
configuration.validAPIPluginKey,
configuration.validAuthPluginKey,
modelRegistration.version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,13 +96,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)

Expand Down