PersistableRecord
A type that can be persisted in the database.
protocol PersistableRecord : MutablePersistableRecord
Browse conforming typesPersistableRecord
has non-mutating variants of MutablePersistableRecord
methods.
Conforming to the PersistableRecord Protocol
To conform to PersistableRecord
, provide an implementation for the encode(to:)
method. This implementation is ready-made for Encodable
types.
You configure the database table where records are persisted with the TableRecord
inherited protocol.
Inserting a Record
func insert(Database, onConflict: Database.ConflictResolution?
) throws Executes an
INSERT
statement.func upsert(Database
) throws Executes an
INSERT ON CONFLICT DO UPDATE
statement.
Inserting a Record and Fetching the Inserted Row
func insertAndFetch<T>(Database, onConflict: Database.ConflictResolution?, as: T.Type
) throws -> T Executes an
INSERT RETURNING
statement, and returns a new record built from the inserted row.func insertAndFetch<T>(Database, onConflict: Database.ConflictResolution?, selection: [any SQLSelectable], fetch: (Statement) throws -> T
) throws -> T Executes an
INSERT RETURNING
statement, and returns the selected columns from the inserted row.func upsertAndFetch(Database, onConflict: [String], doUpdate: ((_ excluded: TableAlias) -> [ColumnAssignment])?
) throws -> Self Executes an
INSERT ON CONFLICT DO UPDATE RETURNING
statement, and returns the upserted record.func upsertAndFetch<T>(Database, as: T.Type, onConflict: [String], doUpdate: ((_ excluded: TableAlias) -> [ColumnAssignment])?
) throws -> T Executes an
INSERT ON CONFLICT DO UPDATE RETURNING
statement, and returns the upserted record.
Saving a Record
func save(Database, onConflict: Database.ConflictResolution?
) throws Executes an
INSERT
orUPDATE
statement.
Saving a Record and Fetching the Saved Row
func saveAndFetch<T>(Database, onConflict: Database.ConflictResolution?, as: T.Type
) throws -> T Executes an
INSERT RETURNING
orUPDATE RETURNING
statement, and returns a new record built from the saved row.func saveAndFetch<T>(Database, onConflict: Database.ConflictResolution?, selection: [any SQLSelectable], fetch: (Statement) throws -> T
) throws -> T Executes an
INSERT RETURNING
orUPDATE RETURNING
statement, and returns the selected columns from the saved row.
Persistence Callbacks
func willInsert(Database
) throws Persistence callback called before the record is inserted.
func didInsert(InsertionSuccess
) Persistence callback called upon successful insertion.