Skip to content
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
8 changes: 4 additions & 4 deletions Sources/DynamoQueries/Builders/KeyConditionBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@resultBuilder
public struct KeyConditionBuilder {
public static func buildExpression(_ condition: KeyCondition) -> KeyCondition {
condition
public static func buildExpression(_ condition: KeyCondition) -> [KeyCondition] {
[condition]
}

public static func buildBlock(_ components: KeyCondition...) -> [KeyCondition] {
Array(components)
public static func buildBlock(_ components: [KeyCondition]...) -> [KeyCondition] {
components.flatMap(\.self)
}

public static func buildOptional(_ component: [KeyCondition]?) -> [KeyCondition] {
Expand Down
35 changes: 35 additions & 0 deletions Tests/DynamoQueriesTests/QueryEquivalenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ struct QueryEquivalenceTests {
expectEquivalent(dslSoto, reference)
}

@Test("Query: an if includes its sort key condition only when it holds", arguments: [
"hiker-9",
nil,
])
func queryKeyIf(hikerId: String?) async throws {
let client = RecordingDynamoClient()
_ = try await Hike.query { column in
Key {
column.hikeId == "hike-1"
if let hikerId {
column.hikerSeasonGrade.beginsWith(hikerId)
}
}
}
.execute(using: client)
let dslSoto = try #require(await client.lastQueryInput(for: Hike.self)).toSotoQueryInput()

let reference = hikerId == nil
? DynamoDB.QueryInput(
expressionAttributeValues: [":hikeId": .s("hike-1")],
keyConditionExpression: "hikeId = :hikeId",
tableName: "TrailHikes"
)
: DynamoDB.QueryInput(
expressionAttributeValues: [
":hikeId": .s("hike-1"),
":hikerId": .s("hiker-9"),
],
keyConditionExpression:
"(hikeId = :hikeId) AND (begins_with(hikerSeasonGrade, :hikerId))",
tableName: "TrailHikes"
)
expectEquivalent(dslSoto, reference)
}

@Test("Query: filter expression on top of a key condition (descending GSI)")
func queryWithFilterOnKey() async throws {
let client = RecordingDynamoClient()
Expand Down
Loading