Working with tables in DynamoDB - AWS SDK for Swift

Working with tables 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")

Create a new table

Use the createTable() method to create a new DynamoDB table. To call this method, you first need to build the properties for the request. At a minimum, this includes providing an AlternateDefinition to specify value and data type for the record, a KeySchemaElement to specify the composite primary key, and setting the read and write capacity of the table with ProvisionedThroughput.

When you make a request to create a table in DynamoDB, the table isn’t immediately available. Before you can read to or write from the table, verify that it’s status is ACTIVE by calling describeTable()

func createNewTable(ddb: DynamoDbClient, newTableName: String, key: String, completion: @escaping (String?) -> Void) { let attDef = DynamoDbClientTypes.AttributeDefinition(attributeName: key, attributeType: .s) let keySchemaVal = DynamoDbClientTypes.KeySchemaElement(attributeName: key, keyType: .hash) let provisionedVal = DynamoDbClientTypes.ProvisionedThroughput(readCapacityUnits: 10, writeCapacityUnits: 10) let request = CreateTableInput(attributeDefinitions: [attDef], keySchema: [keySchemaVal], provisionedThroughput: provisionedVal, tableName: newTableName) ddb.createTable(input: request) { result in switch(result) { case .success(let response): while true { if checkTableStatus(ddb, newTableName) == "ACTIVE" { break } usleep(500 * 1000) //sleep 500ms } completion(response.tableDescription?.tableArn) case .failure(let err): print(err) completion(nil) } } } func checkTableStatus(_ ddb: DynamoDbClient, _ newTableName: String) -> String { var tableStatus = "" ddb.describeTable(input: DescribeTableInput(tableName: newTableName)) { result in switch(result) { case .success(let response): tableStatus = response.table?.tableStatus?.rawValue ?? "" case .failure(let err): print(err) } } return tableStatus }

This code establishes two functions: one that requests the addition of a new table, and another you can call to ensure the new table is ready for use.

Get table information

Use the describeTable() method to get information about a specific DynamoDB table, as shown in the following code snippet.

func describeDymamoDBTable(ddb: DynamoDbClient, nameOfTable: String?) { ddb.describeTable(input: DescribeTableInput(tableName: nameOfTable)) { result in switch(result) { case .success(let tableInfo): if let table = tableInfo.table, let tableName = table.tableName, let tableArn = table.tableArn, let tableStatus = table.tableStatus { print("Table name \(tableName)") print("Table ARN: \(tableArn)") print("Table Status: \(tableStatus)") print("Item count: \(table.itemCount)") print("Size (bytes): \(table.tableSizeBytes)") } case .failure(let err): print(err) } } }

List tables

To get a list of your DynamoDB tables, use the listTables() method.

func listAllTables(ddb: DynamoDbClient) { ddb.listTables(input: ListTablesInput()) { result in switch(result) { case .success(let response): guard let namesOfTables = response.tableNames else { return } for currName in namesOfTables { print("Table name is \(currName)") } case .failure(let err): print(err) } } }

Delete a table

To delete an existing DynamoDB table, use the deleteTable() method.

func deleteDynamoDBTable(ddb: DynamoDbClient, nameOfTable: String) { ddb.deleteTable(input: DeleteTableInput(tableName: nameOfTable)) { result in switch(result) { case .success: print("\(nameOfTable) was deleted") case .failure(let err): print(err) } } }

Additional information