Documentation

TypeDB.ORM

A lightweight ORM #

deriving Entity and deriving Relation map a Lean structure to a TypeDB entity or relation type (TypeDB.ORM.Deriving). The mapping is Lean-first: the structure is the source of truth, Schema.define generates the TypeQL define for it, and verifySchema checks a live database against it.

Fields map by their type:

A structure that extends a mapped structure maps to a subtype (sub) and inherits its attributes, roles and key. @[typedb "label"] on the structure sets the type label.

Instances are referenced by Ref α: by key value (Ref.key) or by IID (Ref.iid). insert returns an IID reference.

@[reducible, inline]
abbrev TypeDB.Key (α : Type) :

Marks the field holding the type's key attribute. Transparent: a Key String is a String.

Equations
Instances For
    @[reducible, inline]
    abbrev TypeDB.Role (_name : String) (α : Type) :

    Marks a relation field holding the player(s) of role name.

    Equations
    Instances For
      @[reducible, inline]
      abbrev TypeDB.Attr (_label : String) (α : Type) :

      Sets the TypeDB attribute label of a field.

      Equations
      Instances For

        How a reference finds its instance.

        Instances For
          def TypeDB.ORM.instDecidableEqTarget.decEq (x✝ x✝¹ : Target) :
          Decidable (x✝ = x✝¹)
          Equations
          Instances For
            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              structure TypeDB.Ref (α : Type) :

              A reference to an instance of the mapped type α.

              Instances For
                def TypeDB.instDecidableEqRef.decEq {α✝ : Type} [DecidableEq α✝] (x✝ x✝¹ : Ref α✝) :
                Decidable (x✝ = x✝¹)
                Equations
                Instances For
                  def TypeDB.instReprRef.repr {α✝ : Type} [Repr α✝] :
                  Ref α✝ → Nat → Std.Format
                  Equations
                  Instances For
                    @[instance_reducible]
                    instance TypeDB.instReprRef {α✝ : Type} [Repr α✝] :
                    Repr (Ref α✝)
                    Equations
                    @[instance_reducible]
                    instance TypeDB.instInhabitedRef {a✝ : Type} :
                    Inhabited (Ref a✝)
                    Equations
                    def TypeDB.instHashableRef.hash {α✝ : Type} [Hashable α✝] :
                    Ref α✝ → UInt64
                    Equations
                    Instances For
                      @[instance_reducible]
                      instance TypeDB.instHashableRef {α✝ : Type} [Hashable α✝] :
                      Hashable (Ref α✝)
                      Equations
                      @[instance_reducible]
                      instance TypeDB.instBEqRef {α : Type} :
                      BEq (Ref α)
                      Equations
                      Instances For
                        @[instance_reducible]
                        Equations
                        @[instance_reducible]
                        Equations
                        Instances For
                          @[instance_reducible]
                          Equations

                          What a field stores.

                          • attr (valueType : String) (card : Card) : Shape
                          • role (player : String) (many : Bool) : Shape

                            Players of a role; player is the label of the referenced type.

                          Instances For
                            def TypeDB.ORM.instDecidableEqShape.decEq (x✝ x✝¹ : Shape) :
                            Decidable (x✝ = x✝¹)
                            Equations
                            Instances For
                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For

                                One mapped field. name is the attribute label or the role name.

                                Instances For
                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For

                                      A field's contribution to an insert.

                                      Instances For

                                        What a field decodes from: attribute values or role players' IIDs.

                                        Instances For

                                          TypeDB value types of Lean types.

                                          Instances
                                            @[instance_reducible]
                                            Equations
                                            @[instance_reducible]
                                            Equations
                                            @[instance_reducible]
                                            Equations
                                            @[instance_reducible]
                                            Equations
                                            @[instance_reducible]
                                            Equations
                                            @[instance_reducible]
                                            Equations
                                            @[instance_reducible]
                                            Equations
                                            @[instance_reducible]
                                            Equations
                                            @[instance_reducible]
                                            Equations
                                            @[instance_reducible]
                                            Equations

                                            How a field type is stored and read back.

                                            Instances
                                              @[instance_reducible, instance 100]
                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              @[instance_reducible]
                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              @[instance_reducible]
                                              Equations
                                              • One or more equations did not get rendered due to their size.

                                              A Lean type mapped to a TypeDB entity or relation type. Derive it with deriving Entity or deriving Relation. fields, encode and decode cover every field, inherited ones included, in the same order.

                                              Instances
                                                class TypeDB.ORM.Keyed (α : Type) (K : outParam Type) :

                                                Mapped types with a key attribute of type K.

                                                Instances
                                                  @[instance_reducible]
                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  @[instance_reducible]
                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  structure TypeDB.ORM.Field (α β : Type) :

                                                  A typed handle on one attribute field of α, generated as α.field.<name>.

                                                  Instances For
                                                    def TypeDB.Ref.iid {α : Type} (i : IID) :
                                                    Ref α
                                                    Equations
                                                    Instances For
                                                      def TypeDB.Ref.key {α K : Type} [ORM.Mapped α] [ORM.Keyed α K] [ToTypeQL K] (k : K) :
                                                      Ref α

                                                      A reference by key value. Only types with a key attribute have one.

                                                      Equations
                                                      Instances For
                                                        def TypeDB.Ref.iid? {α : Type} (r : Ref α) :
                                                        Equations
                                                        Instances For
                                                          def TypeDB.Ref.up {α β : Type} (r : Ref α) :
                                                          Ref β

                                                          The same instance seen as a supertype.

                                                          Equations
                                                          Instances For

                                                            A TypeQL pattern binding var to the referenced instance of type label.

                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For
                                                              def TypeDB.ORM.insertText {α : Type} [Mapped α] (x : α) :

                                                              The insert query for a value, with match clauses for its role players.

                                                              Equations
                                                              • One or more equations did not get rendered due to their size.
                                                              Instances For
                                                                def TypeDB.ORM.assemble (fields : Array FieldSpec) (attrs : Array Concept) (players : Array (String × IID)) :

                                                                The field data of one instance from its attributes and role players. Fields that share a role take its players in order: a single field one player, an array field the rest.

                                                                Equations
                                                                • One or more equations did not get rendered due to their size.
                                                                Instances For
                                                                  def TypeDB.ORM.fetchRaw {m : TxMode} [m.Allows TxMode.read] (pattern : String) (isRelation : Bool) :

                                                                  The attributes and role players of every instance matched by pattern (which binds $x): two queries, whatever the number of instances.

                                                                  Equations
                                                                  • One or more equations did not get rendered due to their size.
                                                                  Instances For
                                                                    def TypeDB.ORM.fetchInstances {m : TxMode} [m.Allows TxMode.read] (pattern : String) (fields : Array FieldSpec) (isRelation : Bool) :

                                                                    Decodes the instances matched by pattern with the given fields.

                                                                    Equations
                                                                    • One or more equations did not get rendered due to their size.
                                                                    Instances For

                                                                      Helpers the deriving handlers call: each is typed by a field's projection, so generated code never spells out field types.

                                                                      @[reducible, inline]
                                                                      abbrev TypeDB.ORM.TypeOf {σ β : Type} :
                                                                      (σ → β) → Type
                                                                      Equations
                                                                      Instances For
                                                                        def TypeDB.ORM.shapeOf {σ β : Type} [FieldCodec β] :
                                                                        (σ → β) → Shape
                                                                        Equations
                                                                        Instances For
                                                                          def TypeDB.ORM.cardOf {σ β : Type} [FieldCodec β] :
                                                                          (σ → β) → Card
                                                                          Equations
                                                                          • One or more equations did not get rendered due to their size.
                                                                          Instances For
                                                                            def TypeDB.ORM.decodeField {σ β : Type} [FieldCodec β] :
                                                                            (σ → β) → (name : String) → (d : Array FieldData) → (i : Nat) → Except String β
                                                                            Equations
                                                                            • One or more equations did not get rendered due to their size.
                                                                            Instances For
                                                                              def TypeDB.ORM.columnOf {σ β : Type} [FromColumn β] :
                                                                              (σ → β) → (name : String) → RowDecoder β
                                                                              Equations
                                                                              Instances For
                                                                                class TypeDB.Entity (α : Type) :

                                                                                Marks a structure derived as a TypeDB entity type (deriving Entity).

                                                                                Instances
                                                                                  class TypeDB.Relation (α : Type) :

                                                                                  Marks a structure derived as a TypeDB relation type (deriving Relation).

                                                                                  Instances

                                                                                    Operations on mapped types #

                                                                                    def TypeDB.Entity.insert {α : Type} {m : TxMode} [ORM.Mapped α] [m.Allows TxMode.write] (x : α) :
                                                                                    Tx m (Ref α)

                                                                                    Inserts the value and returns an IID reference to it. Fails if a referenced role player does not exist, in which case nothing is inserted.

                                                                                    Equations
                                                                                    • One or more equations did not get rendered due to their size.
                                                                                    Instances For
                                                                                      def TypeDB.Entity.get? {α : Type} {m : TxMode} [ORM.Mapped α] [m.Allows TxMode.read] (r : Ref α) :
                                                                                      Tx m (Option α)

                                                                                      The instance a reference points to, or none.

                                                                                      Equations
                                                                                      • One or more equations did not get rendered due to their size.
                                                                                      Instances For
                                                                                        def TypeDB.Entity.all {α : Type} {m : TxMode} [ORM.Mapped α] [m.Allows TxMode.read] :
                                                                                        Tx m (Array (Ref α × α))

                                                                                        Every instance of the type (and its subtypes), with an IID reference to each.

                                                                                        Equations
                                                                                        • One or more equations did not get rendered due to their size.
                                                                                        Instances For
                                                                                          def TypeDB.Entity.update {α β : Type} {m : TxMode} [ORM.Mapped α] [m.Allows TxMode.write] [ORM.FieldCodec β] (r : Ref α) (f : ORM.Field α β) (v : β) :

                                                                                          Replaces the value(s) of one attribute.

                                                                                          Equations
                                                                                          • One or more equations did not get rendered due to their size.
                                                                                          Instances For
                                                                                            def TypeDB.Entity.delete {α : Type} {m : TxMode} [ORM.Mapped α] [m.Allows TxMode.write] (r : Ref α) :

                                                                                            Deletes the instance.

                                                                                            Equations
                                                                                            • One or more equations did not get rendered due to their size.
                                                                                            Instances For

                                                                                              How many instances of the type (and its subtypes) exist.

                                                                                              Equations
                                                                                              • One or more equations did not get rendered due to their size.
                                                                                              Instances For

                                                                                                Polymorphic decoding #

                                                                                                A sum type whose constructors wrap mapped types, decoded by each instance's concrete type. Derive it with deriving Polymorphic; the first constructor's type is the root that is queried.

                                                                                                Instances

                                                                                                  Every instance of the root type, each decoded into the constructor of its concrete type. Instances of an unmapped subtype decode as the closest constructor listed first.

                                                                                                  Equations
                                                                                                  • One or more equations did not get rendered due to their size.
                                                                                                  Instances For