Skip to content
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
39 changes: 15 additions & 24 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
.package(url: "https://github.com/vapor/fluent", from: "3.1.0"),

// 💫 AWS Client Library
.package(url: "https://github.com/swift-aws/aws-sdk-swift", from: "3.0.0"),
.package(url: "https://github.com/swift-aws/aws-sdk-swift", from: "3.3.0"),
],
targets: [
.target(
Expand Down
25 changes: 20 additions & 5 deletions Sources/FluentDynamoDB/DynamoConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,22 @@ extension DynamoConnection: DatabaseQueryable {
case .get:
let inputItem = DynamoDB.GetItemInput(
key: requestedKey, tableName: query.table)
return try self.handle.getItem(inputItem).map { output in
return self.handle.getItem(inputItem).map { output in
return try handler(Output(attributes: output.item))
}
case .set:
let inputItem = DynamoDB.PutItemInput(item: requestedKey, returnValues: .allOld, tableName: query.table)
return try self.handle.putItem(inputItem).map { output in
return self.handle.putItem(inputItem).map { output in
return try handler(Output(attributes: output.attributes))
}
case .delete:
let inputItem: DynamoDB.DeleteItemInput = DynamoDB.DeleteItemInput(
let inputItem = DynamoDB.DeleteItemInput(
key: requestedKey, returnValues: .allOld, tableName: query.table)
return try self.handle.deleteItem(inputItem).map { output in
return self.handle.deleteItem(inputItem).map { output in
return try handler(DynamoValue(attributes: output.attributes))
}
case .filter:
return self.eventLoop.newFailedFuture(error: DynamoConnectionError.notImplementedYet)
}
} catch {
return self.eventLoop.newFailedFuture(error: error)
Expand Down Expand Up @@ -127,7 +129,7 @@ extension DynamoConnection: DatabaseQueryable {
// Note that DynamoDB allows batch operations to query multiple items. For simplicity, we're
// always assuming we're querying one table at a time. We will always check the response for
// the table name we've specified in the query itself.
return try self.handle.batchGetItem(batchInput).map { (output: DynamoDB.BatchGetItemOutput) -> [DynamoValue] in
return self.handle.batchGetItem(batchInput).map { (output: DynamoDB.BatchGetItemOutput) -> [DynamoValue] in
guard let values: [[String : DynamoDB.AttributeValue]] = output.responses?[query.table] else { return [DynamoValue]() }
return values.map { DynamoValue(attributes: $0) }
}
Expand All @@ -136,7 +138,20 @@ extension DynamoConnection: DatabaseQueryable {

case .delete:
throw DynamoConnectionError.notImplementedYet
case .filter:
let queryInput = DynamoDB.QueryInput(
expressionAttributeNames: query.expressionAttributeNames,
expressionAttributeValues: query.expressionAttributeValues,
indexName: query.index,
keyConditionExpression: query.keyConditionExpression,
tableName: query.table)
return self.handle.query(queryInput).map { (output: DynamoDB.QueryOutput) in
return output.items?.compactMap { (item: [String: DynamoDB.AttributeValue]) -> DynamoValue in
return DynamoValue(attributes: item)
} ?? [DynamoValue]()
}
}

} catch {
return self.eventLoop.newFailedFuture(error: error)
}
Expand Down
23 changes: 21 additions & 2 deletions Sources/FluentDynamoDB/Query/DynamoQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
// Created by Joe Smith on 4/12/19.
//

// Good sign of abstraction leakage
import DynamoDB

/// What type of request to make to DynamoDB
public enum DynamoQueryAction {
case set, get, delete
case set, get, delete, filter
}

/// 🔎 A DynamoDB operation
Expand All @@ -20,12 +23,28 @@ public struct DynamoQuery {
/// 🍴 The name of the table in DynamoDB to work on
public let table: String

/// ↪️ An optional (Globlal Secondary or Local) Index to query against
public let index: String?

/// 💸 Which value(s) to perform the action upon
public let keys: [DynamoValue]

public init(action: DynamoQueryAction, table: String, keys: [DynamoValue]) {
public init(action: DynamoQueryAction, table: String, keys: [DynamoValue], index: String? = nil, expressionAttributeNames: [String: String]? = nil, expressionAttributeValues: [String: DynamoDB.AttributeValue]? = nil, keyConditionExpression: String? = nil) {
self.action = action
self.table = table
self.keys = keys
self.index = index

self.expressionAttributeNames = expressionAttributeNames
self.expressionAttributeValues = expressionAttributeValues
self.keyConditionExpression = keyConditionExpression
}

// Need to clean these up and figure out a better/simpler way to do queries.
/// `ExpressionAttributeNames` used when `filter`ing
public let expressionAttributeNames: [String: String]?
/// ExpressionAttributeValues used when `filter`ing
public let expressionAttributeValues: [String: DynamoDB.AttributeValue]?
/// 🔀 [KeyConditionExpression](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#API_Query_RequestSyntax) which tells DynamoDB which queries to filter for. Only used when performing a `filter` action.
public let keyConditionExpression: String?
}