@@ -12,15 +12,20 @@ extension Request {
12
12
}
13
13
14
14
public func db( _ id: DatabaseID ? ) -> any Database {
15
- self . application
16
- . databases
17
- . database (
18
- id,
19
- logger: self . logger,
20
- on: self . eventLoop,
21
- history: self . fluent. history. historyEnabled ? self . fluent. history. history : nil ,
22
- pageSizeLimit: self . fluent. pagination. pageSizeLimit != nil ? self . fluent. pagination. pageSizeLimit? . value : self . application. fluent. pagination. pageSizeLimit
23
- ) !
15
+ self . db ( id, logger: self . logger)
16
+ }
17
+
18
+ public func db( _ id: DatabaseID ? , logger: Logger ) -> any Database {
19
+ self . application. databases. database (
20
+ id,
21
+ logger: logger,
22
+ on: self . eventLoop,
23
+ history: self . fluent. history. historyEnabled ? self . fluent. history. history : nil ,
24
+ // Use map() (not flatMap()) so if pageSizeLimit is non-nil but the value is nil
25
+ // the request's "no limit" setting overrides the app's setting.
26
+ pageSizeLimit: self . fluent. pagination. pageSizeLimit. map ( \. value) ??
27
+ self . application. fluent. pagination. pageSizeLimit
28
+ ) !
24
29
}
25
30
26
31
public var fluent : Fluent {
@@ -34,14 +39,17 @@ extension Application {
34
39
}
35
40
36
41
public func db( _ id: DatabaseID ? ) -> any Database {
37
- self . databases
38
- . database (
39
- id,
40
- logger: self . logger,
41
- on: self . eventLoopGroup. any ( ) ,
42
- history: self . fluent. history. historyEnabled ? self . fluent. history. history : nil ,
43
- pageSizeLimit: self . fluent. pagination. pageSizeLimit
44
- ) !
42
+ self . db ( id, logger: self . logger)
43
+ }
44
+
45
+ public func db( _ id: DatabaseID ? , logger: Logger ) -> any Database {
46
+ self . databases. database (
47
+ id,
48
+ logger: logger,
49
+ on: self . eventLoopGroup. any ( ) ,
50
+ history: self . fluent. history. historyEnabled ? self . fluent. history. history : nil ,
51
+ pageSizeLimit: self . fluent. pagination. pageSizeLimit
52
+ ) !
45
53
}
46
54
47
55
public var databases : Databases {
@@ -53,7 +61,7 @@ extension Application {
53
61
}
54
62
55
63
public var migrator : Migrator {
56
- Migrator (
64
+ . init (
57
65
databases: self . databases,
58
66
migrations: self . migrations,
59
67
logger: self . logger,
@@ -85,10 +93,7 @@ extension Application {
85
93
let migrationLogLevel : NIOLockedValueBox < Logger . Level >
86
94
87
95
init ( threadPool: NIOThreadPool , on eventLoopGroup: any EventLoopGroup , migrationLogLevel: Logger . Level ) {
88
- self . databases = Databases (
89
- threadPool: threadPool,
90
- on: eventLoopGroup
91
- )
96
+ self . databases = Databases ( threadPool: threadPool, on: eventLoopGroup)
92
97
self . migrations = . init( )
93
98
self . migrationLogLevel = . init( migrationLogLevel)
94
99
}
@@ -99,25 +104,35 @@ extension Application {
99
104
}
100
105
101
106
struct Lifecycle : LifecycleHandler {
102
- func willBoot( _ application: Application ) throws {
103
- struct Signature : CommandSignature {
104
- @Flag ( name: " auto-migrate " , help: " If true, Fluent will automatically migrate your database on boot " )
105
- var autoMigrate : Bool
106
-
107
- @Flag ( name: " auto-revert " , help: " If true, Fluent will automatically revert your database on boot " )
108
- var autoRevert : Bool
107
+ struct Signature : CommandSignature {
108
+ @Flag ( name: " auto-migrate " , help: " If true, Fluent will automatically migrate your database on boot " )
109
+ var autoMigrate : Bool
109
110
110
- init ( ) { }
111
- }
111
+ @Flag ( name: " auto-revert " , help: " If true, Fluent will automatically revert your database on boot " )
112
+ var autoRevert : Bool
113
+ }
112
114
115
+ func willBoot( _ application: Application ) throws {
113
116
let signature = try Signature ( from: & application. environment. commandInput)
117
+
114
118
if signature. autoRevert {
115
119
try application. autoRevert ( ) . wait ( )
116
120
}
117
121
if signature. autoMigrate {
118
122
try application. autoMigrate ( ) . wait ( )
119
123
}
120
124
}
125
+
126
+ func willBootAsync( _ application: Application ) async throws {
127
+ let signature = try Signature ( from: & application. environment. commandInput)
128
+
129
+ if signature. autoRevert {
130
+ try await application. autoRevert ( )
131
+ }
132
+ if signature. autoMigrate {
133
+ try await application. autoMigrate ( )
134
+ }
135
+ }
121
136
122
137
func shutdown( _ application: Application ) {
123
138
application. databases. shutdown ( )
@@ -148,21 +163,11 @@ extension Application {
148
163
nonmutating set { self . storage. migrationLogLevel. withLockedValue { $0 = newValue } }
149
164
}
150
165
151
- public var history : History {
152
- . init( fluent: self )
153
- }
154
-
155
- public struct History {
156
- let fluent : Fluent
157
- }
166
+ public struct History { let fluent : Fluent }
167
+ public var history : History { . init( fluent: self ) }
158
168
159
- public var pagination : Pagination {
160
- . init( fluent: self )
161
- }
162
-
163
- public struct Pagination {
164
- let fluent : Fluent
165
- }
169
+ public struct Pagination { let fluent : Fluent }
170
+ public var pagination : Pagination { . init( fluent: self ) }
166
171
}
167
172
168
173
public var fluent : Fluent {
0 commit comments