Skip to content

Instantly share code, notes, and snippets.

@souenzzo
Created November 18, 2020 14:08
Show Gist options
  • Save souenzzo/ebd049a99443883ebab180ff019400ba to your computer and use it in GitHub Desktop.
Save souenzzo/ebd049a99443883ebab180ff019400ba to your computer and use it in GitHub Desktop.
(let [db-uri (doto "datomic:mem://example"
d/create-database)
conn (d/connect db-uri)
tx-schema [{:db/ident :cart/item
:db/cardinality :db.cardinality/one
:db/valueType :db.type/ref}
{:db/ident :item/name
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity
:db/valueType :db.type/string}]
{:keys [db-after]} @(d/transact conn tx-schema)
tx-initial-data [{:db/id "panetone"
:item/name "Panetone"}
{:db/id "pizza"
:item/name "Pizza"}
{:cart/item "pizza"}]
{:keys [db-after]} @(d/transact conn tx-initial-data)]
(d/q '[:find ?name ?in-cart?
:keys :item/name :item/in-cart?
:where
[?item :item/name ?name]
(or-join [?item ?in-cart?]
(and [_ :cart/item ?item]
[(ground true) ?in-cart?])
(and (not [_ :cart/item ?item])
[(ground false) ?in-cart?]))]
db-after))
=> [{:item/name "Pizza", :item/in-cart? true} {:item/name "Panetone", :item/in-cart? false}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment