Instance Method
model(_:with:)
Use an
Encodable value to generate a row to insert and add that row to the query.@discardableResult func model(_ model: some Encodable, with encoder: SQLQueryEncoder) throws -> Self
Parameters
modelA value to insert. This can 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 encoder = SQLQueryEncoder(nilEncodingStrategy: .asNil)
try await sqlDatabase.insert(into: "planets")
.model(earth, 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))
.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.