Working with items in DynamoDB - AWS SDK for Swift

Working with items in DynamoDB

Dependencies

Include the following dependencies in your Package.swift file.

dependencies: [ .package(name: "AWSSwiftSDK", url: "https://github.com/awslabs/aws-sdk-swift", from: "0.1.0") ]

Add a target dependency to include the DynamoDB product within your Package.swift file. For example:

targets: [ .target( name: "<your target name>", dependencies: [.product(name: "AWSDynamoDB", package: "AWSSwiftSDK")]), ]

If needed, add appropriate platforms to your Package.swift file. For example, to declare support for macOS 10.15 and later as well as iOS 13 and later:

platforms: [.macOS(.v10_15), .iOS(.v13)],

Imports

Add the following imports to your application source:

import AWSDynamoDB import ClientRuntime import AWSClientRuntime

Instantiate a service client

Before using DynamoDB, instantiate a service client object to handle requests:

let dynamoDbClient = try DynamoDbClient(region: "us-east-1")

Insert an item into a table

Use the putItem() method to insert a new record into a DynamoDB table. Refer to the following code snippet.

func putItemInTable(dynamoDbClient: DynamoDbClient, nameOfTable: String, key: String, keyVal: String, albumTitle: String, titleOfAlbum: String, awards: String, awardVal: String, songTitle: String, titleOfSong: String ) { let itemValues = [ key : DynamoDbClientTypes.AttributeValue.s(keyVal), songTitle: DynamoDbClientTypes.AttributeValue.s(titleOfSong), albumTitle: DynamoDbClientTypes.AttributeValue.s(titleOfAlbum), awards: DynamoDbClientTypes.AttributeValue.s(awardVal)] dynamoDbClient.putItem(input: PutItemInput(item: itemValues, tableName: nameOfTable)) { result in switch(result) { case .success: print("A new item was placed into \(nameOfTable)") case .failure(let err): print(err) } } }

This example function defines a function that takes information about a song and writes it to a new record on DynamoDB.

Get an item from a table

Use the getItem() method to fetch an item from a DynamoDB table.

func getDynamoDbItem(dynamoDbClient: DynamoDbClient, nameOfTable: String, keyName: String, keyVal: String) { let keyToGet = [keyName : DynamoDbClientTypes.AttributeValue.s(keyVal)] dynamoDbClient.getItem(input: GetItemInput(key: keyToGet, tableName: nameOfTable)) { result in switch(result) { case .success(let response): guard let numbersMap = response.item else { return } for returnedKey in numbersMap { print("\(returnedKey.key) : \(returnedKey.value)") } case .failure(let err): print(err) } } }

Update an item in a table

Use the updateItem() method to update an item that’s already in a DynamoDB table.

func updateDynamoDbItem(dynamoDbClient: DynamoDbClient, nameOfTable: String, keyName: String, keyVal: String, name: String, newValue: String ) { let itemKey = [keyName : DynamoDbClientTypes.AttributeValue.s(keyVal)] let updatedValues = [name: DynamoDbClientTypes.AttributeValueUpdate(action: .put, value: DynamoDbClientTypes.AttributeValue.s(newValue))] dynamoDbClient.updateItem(input: UpdateItemInput(attributeUpdates: updatedValues, key: itemKey, tableName: nameOfTable)) { result in switch(result){ case .success: print("Item in \(nameOfTable) was updated") case .failure(let err): print(err) } } }

Delete an item from a table

To delete an item from a DynamoDB table, use the deleteItem() method, as shown in the following snippet.

func deleteDynamoDbItem(dynamoDbClient: DynamoDbClient, nameOfTable: String, keyName: String, keyVal: String) { let keyToDelete = [keyName : DynamoDbClientTypes.AttributeValue.s(keyVal)] dynamoDbClient.deleteItem(input: DeleteItemInput(key: keyToDelete, tableName: nameOfTable)) { result in switch(result) { case .success: print("Item with key matching \(keyVal) was deleted") case .failure(let err): print(err) } } }

Additional information