"the most interesting database you've never heard of"
zilvinasu
“ANYONE WHO STOPS LEARNING IS OLD, WHETHER AT TWENTY OR EIGHTY.” HENRY FORD
entity
attribute
value
transaction
| id | first_name | last_name | |
|---|---|---|---|
| 100 | JSON | McNugget | mcnugget@mailinator.com |
| 101 | Jo | Jenkins | jenkins@mailinator.com |
table: person
[[100 :person/first-name "JSON"]
[100 :person/last-name "McNugget"]
[100 :person/email "mcnugget@mailinator.com"]
[101 :person/first-name "Jo"]
[101 :person/last-name "Jenkins"]
[101 :person/email "jenkins@mailinator.com"]]{;; attribute's unique name
:db/ident :person/first-name
;; one or many
:db/cardinality :db.cardinality/one
;; i.e. string, boolean, long, ref, uuid ...
:db/valueType :db.type/string ;; value type
;; what's the purpose of your attribute?
:db/doc "Person's name"}[{:db/ident :person/first-name
:db/cardinality :db.cardinality/one
:db/valueType :db.type/string
:db/doc "First name of the person"}
{:db/ident :person/last-name
:db/cardinality :db.cardinality/one
:db/valueType :db.type/string
:db/doc "Last name of the person"}
{:db/ident :person/email
:db/cardinality :db.cardinality/one
:db/valueType :db.type/string
:db/doc "Email of the person"}](d/transact
conn
[{:db/ident :person/first-name
:db/cardinality :db.cardinality/one
:db/valueType :db.type/string
:db/doc "First name of the person"}]){:db/unique :db.unique/identity
:db/fulltext true
:db/noHistory true
:db/isComponent true};; pull all of the entity attributes
;; by matching the email
[:find ?email
:where [?e :person/email ?email]];; actual example using the datomic api
(d/q
'[:find ?email
:where [?e :person/email ?email]]
db)
=> #{["jenkins@mailinator.com"] ["mcnugget@mailinator.com"]}(d/pull
(d/db conn)
[:person/last-name :person/first-name]
[:email "jenkins@mailinator.com"])
=> #:person{:last-name "Jenkins", :first-name "Jo"};; getting lazy values
(d/entity
(d/db conn)
[:person/email "jenkins@mailinator.com"])
=> #:db{:id 17592186045419}
(d/entity (d/db conn) 17592186045419)
=> #:db{:id 17592186045419};; lower-level list-based
; List of format [command e a v]
; Command is either :db/add or :db/retract
[[:db/add "gm-tid" :person/first-name "JSON"]
[:db/add "gm-tid" :person/last-name "McNugget"]
[:db/add "gm-tid" :person/email "mcnugget@mailinator.com"]]
;; higher-level map-based
[{:db/id 100
:person/first-name "JSON"
:person/last-name "McNugget"
:person/email "mcnugget@mailinator.com"}];; in schema
{:db/ident :person/normalize-last-name
:db/fn #db/fn
{:lang "clojure"
:params [db email]
:code
(let [person (d/entity db [:person/email email])
normalized (clojure.string/upper-case (:person/last-name person))]
[{:db/id (:db/id person)
:person/last-name normalized}])}
;; make use of that
(d/transact conn [[:person/normalize-last-name "mcnugget@mailinator.com"]])
=>
#object[datomic.promise$settable_future$reify__4751
0x230b0540
{:status :ready,
:val {:db-before datomic.db.Db, @e7f03ab :db-after, datomic.db.Db @106f37f,
:tx-data [#datom[17592186045419 64 "MCNUGGET" 13194139534317 true]
#datom[17592186045419 64 "McNugget" 13194139534317 false]],
:tempids {}}}]🏷 We're hiring!