15
15
import BreezeLambdaAPI
16
16
import BreezeDynamoDBService
17
17
18
+ /// The BreezeLambdaItemAPI is an example of a Breeze Lambda API that interacts with DynamoDB to manage items.
19
+ /// Use this example to understand how to create a Breeze Lambda API that can list, create, update, and delete items in a DynamoDB table.
20
+
21
+ /// The Item struct represents an item in the DynamoDB table.
22
+ /// It conforms to Codable for easy encoding and decoding to/from JSON.
18
23
struct Item : Codable {
19
24
public var key : String
20
25
public let name : String
@@ -31,27 +36,48 @@ struct Item: Codable {
31
36
}
32
37
}
33
38
39
+ /// BreezeCodable is a protocol that allows the Item struct to be used with Breeze Lambda API.
34
40
extension Item : BreezeCodable { }
35
41
42
+ /// APIConfiguration is a struct that conforms to APIConfiguring.
43
+ /// It provides the configuration for the Breeze Lambda API, including the DynamoDB table name, key name, and endpoint.
44
+ /// It also specifies the operation to be performed, which in this case is listing items.
36
45
struct APIConfiguration : APIConfiguring {
37
46
let dbTimeout : Int64 = 30
38
47
func operation( ) throws -> BreezeOperation {
39
48
. list
40
49
}
41
50
51
+ /// Get the configuration for the DynamoDB service.
52
+ /// It specifies the region, table name, key name, and endpoint.
53
+ /// In this example, it uses a local Localstack endpoint for testing purposes.
54
+ /// You can change the region, table name, key name, and endpoint as needed for your application.
55
+ /// Remove the endpoint for production use.
42
56
func getConfig( ) throws -> BreezeDynamoDBConfig {
43
57
BreezeDynamoDBConfig ( region: . useast1, tableName: " Breeze " , keyName: " itemKey " , endpoint: " http://127.0.0.1:4566 " )
44
58
}
45
59
}
46
60
47
61
@main
48
- struct BreezeDemoApplication {
62
+ struct BreezeLambdaItemAPI {
49
63
static func main( ) async throws {
64
+ #if DEBUG
50
65
do {
51
66
let lambdaAPIService = try await BreezeLambdaAPI < Item > ( apiConfig: APIConfiguration ( ) )
52
67
try await lambdaAPIService. run ( )
53
68
} catch {
54
69
print ( error. localizedDescription)
55
70
}
71
+ #else
72
+ // In production, you can run the BreezeLambdaAPI without the API configuration.
73
+ // This will use the default configuration for the BreezeDynamoDBService.
74
+ // Make sure to set the environment variables for the DynamoDB service:
75
+ // DYNAMODB_TABLE_NAME, DYNAMODB_KEY_NAME, and AWS_REGION.
76
+ do {
77
+ try await BreezeLambdaAPI < Item > ( ) . run ( )
78
+ } catch {
79
+ print ( error. localizedDescription)
80
+ }
81
+ #endif
56
82
}
57
83
}
0 commit comments