AwsDynamoDB is a Swift library that enables you to use Amazon DynamoDB with Swift. More details on this are available from the AWS DynamoDB docmentation.
This package builds with Swift Package Manager. Ensure you have installed and activated the latest Swift 4.2 tool chain.
To use AwsSns, modify the Package.swift file and add following dependency:
.package(url: "https://github.com/nikola-mladenovic/AwsSwiftDynamoDBsdk", from: "0.4.0")
Then import the AwsDynamoDB
library into the swift source code:
import AwsDynamoDB
The current release supports following functionalities: Get Item, Put Item, Delete Item and Query. Library uses Codable
to encode and decode items sent and recieved from DynamoDB.
To use library first initialize the AwsDynamoDB
instance with your credentials and host. After that initialize AwsDynamoDBTable
instance:
let dynamoDb = AwsDynamoDB(host: "https://dynamodb.us-west-2.amazonaws.com", accessKeyId: "OPKASPJPOAS23IOJS", secretAccessKey: "232(I(%$jnasoijaoiwj2919109233")
let testTable = dynamoDb.table(name: "test")
To get item from DynamoDB use the getItem
method of the AwsDynamoDBTable
instance:
testTable.getItem(key: (field: "id", value: "012345"), completion: { (success, item, error) in
// Do some work
...
})
To put item from DynamoDB use the putItem
method of the AwsDynamoDBTable
instance:
struct Person: Codable {
let id: String
let name: String?
}
let person = Person(id: "012345", name: "Bill")
testTable.put(item: person, completion: { (success, error) in
// Do some work
...
})
To delete item from DynamoDB use the deleteItem
method of the AwsDynamoDBTable
instance:
testTable.deleteItem(key: (field: "id", value: "012345"), completion: { (success, error) in
// Do some work
...
})
To update item on DynamoDB use the update
method of the AwsDynamoDBTable
instance:
testTable.update(key: (field: "id", value: "012345"),
expressionAttributeValues: [":newBool" : true, ":incVal" : 3],
updateExpression: "SET bool=:newBool, num = num + :incVal",
completion: { (success, error) in
// Do some work
...
})
To query items in DynamoDB use the query
method of the AwsDynamoDBTable
instance:
testTable.query(keyConditionExpression: "id = :ident", expressionAttributeValues: [":ident" : "012345"]) { (success, items, error) in
// Do some work
...
}
To scan items in DynamoDB use the scan
method of the AwsDynamoDBTable
instance:
testTable.scan(expressionAttributeValues: [":ident" : "Test"], filterExpression: "id = :ident", limit: 1) { (success, items, error) in
// Do some work
...
}