I’m trying to get my head around normalization and thought I was making progress, but I’ve stumbled again and am not sure if I’m just not thinking correctly about the problem. How do I normalize the current user’s messages?
(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 "[email protected]"}
{:user/id 2 :email "[email protected]"}]})
(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}])))
=> (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 "[email protected]", :messages [{:message/id 1}]},
2 {:user/id 2, :email "[email protected]"}},
:om.next/tables #{:message/by-id :user/by-id}}
I changed your initial data a little and managed to get
tree->db
to get us to a sensible looking default-db-format, where Idents are ubiquitous:The components:
And the initial data (the input to
tree->db
):