Instance Method
models(_:prefix:keyEncodingStrategy:nilEncodingStrategy:userInfo:)
Use an array of
Encodable values to generate rows to insert and add those rows to the query.@discardableResult func models(_ models: [some Encodable], prefix: String? = nil, keyEncodingStrategy: SQLQueryEncoder.KeyEncodingStrategy = .useDefaultKeys, nilEncodingStrategy: SQLQueryEncoder.NilEncodingStrategy = .default, userInfo: [CodingUserInfoKey : any Sendable] = [:]) throws -> Self
Parameters
modelsArray of values of a given type to insert. The given type may be any encodable type which represents an aggregate value.
prefixSee
prefix.keyEncodingStrategySee
keyEncodingStrategy.nilEncodingStrategySee ``SQLQueryEncoder/nilEncodingStrategy-swift.property`.
userInfoSee
userInfo.
Discussion
Example usage:
let earth = Planet(id: nil, name: "Earth", isInhabited: true)
let mars = Planet(id: nil, name: "Mars", isInhabited: false)
try await sqlDatabase.insert(into: "planets")
.models([earth, mars], keyEncodingStrategy: .convertToSnakeCase)
.run()
// Effectively the same as:
try await sqlDatabase.insert(into: "planets")
.columns("id", "name", "is_inhabited")
.values(SQLBind(earth.id), SQLBind(earth.name), SQLBind(earth.isInhabited))
.values(SQLBind(mars.id), SQLBind(mars.name), SQLBind(mars.isInhabited))
.run()
Note
The term “model” does not refer to Fluent’s Model type. Fluent models are not compatible with this method or any of its variants.