Skip to content

Commit

Permalink
delete
Browse files Browse the repository at this point in the history
  • Loading branch information
lilactown committed Feb 21, 2021
1 parent 41dffaa commit 1833dd4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG.md

## UNRELEASED

### Added

* `delete`, which dissocs an entity from the db and removes all references to it

## 1.0.2

### Added
Expand Down
38 changes: 34 additions & 4 deletions src/autonormal/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
(lookup-ref-of v)

(map? v) ;; map not an entity
(into (empty v) (map (juxt
first
(comp replace-all-nested-entities second)))
v)
(into
(empty v)
(map
(juxt first
(comp replace-all-nested-entities second)))
v)

(and (coll? v) (every? entity-map? v))
(into (empty v) (map lookup-ref-of) v)
Expand Down Expand Up @@ -314,3 +316,31 @@
(defn pull
[db query]
(:data (pull-report db query)))


(defn- delete-nested-entity
[lookup-ref v]
(cond
(map? v) (into
(empty v)
(comp
(filter #(not= lookup-ref (val %)))
(map
(juxt key #(delete-nested-entity lookup-ref (val %)))))
v)

(coll? v) (into
(empty v)
(comp
(filter #(not= lookup-ref %))
(map #(delete-nested-entity lookup-ref %)))
v)

:else v))


(defn delete
[db lookup-ref]
(delete-nested-entity
lookup-ref
(update db (first lookup-ref) dissoc (second lookup-ref))))
15 changes: 12 additions & 3 deletions test/autonormal/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
(def data
{:people/all [{:person/id 0
:person/name "Alice"
:person/age 17
:person/age 25
:best-friend {:person/id 1}
:person/favorites
{:favorite/ice-cream "vanilla"}}
Expand Down Expand Up @@ -158,7 +158,7 @@
"ident acts as ref lookup")
(t/is (= {[:person/id 0] {:person/id 0
:person/name "Alice"
:person/age 17
:person/age 25
:best-friend {:person/id 1}
:person/favorites #:favorite{:ice-cream "vanilla"}}}
(a/pull db [[:person/id 0]]))
Expand Down Expand Up @@ -435,9 +435,18 @@
"ident acts as ref lookup")
(t/is (= {:data {[:person/id 0] {:person/id 0
:person/name "Alice"
:person/age 17
:person/age 25
:best-friend {:person/id 1}
:person/favorites #:favorite{:ice-cream "vanilla"}}}
:entities #{[:person/id 0]}}
(a/pull-report db [[:person/id 0]]))
"ident does not resolve nested refs"))


(t/deftest delete
(t/is (= {:people/all [[:person/id 0]]
:person/id {0 {:person/id 0
:person/name "Alice"
:person/age 25
:person/favorites #:favorite{:ice-cream "vanilla"}}}}
(a/delete db [:person/id 1]))))

0 comments on commit 1833dd4

Please sign in to comment.