Defining Schema Attributes

Schema attributes are defined using the same data model used for application data. That is, schema attributes are themselves defined by entities with associated attributes.

The following built-in attributes are required to define a new schema attribute:

  • :db/ident, a name that is unique within the database
  • :db/cardinality, specifying whether entities can have one or a set of values for the attribute
  • :db/type, the type allowed for an attribute's value

In addition to these required attributes, there are many optional attributes that can be used to further define your schema attributes. For a full list of available built-in attributes, see the Schema Data Reference.

You define new schema attributes by submitting a data structure to Datomic as part of a transaction. All the required information for an attribute must be specified in a single transaction, after which the attribute will be immediately available for use.

It is idiomatic to use the transaction map data structure to define schema attributes. The example below defines an attribute named :person/name of type :db.type/string with :db.cardinality/one that is intended to capture a person's name.

(def name-schema
  {:db/ident :person/name
   :db/valueType :db.type/string
   :db/cardinality :db.cardinality/one
   :db/doc "A person's name"})

;; using a connection named conn:
(d/transact conn {:tx-data name-schema})

;; now you can transact names
(d/transact conn {:tx-data [{:person/name "Grace Hopper"}]})

Next: Schema Reference