1515import BreezeLambdaAPI
1616import BreezeDynamoDBService
1717
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.
1823struct Item : Codable {
1924 public var key : String
2025 public let name : String
@@ -31,27 +36,48 @@ struct Item: Codable {
3136 }
3237}
3338
39+ /// BreezeCodable is a protocol that allows the Item struct to be used with Breeze Lambda API.
3440extension Item : BreezeCodable { }
3541
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.
3645struct APIConfiguration : APIConfiguring {
3746 let dbTimeout : Int64 = 30
3847 func operation( ) throws -> BreezeOperation {
3948 . list
4049 }
4150
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.
4256 func getConfig( ) throws -> BreezeDynamoDBConfig {
4357 BreezeDynamoDBConfig ( region: . useast1, tableName: " Breeze " , keyName: " itemKey " , endpoint: " http://127.0.0.1:4566 " )
4458 }
4559}
4660
4761@main
48- struct BreezeDemoApplication {
62+ struct BreezeLambdaItemAPI {
4963 static func main( ) async throws {
64+ #if DEBUG
5065 do {
5166 let lambdaAPIService = try await BreezeLambdaAPI < Item > ( apiConfig: APIConfiguration ( ) )
5267 try await lambdaAPIService. run ( )
5368 } catch {
5469 print ( error. localizedDescription)
5570 }
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
5682 }
5783}
0 commit comments