Skip to content

Instantly share code, notes, and snippets.

@matthavener
Last active March 8, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthavener/2d2910222c1221b719ea to your computer and use it in GitHub Desktop.
Save matthavener/2d2910222c1221b719ea to your computer and use it in GitHub Desktop.
om.next normalization with links
(ns testcase
(:require
[devcards.core :as dc :refer-macros [defcard defcard-doc defcard-om-next]]
[om.next :as om :refer-macros [defui]]
[cljs.test :refer-macros [is testing async]])
)
(defui Child
static om/Ident
(ident [_ props]
[:db/id (get props :db/id)])
static om/IQuery
(query [_]
[:db/id :name]))
(defui Children
static om/IQuery
(query [_]
[{:kids (om/get-query Child)}]))
(defui Parent
static om/IQuery
(query [_]
[{[:children '_] (om/get-query Children)}]))
(defui App
static om/IQuery
(query [this]
[:parent (om/get-query Parent)]))
(dc/deftest app-test
(testing "App"
(testing "tree->db"
(om/get-query App)
(let [r (om/tree->db App {:parent {:children {:kids [{:db/id 1 :name "matt"} {:db/id 2 :name "chris"}]}}} true)]
(is (= r {:parent {:children [:children '_]}
:children [[:db/id 1] [:db/id 2]]
:db/id {1 {:db/id 1 :name "matt"} 2 {:db/id 2 :name "chris"}}
:om.next/tables #{:db/id}
}))))
(comment
{:parent
{:children
{:kids [{:db/id 1, :name "matt"} {:db/id 2, :name "chris"}]}},
:om.next/tables #{}})
(testing "db->tree"
(let [st {:parent {:children [:children '_]}
:children {:kids [[:db/id 1] [:db/id 2]]}
:db/id {1 {:db/id 1 :name "matt"} 2 {:db/id 2 :name "chris"}}}
r (om/db->tree (om/get-query App) st st)]
(is (= r {:parent {:children [{:db/id 1 :name "matt"} {:db/id 2 :name "chris"}]}}))))
(comment
{:parent {:children [:children _]}})
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment