66/// SPDX-License-Identifier: Apache-2.0
77
88// snippet-start:[ddb.swift.databasemanager-all]
9- import Foundation
109import AWSDynamoDB
1110import ClientRuntime
11+ import Foundation
1212
1313/// A protocol describing the implementation of functions that allow either
1414/// calling through to Amazon DynamoDB or mocking DynamoDB functions.
@@ -22,26 +22,32 @@ public protocol DatabaseSession {
2222 /// - Parameter input: A `ListTablesInput` object specifying the input
2323 /// parameters for the call to `listTables()`.
2424 ///
25- /// - Returns: A `ListTablesOutput` structure with the results .
26- func listTables( input: ListTablesInput ) async throws -> ListTablesOutput
25+ /// - Returns: A `[String]` list of table names. .
26+ func listTables( input: ListTablesInput ) async throws -> [ String ]
2727}
2828
2929// snippet-start:[ddb.swift.dynamodbsession]
3030/// An implementation of the `DatabaseSession` protocol that calls through to
3131/// DynamoDB for its operations.
3232public struct DynamoDBSession : DatabaseSession {
33- let awsRegion : String
3433 let client : DynamoDBClient
3534
3635 // snippet-start:[ddb.swift.dynamodbsession.init]
3736 /// Initialize the `DatabaseSession`.
3837 ///
3938 /// - Parameter region: The AWS Region to use for DynamoDB.
4039 ///
41- init ( region: String = " us-east-2 " ) throws {
42- self . awsRegion = region
43- self . client = try DynamoDBClient ( region: awsRegion)
40+ init ( region: String ? = nil ) async throws {
41+ do {
42+ let config = try await DynamoDBClient . DynamoDBClientConfiguration ( )
43+ if let region = region {
44+ config. region = region
45+ }
46+
47+ self . client = DynamoDBClient ( config: config)
48+ }
4449 }
50+
4551 // snippet-end:[ddb.swift.dynamodbsession.init]
4652
4753 // snippet-start:[ddb.swift.dynamodbsession.listtables]
@@ -52,13 +58,31 @@ public struct DynamoDBSession: DatabaseSession {
5258 /// `ListTablesInput` object.
5359 ///
5460 /// - Returns: The `ListTablesOutput` returned by `listTables()`.
55- ///
61+ ///
5662 /// - Throws: Errors from DynamoDB are thrown as usual.
57- public func listTables( input: ListTablesInput ) async throws -> ListTablesOutput {
58- return try await client. listTables ( input: input)
63+ public func listTables( input: ListTablesInput ) async throws -> [ String ] {
64+ do {
65+ // Use "Paginated" to get all the tables.
66+ // This lets the SDK handle the 'lastEvaluatedTableName' property in "ListTablesOutput".
67+ let pages = client. listTablesPaginated ( input: input)
68+
69+ var allTableNames : [ String ] = [ ]
70+ for try await page in pages {
71+ guard let tableNames = page. tableNames else {
72+ print ( " Error: no table names returned. " )
73+ continue
74+ }
75+ allTableNames += tableNames
76+ }
77+ return allTableNames
78+ } catch {
79+ print ( " ERROR: listTables: " , dump ( error) )
80+ throw error
81+ }
5982 }
6083 // snippet-end:[ddb.swift.dynamodbsession.listtables]
6184}
85+
6286// snippet-end:[ddb.swift.dynamodbsession]
6387
6488// snippet-start:[ddb.swift.databasemanager]
@@ -77,6 +101,7 @@ public class DatabaseManager {
77101 init ( session: DatabaseSession ) {
78102 self . session = session
79103 }
104+
80105 // snippet-end:[ddb.swift.databasemanager.init]
81106
82107 // snippet-start:[ddb.swift.databasemanager.gettablelist]
@@ -85,29 +110,12 @@ public class DatabaseManager {
85110 /// - Returns: An array of strings listing all of the tables available
86111 /// in the Region specified when the session was created.
87112 public func getTableList( ) async throws -> [ String ] {
88- var tableList : [ String ] = [ ]
89- var lastEvaluated : String ? = nil
90-
91- // Iterate over the list of tables, 25 at a time, until we have the
92- // names of every table. Add each group to the `tableList` array.
93- // Iteration is complete when `output.lastEvaluatedTableName` is `nil`.
94-
95- repeat {
96- let input = ListTablesInput (
97- exclusiveStartTableName: lastEvaluated,
98- limit: 25
99- )
100- let output = try await self . session. listTables ( input: input)
101- guard let tableNames = output. tableNames else {
102- return tableList
103- }
104- tableList. append ( contentsOf: tableNames)
105- lastEvaluated = output. lastEvaluatedTableName
106- } while lastEvaluated != nil
107-
108- return tableList
113+ let input = ListTablesInput (
114+ )
115+ return try await session. listTables ( input: input)
109116 }
110117 // snippet-end:[ddb.swift.databasemanager.gettablelist]
111118}
119+
112120// snippet-end:[ddb.swift.databasemanager]
113121// snippet-end:[ddb.swift.databasemanager-all]
0 commit comments