Skip to content

Commit

Permalink
Added nested? flag to the entity function
Browse files Browse the repository at this point in the history
  • Loading branch information
Paula Gearon committed Nov 9, 2020
1 parent 98e42a4 commit b564627
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject org.clojars.quoll/asami "1.2.5"
(defproject org.clojars.quoll/asami "1.2.6"
:description "An in memory graph store for Clojure and ClojureScript"
:url "http://github.com/threatgrid/asami"
:license {:name "Eclipse Public License"
Expand All @@ -7,7 +7,7 @@
[org.clojure/clojurescript "1.10.773"]
[prismatic/schema "1.1.12"]
[org.clojure/core.cache "0.8.2"]
[org.clojars.quoll/zuko "0.3.1"]]
[org.clojars.quoll/zuko "0.3.2"]]
:plugins [[lein-cljsbuild "1.1.7"]
[cider/cider-nrepl "0.24.0"]]
:cljsbuild {
Expand Down
7 changes: 4 additions & 3 deletions src/asami/memory.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
(since [this t] (since* this t))
(since-t [this] (since-t* this))
(graph [this] (graph* this))
(entity [this id] (entity* this id)))
(entity [this id] (entity* this id false))
(entity [this id nested?] (entity* this id nested?)))

;; name is the name of the database
;; state is an atom containing:
Expand Down Expand Up @@ -158,8 +159,8 @@
"Returns an entity based on an identifier, either the :db/id or a :db/ident if this is available. This eagerly retrieves the entity.
Objects may be nested, but references to top level objects will be nil in order to avoid loops."
;; TODO create an Entity type that lazily loads, and references the database it came from
[{graph :graph :as db} id]
[{graph :graph :as db} id nested?]
(if-let [ref (or (and (seq (gr/resolve-triple graph id '?a '?v)) id)
(ffirst (gr/resolve-triple graph '?e :db/ident id)))]
(reader/ref->entity graph ref)))
(reader/ref->entity graph ref nested?)))

2 changes: 1 addition & 1 deletion src/asami/storage.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(since [this t] "Retrieves a database since a given moment, exclusive")
(since-t [this] "Returns the since point for a database")
(graph [this] "Returns the internal graph for the database")
(entity [this id] "Returns an entity for an identifier"))
(entity [this id] [this id nested?] "Returns an entity for an identifier"))

(def DatabaseType (s/pred #(satisfies? Database %)))
(def ConnectionType (s/pred #(satisfies? Connection %)))
Expand Down
34 changes: 34 additions & 0 deletions test/asami/test_api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,40 @@
:rooms ["Room 1" nil "Room 2" nil "Room 3"]}
(entity d one)))))

(deftest test-entity-nested
(let [c (connect "asami:mem://test4b")
d1 {:db/id -1
:db/ident "nested-object"
:name "nested"}
d2 {:db/id -2
:name "nested2"}
data {:db/id -3
:db/ident "top"
:name "Main"
:sub {:db/ident "nested-object"}}
data2 {:db/id -4
:db/ident "top2"
:name "Main2"
:sub {:db/id -2}}
r0 @(transact c [d1])
{:keys [tempids tx-data] :as r} @(transact c [d2 data data2])
dx (tempids -2)
one (tempids -3)
two (tempids -4)
d (db c)]
(is (= {:name "Main"
:sub {:db/ident "nested-object"}}
(entity d one)))
(is (= {:name "Main"
:sub {:name "nested"}}
(entity d one true)))
(is (= {:name "Main2"
:sub {:db/ident dx}}
(entity d two)))
(is (= {:name "Main2"
:sub {:name "nested2"}}
(entity d two true)))))

(defn sleep [msec]
#?(:clj
(Thread/sleep msec)
Expand Down

0 comments on commit b564627

Please sign in to comment.