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

Support onTransactionWrite with GSI key in InMemoryDynamoDBCompositePrimaryKeyTableWithIndex #100

Merged
merged 1 commit into from
Jan 9, 2024
Merged
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
23 changes: 21 additions & 2 deletions Sources/SmokeDynamoDB/DynamoDBCompositePrimaryKeyGSILogic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@
import DynamoDBModel
import NIO

// Provide a default `PolymorphicWriteEntry` for the `DynamoDBCompositePrimaryKeyGSILogic` for backwards compatibility
public struct NoOpPolymorphicWriteEntry: PolymorphicWriteEntry {
public func handle<Context>(context: Context) throws -> Context.WriteEntryTransformType where Context : PolymorphicWriteEntryContext {

Check warning on line 25 in Sources/SmokeDynamoDB/DynamoDBCompositePrimaryKeyGSILogic.swift

View workflow job for this annotation

GitHub Actions / SwiftLint version 3.2.1

Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)
fatalError("Unimplemented")
}
}

/**
A protocol that simulates the logic of a GSI reacting to events on the main table.
*/
public protocol DynamoDBCompositePrimaryKeyGSILogic {
associatedtype GSIAttributesType: PrimaryKeyAttributes

associatedtype WriteEntryType: PolymorphicWriteEntry = NoOpPolymorphicWriteEntry
/**
* Called when an item is inserted on the main table. Can be used to transform the provided item to the item that would be made available on the GSI.

Check warning on line 37 in Sources/SmokeDynamoDB/DynamoDBCompositePrimaryKeyGSILogic.swift

View workflow job for this annotation

GitHub Actions / SwiftLint version 3.2.1

Line Length Violation: Line should be 150 characters or less; currently it has 153 characters (line_length)
*/
func onInsertItem<AttributesType, ItemType>(_ item: TypedDatabaseItem<AttributesType, ItemType>,
gsiDataStore: InMemoryDynamoDBCompositePrimaryKeyTable) -> EventLoopFuture<Void>
Expand Down Expand Up @@ -78,6 +85,13 @@
*/
func onDeleteItem<AttributesType>(forKey key: CompositePrimaryKey<AttributesType>,
gsiDataStore: InMemoryDynamoDBCompositePrimaryKeyTable) async throws

/**
* Called when an transact write in the main table. Can be used to also transact write the corresponding item on the GSI.

*/
func onTransactWrite(_ entries: [WriteEntryType],
gsiDataStore: InMemoryDynamoDBCompositePrimaryKeyTable) async throws
#endif
}

Expand Down Expand Up @@ -114,6 +128,11 @@
gsiDataStore: InMemoryDynamoDBCompositePrimaryKeyTable) async throws {
try await onDeleteItem(forKey: key, gsiDataStore: gsiDataStore).get()
}

// provide default for backwards compatibility
func onTransactWrite(_ entries: [WriteEntryType],
gsiDataStore: InMemoryDynamoDBCompositePrimaryKeyTable) async throws {
// do nothing
}
#endif
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swiftlint:disable cyclomatic_complexity
// swiftlint:disable cyclomatic_complexity type_body_length
// Copyright 2018-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
Expand Down Expand Up @@ -76,8 +76,17 @@ public struct InMemoryDynamoDBCompositePrimaryKeyTableWithIndex<GSILogic: Dynamo
}
}

public func transactWrite<WriteEntryType: PolymorphicWriteEntry>(_ entries: [WriteEntryType]) async throws {
return try await self.primaryTable.transactWrite(entries)
public func transactWrite<WriteEntryType>(_ entries: [WriteEntryType]) async throws where WriteEntryType : PolymorphicWriteEntry {
let transformedEntries = entries.map { entry -> GSILogic.WriteEntryType in
guard let transformedEntry = entry as? GSILogic.WriteEntryType else {
fatalError("Unable to transform transactWrite of type \(type(of: entry)) to \(GSILogic.WriteEntryType.self)")
}

return transformedEntry
}

try await self.primaryTable.transactWrite(transformedEntries)
try await self.gsiLogic.onTransactWrite(transformedEntries, gsiDataStore: self.gsiDataStore)
}

public func transactWrite<WriteEntryType: PolymorphicWriteEntry,
Expand Down
Loading