FetchableRecord
A type that can decode itself from a database row.
protocol FetchableRecord
Browse conforming typesTo conform to FetchableRecord
, provide an implementation for the init(row:)
initializer. This implementation is ready-made for Decodable
types.
For example:
struct Player: FetchableRecord, Decodable {
var name: String
var score: Int
}
if let row = try Row.fetchOne(db, sql: "SELECT * FROM player") {
let player = try Player(row: row)
}
If you add conformance to TableRecord
, the record type can generate SQL queries for you:
struct Player: FetchableRecord, TableRecord, Decodable {
var name: String
var score: Int
}
let players = try Player.fetchAll(db)
let players = try Player.order(Column("score")).fetchAll(db)
Initializers
init(row: Row
) throws Creates a record from
row
.
Fetching Records
static func fetchCursor(Database
) throws -> RecordCursor<Self> Returns a cursor over all records fetched from the database.
static func fetchAll(Database
) throws -> [Self] Returns an array of all records fetched from the database.
static func fetchSet(Database
) throws -> Set<Self> Returns a set of all records fetched from the database.
static func fetchOne(Database
) throws -> Self? Returns a single record fetched from the database.
Fetching Records from Raw SQL
static func fetchCursor(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> RecordCursor<Self> Returns a cursor over records fetched from an SQL query.
static func fetchAll(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> [Self] Returns an array of records fetched from an SQL query.
static func fetchSet(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> Set<Self> Returns a set of records fetched from an SQL query.
static func fetchOne(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> Self? Returns a single record fetched from an SQL query.
Fetching Records from a Prepared Statement
static func fetchCursor(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> RecordCursor<Self> Returns a cursor over records fetched from a prepared statement.
static func fetchAll(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> [Self] Returns an array of records fetched from a prepared statement.
static func fetchSet(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> Set<Self> Returns a set of records fetched from a prepared statement.
static func fetchOne(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> Self? Returns a single record fetched from a prepared statement.
Fetching Records from a Request
static func fetchCursor(Database, some FetchRequest
) throws -> RecordCursor<Self> Returns a cursor over records fetched from a fetch request.
static func fetchAll(Database, some FetchRequest
) throws -> [Self] Returns an array of records fetched from a fetch request.
static func fetchSet(Database, some FetchRequest
) throws -> Set<Self> Returns a set of records fetched from a fetch request.
static func fetchOne(Database, some FetchRequest
) throws -> Self? Returns a single record fetched from a fetch request.
Fetching Records by Primary Key
static func fetchCursor(Database, ids: some Collection<ID>
) throws -> RecordCursor<Self> Returns a cursor over records identified by their primary keys.
static func fetchAll(Database, ids: some Collection<ID>
) throws -> [Self] Returns an array of records identified by their primary keys.
static func fetchSet(Database, ids: some Collection<ID>
) throws -> Set<Self> Returns a set of records identified by their primary keys.
static func fetchOne(Database, id: ID
) throws -> Self? Returns the record identified by a primary key.
static func fetchCursor(Database, keys: some Collection<some DatabaseValueConvertible>
) throws -> RecordCursor<Self> Returns a cursor over records identified by their primary keys.
static func fetchAll(Database, keys: some Collection<some DatabaseValueConvertible>
) throws -> [Self] Returns an array of records identified by their primary keys.
static func fetchSet(Database, keys: some Collection<some DatabaseValueConvertible>
) throws -> Set<Self> Returns a set of records identified by their primary keys.
static func fetchOne(Database, key: some DatabaseValueConvertible
) throws -> Self? Returns the record identified by a primary key.
static func find(Database, id: ID
) throws -> Self Returns the record identified by a primary key, or throws an error if the record does not exist.
static func find(Database, key: some DatabaseValueConvertible
) throws -> Self Returns the record identified by a primary key, or throws an error if the record does not exist.
static func find(Database, key: [String : (any DatabaseValueConvertible)?]
) throws -> Self Returns the record identified by a unique key (the primary key or any key with a unique index on it), or throws an error if the record does not exist.
Fetching Record by Key
static func fetchCursor(Database, keys: [[String : (any DatabaseValueConvertible)?]]
) throws -> RecordCursor<Self> Returns a cursor over records identified by the provided unique keys (primary key or any key with a unique index on it).
static func fetchAll(Database, keys: [[String : (any DatabaseValueConvertible)?]]
) throws -> [Self] Returns an array of records identified by the provided unique keys (primary key or any key with a unique index on it).
static func fetchSet(Database, keys: [[String : (any DatabaseValueConvertible)?]]
) throws -> Set<Self> Returns a set of records identified by the provided unique keys (primary key or any key with a unique index on it).
static func fetchOne(Database, key: [String : (any DatabaseValueConvertible)?]?
) throws -> Self? Returns the record identified by a unique key (the primary key or any key with a unique index on it).
Configuring Row Decoding for the Standard Decodable Protocol
static var databaseColumnDecodingStrategy: DatabaseColumnDecodingStrategy
The strategy for converting column names to coding keys.
databaseDataDecodingStrategy(for:)
databaseDateDecodingStrategy(for:)
databaseJSONDecoder(for:)-7lmxd
static var databaseDecodingUserInfo: [CodingUserInfoKey : Any]
Contextual information made available to the
Decodable.init(from:)
initializer.enum DatabaseColumnDecodingStrategy
DatabaseColumnDecodingStrategy
specifies howFetchableRecord
types that also adopt the standardDecodable
protocol look for the database columns that match their coding keys.enum DatabaseDataDecodingStrategy
DatabaseDataDecodingStrategy
specifies howFetchableRecord
types that also adopt the standardDecodable
protocol decode theirData
properties.enum DatabaseDateDecodingStrategy
DatabaseDateDecodingStrategy
specifies howFetchableRecord
types that also adopt the standardDecodable
protocol decode theirDate
properties.
Supporting Types
class RecordCursor<Record>
A cursor of records.
class FetchableRecordDecoder
An object that decodes fetchable records from database rows.