Skip to content

Commit

Permalink
update datastore local only operations with seperate init method
Browse files Browse the repository at this point in the history
  • Loading branch information
Di Wu committed Sep 5, 2023
1 parent d5c409f commit d8a48e8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
{
"pins" : [
{
"identity" : "amplify-swift-utils-notifications",
"kind" : "remoteSourceControl",
"location" : "https://github.com/aws-amplify/amplify-swift-utils-notifications.git",
"state" : {
"revision" : "f970384ad1035732f99259255cd2f97564807e41",
"version" : "1.1.0"
}
},
{
"identity" : "aws-appsync-realtime-client-ios",
"kind" : "remoteSourceControl",
"location" : "https://github.com/aws-amplify/aws-appsync-realtime-client-ios.git",
"state" : {
"revision" : "b036e83716789c13a3480eeb292b70caa54114f2",
"version" : "3.1.0"
}
},
{
"identity" : "aws-crt-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/awslabs/aws-crt-swift",
"state" : {
"revision" : "6feec6c3787877807aa9a00fad09591b96752376",
"version" : "0.6.1"
}
},
{
"identity" : "aws-sdk-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/awslabs/aws-sdk-swift.git",
"state" : {
"revision" : "24bae88a2391fe75da8a940a544d1ef6441f5321",
"version" : "0.13.0"
}
},
{
"identity" : "cwlcatchexception",
"kind" : "remoteSourceControl",
Expand All @@ -53,60 +17,6 @@
"revision" : "a23ded2c91df9156628a6996ab4f347526f17b6b",
"version" : "2.1.2"
}
},
{
"identity" : "smithy-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/awslabs/smithy-swift",
"state" : {
"revision" : "7b28da158d92cd06a3549140d43b8fbcf64a94a6",
"version" : "0.15.0"
}
},
{
"identity" : "sqlite.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stephencelis/SQLite.swift.git",
"state" : {
"revision" : "5f5ad81ac0d0a0f3e56e39e646e8423c617df523",
"version" : "0.13.2"
}
},
{
"identity" : "starscream",
"kind" : "remoteSourceControl",
"location" : "https://github.com/daltoniam/Starscream",
"state" : {
"revision" : "df8d82047f6654d8e4b655d1b1525c64e1059d21",
"version" : "4.0.4"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version" : "1.0.4"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "32e8d724467f8fe623624570367e3d50c5638e46",
"version" : "1.5.2"
}
},
{
"identity" : "xmlcoder",
"kind" : "remoteSourceControl",
"location" : "https://github.com/MaxDesiatov/XMLCoder.git",
"state" : {
"revision" : "b1e944cbd0ef33787b13f639a5418d55b3bed501",
"version" : "0.17.1"
}
}
],
"version" : 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
completion: @escaping DataStoreCallback<M>
) {
log.verbose("Saving: \(model) with condition: \(String(describing: condition))")
let prepareSaveResult = initStorageEngineAndStartSync().flatMap { storageEngineBehavior in
mutationTypeOfModel(model, storageEngine: storageEngineBehavior)
let prepareSaveResult = initStorageEngineAndTryStartSync().flatMap { storageEngineBehavior in
mutationTypeOfModel(model, modelSchema: modelSchema, storageEngine: storageEngineBehavior)
.map { (storageEngineBehavior, $0) }
}

Expand Down Expand Up @@ -197,7 +197,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
paginate paginationInput: QueryPaginationInput? = nil,
completion: DataStoreCallback<[M]>
) {
switch initStorageEngineAndStartSync() {
switch initStorageEngineAndTryStartSync() {
case .success(let storageEngineBehavior):
storageEngineBehavior.query(
modelType,
Expand Down Expand Up @@ -240,7 +240,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
withId id: String,
where predicate: QueryPredicate? = nil) async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
switch initStorageEngineAndStartSync() {
switch initStorageEngineAndTryStartSync() {
case .success(let storageEngineBehavior):
storageEngineBehavior.delete(modelType, modelSchema: modelSchema, withId: id, condition: predicate) { result in
self.onDeleteCompletion(result: result, modelSchema: modelSchema) { result in
Expand Down Expand Up @@ -300,7 +300,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
identifier: ModelIdentifierProtocol,
where predicate: QueryPredicate?,
completion: @escaping DataStoreCallback<Void>) where M: ModelIdentifiable {
switch initStorageEngineAndStartSync() {
switch initStorageEngineAndTryStartSync() {
case .success(let storageEngineBehavior):
storageEngineBehavior.delete(
modelType,
Expand Down Expand Up @@ -346,7 +346,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
modelSchema: ModelSchema,
where predicate: QueryPredicate? = nil,
completion: @escaping DataStoreCallback<Void>) {
switch initStorageEngineAndStartSync() {
switch initStorageEngineAndTryStartSync() {
case .success(let storageEngineBehavior):
storageEngineBehavior.delete(
type(of: model),
Expand Down Expand Up @@ -387,7 +387,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
modelSchema: ModelSchema,
where predicate: QueryPredicate,
completion: @escaping DataStoreCallback<Void>) {
switch initStorageEngineAndStartSync() {
switch initStorageEngineAndTryStartSync() {
case .success(let storageEngineBehavior):
storageEngineBehavior.delete(
modelType,
Expand Down Expand Up @@ -515,15 +515,16 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {

private func mutationTypeOfModel<M: Model>(
_ model: M,
modelSchema: ModelSchema,
storageEngine: StorageEngineBehavior
) -> Result<MutationEvent.MutationType, DataStoreError> {
let modelExists: Bool
do {
guard let engine = storageEngine as? StorageEngine else {
throw DataStoreError.configuration("Unable to get storage adapter", "")
}
modelExists = try engine.storageAdapter.exists(model.schema,
withIdentifier: model.identifier(schema: model.schema),
modelExists = try engine.storageAdapter.exists(modelSchema,
withIdentifier: model.identifier(schema: modelSchema),
predicate: nil)
} catch {
if let dataStoreError = error as? DataStoreError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension AWSDataStorePlugin: DataStoreSubscribeBehavior {
public func observeQuery<M: Model>(for modelType: M.Type,
where predicate: QueryPredicate?,
sort sortInput: QuerySortInput?) -> AmplifyAsyncThrowingSequence<DataStoreQuerySnapshot<M>> {
switch initStorageEngineAndStartSync() {
switch initStorageEngineAndTryStartSync() {
case .success(let storageEngineBehavior):
let modelSchema = modelType.schema
guard let dataStorePublisher = dataStorePublisher else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ final public class AWSDataStorePlugin: DataStoreCategoryPlugin {
}
}

func initStorageEngineAndTryStartSync() -> Result<StorageEngineBehavior, DataStoreError> {
initStorageEngineAndStartSync().flatMapError { error in
switch error {
case .configuration:
return .success(storageEngine)
default:
return .failure(error)
}
}
}

func resolveStorageEngine(dataStoreConfiguration: DataStoreConfiguration) throws {
guard storageEngine == nil else {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import XCTest

@testable import Amplify
@testable import AWSDataStorePlugin
@testable import AWSAPIPlugin

class LocalStoreIntegrationTestBase: XCTestCase {

Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ let dataStoreTargets: [Target] = [
.testTarget(
name: "AWSDataStoreCategoryPluginTests",
dependencies: [
"AWSAPIPlugin",
"AWSDataStorePlugin",
"AmplifyTestCommon",
"AmplifyAsyncTesting"
Expand Down

0 comments on commit d8a48e8

Please sign in to comment.