ToOne

public final class ToOne<T: EntityInspectable & __EntityRelatable>: ExpressibleByNilLiteral
where T == T.EntityBindingType.EntityType
extension ToOne: CustomStringConvertible
extension ToOne: CustomDebugStringConvertible

Object property type to indicate a to-one relation to another object.

Initialize with nil in your type declarations.

  • You can set the value to nil to remove the relation. You can also set target or targetId to nil.
  • You can set target to an object to set the relation. Call Box.put(_:) to persist the changes.
  • You can set targetId to an object’s ID to set the relation. Call Box.put(_:) to persist the changes.

Example

class Person: Entity {
    var spouse: ToOne<Person> = nil
}

let personBox: Box<Person> = store.box(for: Person.self)
let amanda: Person = ...
let neil: Person = ...

amanda.spouse.target = neil
try personBox.put(amanda)

Remove a relation

amanda.spouse.target = nil
// ... or ...
amanda.spouse.targetId = nil
// ... are equivalent to:
amanda.spouse = nil
// ... whis is a short version of:
amanda.spouse = ToOne<Person>(target: nil)
  • The type of object this relation will produce.

    Declaration

    Swift

    public typealias Target = T
  • Whether the relation was set to a target object.

    Declaration

    Swift

    public var hasValue: Bool { get }
  • Access to the relation’s target, if any. Set to nil to remove the relation.

    Alternatively, you can also overwrite the whole relation:

    order.customer.target = nil
    // ... is equivalent to:
    order.customer = nil
    // ... whis is a short version of:
    order.customer = ToOne<Customer>(target: nil)
    

    Note

    Call Box.put(_:) to persist changes.

    Declaration

    Swift

    public var target: Target? { get set }
  • Access to the ID of the relation’s target, if any. Set to nil to remove the relation.

    Alternatively, you can also overwrite the whole relation:

    order.customer.target = nil
    // ... is equivalent to:
    order.customer = nil
    // ... whis is a short version of:
    order.customer = ToOne<Customer>(target: nil)
    

    Note

    Call Box.put(_:) to persist changes.

    Declaration

    Swift

    public var targetId: EntityId<Target>? { get set }
  • Initialize an empty relation.

    Use this during entity creation, like:

    class Order { var customer: ToOne = nil // … }

    Declaration

    Swift

    public required init(nilLiteral: ())
  • Initialize a relation with a target set from the get-go.

    Declaration

    Swift

    public required init(_ entity: Target?)

    Parameters

    entity

    Target entity, or nil if the relation is empty.

  • If this relation’s target has already been persisted, unload the entity so it is re-loaded from the database the next time you ask for the target. Does nothing if the target hasn’t been persisted yet. If you want to reset the target to what is set in the database, get() the entity again instead.

    Declaration

    Swift

    public func reset()