Last active
August 29, 2015 14:27
-
-
Save gigasquid/92a1effb14fe4f6ced59 to your computer and use it in GitHub Desktop.
Conversation transcript of http://gigasquidsoftware.com/blog/2015/08/15/conversations-with-datomic/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns conversations.datomic | |
(require [datomic.api :as d])) | |
;; Hi Datomic! I have been hearing good things about you. I would | |
;; like to talk to you and get to know you is that alright? | |
;; Sure - I would be happy to have a conversation with you. | |
(def uri "datomic:mem://first-conversation") | |
(d/create-database uri) | |
(def conn (d/connect uri)) | |
(def dog-schema [{:db/id (d/tempid :db.part/db) | |
:db/ident :dog/name | |
:db/valueType :db.type/string | |
:db/cardinality :db.cardinality/one | |
:db/unique :db.unique/identity | |
:db/doc "Name of the Dog" | |
:db.install/_attribute :db.part/db} | |
{:db/id (d/tempid :db.part/db) | |
:db/ident :dog/breed | |
:db/valueType :db.type/string | |
:db/cardinality :db.cardinality/one | |
:db/doc "Breed of the Dog" | |
:db.install/_attribute :db.part/db} | |
{:db/id (d/tempid :db.part/db) | |
:db/ident :dog/favorite-treat | |
:db/valueType :db.type/string | |
:db/cardinality :db.cardinality/one | |
:db/doc "Dog's Favorite Treat to Eat" | |
:db.install/_attribute :db.part/db}]) | |
(d/transact conn dog-schema) | |
(def owner-schema [{:db/id (d/tempid :db.part/db) | |
:db/ident :owner/name | |
:db/valueType :db.type/string | |
:db/cardinality :db.cardinality/one | |
:db/unique :db.unique/identity | |
:db/doc "Name of the Owner" | |
:db.install/_attribute :db.part/db} | |
{:db/id (d/tempid :db.part/db) | |
:db/ident :owner/dogs | |
:db/valueType :db.type/ref | |
:db/cardinality :db.cardinality/many | |
:db/doc "Dogs of the Owner" | |
:db.install/_attribute :db.part/db}]) | |
(d/transact conn owner-schema) | |
(d/transact conn [{:db/id (d/tempid :db.part/user) | |
:owner/name "Bob" | |
:owner/dogs [{:db/id (d/tempid :db.part/user) | |
:dog/name "Fluffy" | |
:dog/breed "Poodle" | |
:dog/favorite-treat "Cheese"} | |
{:db/id (d/tempid :db.part/user) | |
:dog/name "Fido" | |
:dog/breed "Mix" | |
:dog/favorite-treat "Bone"}]} | |
{:db/id (d/tempid :db.part/user) | |
:owner/name "Lucy" | |
:owner/dogs [{:db/id (d/tempid :db.part/user) | |
:dog/name "Tiny" | |
:dog/breed "Great Dane" | |
:dog/favorite-treat "Cheese"}]}]) | |
(d/pull (d/db conn) '[*] [:dog/name "Tiny"]) | |
;; -> {:db/id 17592186045424, :dog/name "Tiny", :dog/breed "Great | |
;; Dane", :dog/favorite-treat "Cheese"} | |
(d/q '[:find ?owner-name | |
:where [?dog :dog/name "Tiny"] | |
[?owner :owner/dogs ?dog] | |
[?owner :owner/name ?owner-name]] | |
(d/db conn)) | |
;; -> #{["Lucy"]} | |
(d/q '[:find ?owner-name | |
:in $ ?dog-name | |
:where [?dog :dog/name ?dog-name] | |
[?owner :owner/dogs ?dog] | |
[?owner :owner/name ?owner-name]] | |
(d/db conn) "Tiny") | |
;; -> #{["Lucy"]} | |
(d/q '[:find [(pull ?dog [:dog/name :dog/breed]) ...] | |
:where [?dog :dog/favorite-treat "Cheese"]] | |
(d/db conn)) | |
;; -> [{:dog/name "Fluffy", :dog/breed "Poodle"} | |
;; {:dog/name "Tiny", :dog/breed "Great Dane"}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment