Instance Method
models(_:with:)
Use an array of
Encodable values to generate rows to insert and add those rows to the query.@discardableResult func models(_ models: [some Encodable], with encoder: SQLQueryEncoder) 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.
encoderA preconfigured
SQLQueryEncoderto use for encoding.
Discussion
Example usage:
let earth = Planet(id: nil, name: "Earth", isInhabited: true)
let mars = Planet(id: nil, name: "Mars", isInhabited: false)
let encoder = SQLQueryEncoder(nilEncodingStrategy: .asNil)
try await sqlDatabase.insert(into: "planets")
.models([earth, mars], with: encoder)
.run()
// Effectively the same as:
try await sqlDatabase.insert(into: "planets")
.columns("id", "name", "isInhabited")
.values(SQLLiteral.null, SQLBind(earth.name), SQLBind(earth.isInhabited))
.values(SQLLiteral.null, 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.