Skip to content

Commit

Permalink
repl-session code to show how Datomic entity API works
Browse files Browse the repository at this point in the history
  • Loading branch information
humorless committed Jul 30, 2024
1 parent 9c9fe70 commit d3d73ad
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
17 changes: 0 additions & 17 deletions repl-sessions/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,6 @@
(db/transact [[:db/retractEntity 17592186045468]]))
(user/conn)

(db/transact [{:user/email "[email protected]"
:user/handle "laurence.chen"
:user/name "Laurence"}])

(db/q
'[:find
[(pull ?e [*]) ...]
:where
[?e :user/email]]
(db/db))

(def session-eid 17592186045438)

(db/transact [{:db/id session-eid
:session/participants 17592186045458}])

(first (:session/participants (db/entity session-eid)))
;; Test transact participants

(def req {:identity {:user/email "ddd"}
Expand Down
66 changes: 66 additions & 0 deletions repl-sessions/init_db.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
(ns repl-sessions.init-db
"Prepare some testing data to transact into db for testing"
(:require
[co.gaiwan.compass.db :as db]
[datomic.api :as d]))

;; Utility

(defn query-session []
(db/q
'[:find
[(pull ?e [*]) ...]
:where
[?e :session/title "Opening Keynote (TBD)"]]
(db/db)))

(defn participants []
(db/q
'[:find
[(pull ?e [*]) ...]
:where
[?e :user/email]]
(db/db)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Test Setup Begin
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; write participants into the database
(db/transact [{:user/email "[email protected]"
:user/handle "laurence.chen"
:user/name "Laurence"}
{:user/email "[email protected]"
:user/handle "sunnyplexus"
:user/name "Arne"}])

;; get the session eid
(def session-eid (:db/id (first (query-session))))

;; get the eids of participants
(def p-eids (mapv :db/id (participants)))

(def tx-data (mapv (fn [x] {:db/id session-eid
:session/participants x}) p-eids))

;; relate the pariticipants to the session
(db/transact tx-data)

;; Simulate the entity access in the frontend
(def session-entity (db/entity session-eid))

;; Get the first participant's name from the session entity
(-> session-entity
:session/participants
first
:user/name)

;; Demonstrate the behaviors of Datomic Entity

(type session-entity)
;; => co.gaiwan.compass.db.munged-entity

(type (-> session-entity
:session/participants
first))
;; => co.gaiwan.compass.db.munged-entity

0 comments on commit d3d73ad

Please sign in to comment.