@@ -47,12 +47,21 @@ struct LambdaFunction {
47
47
private func main( ) async throws {
48
48
49
49
// Instantiate LambdaRuntime with a handler implementing the business logic of the Lambda function
50
- let runtime = LambdaRuntime ( logger: self . logger, body: self . handler)
50
+ let lambdaRuntime = LambdaRuntime ( logger: self . logger, body: self . handler)
51
+
52
+ // Use a prelude service to execute PG code before setting up the Lambda service
53
+ // the PG code will run only once and will create the database schema and populate it with initial data
54
+ let preludeService = PreludeService (
55
+ service: lambdaRuntime,
56
+ prelude: {
57
+ try await prepareDatabase ( )
58
+ }
59
+ )
51
60
52
61
/// Use ServiceLifecycle to manage the initialization and termination
53
62
/// of the PGClient together with the LambdaRuntime
54
63
let serviceGroup = ServiceGroup (
55
- services: [ self . pgClient, runtime ] ,
64
+ services: [ self . pgClient, preludeService ] ,
56
65
gracefulShutdownSignals: [ . sigterm] ,
57
66
cancellationSignals: [ . sigint] ,
58
67
logger: self . logger
@@ -76,12 +85,6 @@ struct LambdaFunction {
76
85
// This is why there is a timeout, as suggested Fabian
77
86
// See: https://github.com/vapor/postgres-nio/issues/489#issuecomment-2186509773
78
87
result = try await timeout ( deadline: . seconds( 3 ) ) {
79
- // check if table exists
80
- // TODO: ideally, I want to do this once, after serviceGroup.run() is done
81
- // but before the handler is called
82
- logger. trace ( " Checking database " )
83
- try await prepareDatabase ( )
84
-
85
88
// query users
86
89
logger. trace ( " Querying database " )
87
90
return try await self . queryUsers ( )
0 commit comments