Model

public protocol Model : Decodable, Encodable

All persistent models should conform to the Model protocol.

  • Alias of Model identifier (i.e. primary key)

    Declaration

    Swift

    @available(*, deprecated, message: "Use ModelIdentifier")
    typealias Identifier = String
  • schema Default implementation

    A reference to the ModelSchema associated with this model.

    Default Implementation

    Warning

    Although this has public access, it is intended for internal & codegen use and should not be used directly by host applications. The behavior of this may change without warning. Though it is not used by host application making any change to these public types should be backward compatible, otherwise it will be a breaking change.

    Declaration

    Swift

    static var schema: ModelSchema { get }
  • rootPath Default implementation

    The reference to the root path. It might be nil if models do not support property references.

    Default Implementation

    The rootPath is set to nil by default. Specific models should override this behavior and provide the proper path reference when available.

    Declaration

    Swift

    static var rootPath: PropertyContainerPath? { get }
  • modelName Default implementation

    The name of the model, as registered in ModelRegistry.

    Default Implementation

    Declaration

    Swift

    static var modelName: String { get }
  • modelName Default implementation

    Convenience property to return the Type’s modelName. Developers are strongly encouraged not to override the instance property, as an implementation that returns a different value for the instance property will cause undefined behavior.

    Default Implementation

    Declaration

    Swift

    var modelName: String { get }
  • identifier(schema:) Default implementation

    For internal use only when a model schema is provided (i.e. calls from Flutter)

    Default Implementation

    Declaration

    Swift

    func identifier(schema: ModelSchema) -> ModelIdentifierProtocol
  • identifier Default implementation

    Convenience property to access the serialized value of a model identifier

    Default Implementation

    Declaration

    Swift

    var identifier: String { get }
  • subscript(_:) Extension method

    Warning

    Although this has public access, it is intended for internal & codegen use and should not be used directly by host applications. The behavior of this may change without warning. Though it is not used by host application making any change to these public types should be backward compatible, otherwise it will be a breaking change.

    Declaration

    Swift

    public subscript(key: String) -> Any?? { get }
  • schema Extension method

    Warning

    Although this has public access, it is intended for internal & codegen use and should not be used directly by host applications. The behavior of this may change without warning. Though it is not used by host application making any change to these public types should be backward compatible, otherwise it will be a breaking change.

    Declaration

    Swift

    public var schema: ModelSchema { get }
  • Utility function that enables a DSL-like ModelSchema definition. Instead of building objects individually, developers can use this to create the schema with a more fluid programming model.

    • Example:

      static let schema = defineSchema { model in
      model.fields(
          .field(name: "title", is: .required, ofType: .string)
      )
      }
      
    • Parameters

      • name: the name of the Model. Defaults to the class name
      • attributes: model attributes (aka “directives” or “annotations”)
      • define: the closure used to define the model attributes and fields

    Warning

    Although this has public access, it is intended for internal & codegen use and should not be used directly by host applications. The behavior of this may change without warning.

    Declaration

    Swift

    public static func defineSchema(name: String? = nil,
                                    attributes: ModelAttribute...,
                                    define: (inout ModelSchemaDefinition) -> Void) -> ModelSchema

    Return Value

    a valid ModelSchema instance

  • Warning

    Although this has public access, it is intended for internal & codegen use and should not be used directly by host applications. The behavior of this may change without warning.

    Declaration

    Swift

    public static func rule(allow: AuthStrategy,
                            ownerField: String? = nil,
                            identityClaim: String? = nil,
                            groupClaim: String? = nil,
                            groups: [String] = [],
                            groupsField: String? = nil,
                            provider: AuthRuleProvider? = nil,
                            operations: [ModelOperation] = []) -> AuthRule

Available where Self: Codable

  • from(json:decoder:) Extension method

    De-serialize a JSON string into an instance of the concrete type that conforms to the Model protocol.

    Throws

    DecodingError.dataCorrupted in case data is not a valid JSON or any other decoding specific error that JSONDecoder.decode() might throw.

    Warning

    Although this has public access, it is intended for internal & codegen use and should not be used directly by host applications. The behavior of this may change without warning. Though it is not used by host application making any change to these public types should be backward compatible, otherwise it will be a breaking change.

    Declaration

    Swift

    public static func from(json: String,
                            decoder: JSONDecoder? = nil) throws -> Self

    Parameters

    json

    a valid JSON object as String

    decoder

    an optional JSONDecoder to use to decode the model. Defaults to JSONDecoder(), using a custom date formatter that decodes ISO8601 dates both with and without fractional seconds

    Return Value

    an instance of the concrete type conforming to Model

  • from(dictionary:) Extension method

    De-serialize a Dictionary into an instance of the concrete type that conforms to the Model protocol.

    Throws

    DecodingError.dataCorrupted in case data is not a valid JSON or any other decoding specific error that JSONDecoder.decode() might throw.

    Warning

    Although this has public access, it is intended for internal & codegen use and should not be used directly by host applications. The behavior of this may change without warning. Though it is not used by host application making any change to these public types should be backward compatible, otherwise it will be a breaking change.

    Declaration

    Swift

    public static func from(dictionary: [String : Any]) throws -> Self

    Parameters

    dictionary

    containing keys and values that match the target type

    Return Value

    an instance of the concrete type conforming to Model

  • toJSON(encoder:) Extension method

    Converts the Model instance to a JSON object as String.

    Warning

    Although this has public access, it is intended for internal & codegen use and should not be used directly by host applications. The behavior of this may change without warning. Though it is not used by host application making any change to these public types should be backward compatible, otherwise it will be a breaking change.

    Declaration

    Swift

    public func toJSON(encoder: JSONEncoder? = nil) throws -> String

    Parameters

    encoder

    an optional JSONEncoder to use to encode the model. Defaults to JSONEncoder(), using a custom date formatter that encodes ISO8601 dates with fractional seconds

    Return Value

    the JSON representation of the Model