Skip to content
Josh Wright edited this page Jan 14, 2021 · 4 revisions

Model

An ActiveRecord-esque type used for modeling a table in a relational database. Contains many extensions for making database queries, supporting relationships & much more.

public protocol Model: Identifiable, ModelMaybeOptional

Inheritance

Identifiable, ModelMaybeOptional

Requirements

id

The identifier / primary key of this type.

var id: Self.Identifier?

tableName

The table with which this object is associated. Defaults to String(describing:​ Self.self) aka the name of the type. Can be overridden for custom table names.

var tableName: String
struct User: Model {
    static var tableName: String = "my_user_table"

    var id: Int?
    let name: String
    let email: String
}

keyMappingStrategy

How should the Swift CodingKeys be mapped to database columns? Defaults to .convertToSnakeCase. Can be overridden on a per-type basis.

var keyMappingStrategy: DatabaseKeyMappingStrategy

belongsToColumnSuffix

When mapping a Model to an SQL table, @BelongsTo properties will have their property names suffixed by this String. Defaults to Id. This value is affected by keyMappingStrategy, so if the keyMappingStrategy of the database is .convertToSnakeCase, the default suffix will effectively be _id.

var belongsToColumnSuffix: String

Usage:

struct User: Model {
    ...
    @BelongsTo
    var parent: User // will map to column `parentId` of type
                     // `User.ID`.
}

jsonDecoder

The JSONDecoder to use when decoding any JSON fields of this type. A JSON field is any Codable field that doesn't have a corresponding DatabaseValue.

var jsonDecoder: JSONDecoder

Defaults to JSONDecoder().

jsonEncoder

The JSONEncoder to use when decoding any JSON fields of this type. A JSON field is any Codable field on this type that doesn't have a corresponding DatabaseValue.

var jsonEncoder: JSONEncoder

Defaults to JSONEncoder().

Alchemy
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Fusion
Types
Protocols
Papyrus
Types
Protocols
Clone this wiki locally