Skip to content

Instantly share code, notes, and snippets.

@addywaddy
Created February 22, 2016 12:55
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 addywaddy/2b2ecf4ca389afb0d202 to your computer and use it in GitHub Desktop.
Save addywaddy/2b2ecf4ca389afb0d202 to your computer and use it in GitHub Desktop.
Om next nested idents
(def init-data
{:session {:user/id 1
:messages [{:message/id 1}]}
:messages [{:message/id 1 :text "Message 1"}
{:message/id 2 :text "Message 1"}]
:users [{:user/id 1 :email "1@foo.com"}
{:user/id 2 :email "2@foo.com"}]})
(defui Message
static om/Ident
(ident [this {:keys [message/id]}]
[:message/by-id id])
static om/IQuery
(query [this]
[:id]))
(defui User
static om/Ident
(ident [this {:keys [user/id]}]
[:user/by-id id])
static om/IQuery
(query [this]
`[:id {:properties ~(om/get-query Property)}]))
(defui Session
static om/Ident
(ident [this {:keys [user/id]}]
[:user/by-id id])
static om/IQuery
(query [this]
[:id]))
(defui RootView
static om/IQuery
(query [this]
(let [message-query (om/get-query Message)
user-query (om/get-query User)
session-query (om/get-query Session)]
`[{:messages ~message-query}
{:users ~user-query}
{:session ~session-query}])))
(require '[cljs.pprint :as pp])
(def norm-data (om/tree->db RootView init-data true))
(pp/pprint norm-data)
;; {:session [:user/by-id 1],
;; :messages [[:message/by-id 1] [:message/by-id 2]],
;; :users [[:user/by-id 1] [:user/by-id 2]],
;; :message/by-id
;; {1 {:message/id 1, :text "Message 1"},
;; 2 {:message/id 2, :text "Message 1"}},
;; :user/by-id
;; {1 {:user/id 1, :email "1@foo.com", :messages [{:message/id 1}]},
;; 2 {:user/id 2, :email "2@foo.com"}},
;; :om.next/tables #{:message/by-id :user/by-id}}
;; How do I ensure that message#1 for the current user (session) is linked to the message in the message/by-id table?
;; Or is my thinking here wrong?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment