Skip to content

Commit cbb0ff5

Browse files
committed
auto foreign keys config
1 parent e0fc759 commit cbb0ff5

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

Sources/FluentProvider/Provider.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public final class Provider: Vapor.Provider {
3535
/// ex: user_pet vs. user+pet vs. user^pet
3636
/// default is _
3737
public let pivotNameConnector: String?
38+
39+
/// If true, foreign keys will automatically
40+
/// be added by Fluent
41+
public let autoForeignKeys: Bool?
3842

3943
public init(
4044
idKey: String? = nil,
@@ -43,7 +47,8 @@ public final class Provider: Vapor.Provider {
4347
defaultPageKey: String? = nil,
4448
defaultPageSize: Int? = nil,
4549
migrationEntityName: String? = nil,
46-
pivotNameConnector: String? = nil
50+
pivotNameConnector: String? = nil,
51+
autoForeignKeys: Bool? = nil
4752
) {
4853
self.idKey = idKey
4954
self.idType = idType
@@ -52,6 +57,7 @@ public final class Provider: Vapor.Provider {
5257
self.defaultPageSize = defaultPageSize
5358
self.migrationEntityName = migrationEntityName
5459
self.pivotNameConnector = pivotNameConnector
60+
self.autoForeignKeys = autoForeignKeys
5561
}
5662

5763
public init(config: Settings.Config) throws {
@@ -99,10 +105,12 @@ public final class Provider: Vapor.Provider {
99105
keyNamingConvention = nil
100106
}
101107

108+
102109
self.defaultPageKey = fluent["defaultPageKey"]?.string
103110
self.defaultPageSize = fluent["defaultPageSize"]?.int
104111
self.migrationEntityName = fluent["migrationEntityName"]?.string
105112
self.pivotNameConnector = fluent["pivotNameConnector"]?.string
113+
self.autoForeignKeys = fluent["autoForeignKeys"]?.bool
106114

107115
// make sure they have specified a fluent.driver
108116
// to help avoid confusing `noDatabase` errors.
@@ -114,29 +122,35 @@ public final class Provider: Vapor.Provider {
114122
)
115123
}
116124
}
117-
118-
public func beforeRun(_ drop: Droplet) throws {
119-
// add configurable driver types, this must
125+
126+
public func boot(_ drop: Droplet) throws {
127+
// add configurable driver types, this must
120128
// come before the preparation calls
121129
try drop.addConfigurable(driver: MemoryDriver.self, name: "memory")
122130
try drop.addConfigurable(driver: SQLiteDriver.self, name: "sqlite")
123-
131+
124132
if let m = self.migrationEntityName {
125133
Fluent.migrationEntityName = m
126134
}
127135

128136
if let p = self.pivotNameConnector {
129137
Fluent.pivotNameConnector = p
130138
}
131-
139+
132140
if let s = self.defaultPageSize {
133141
Fluent.defaultPageSize = s
134142
}
135-
143+
136144
if let k = self.defaultPageKey {
137145
FluentProvider.defaultPageKey = k
138146
}
147+
148+
if let f = self.autoForeignKeys {
149+
Fluent.autoForeignKeys = f
150+
}
151+
}
139152

153+
public func beforeRun(_ drop: Droplet) throws {
140154
if let db = drop.database {
141155
drop.addConfigurable(cache: FluentCache(db), name: "fluent")
142156

@@ -187,6 +201,4 @@ public final class Provider: Vapor.Provider {
187201
/// Preparations run everytime to ensure database is configured properly
188202
try prepare.run(arguments: drop.arguments)
189203
}
190-
191-
public func boot(_ drop: Droplet) {}
192204
}

0 commit comments

Comments
 (0)