3

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 "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}])))

=> (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}}
Adam Groves
  • 1,295
  • 2
  • 8
  • 6
  • 1
    I've written a function called `check` that checks whether default-db-format has been reached. I could run it with your example, but I would need `init-data`. Can you put `init-data` in a gist or something? I can look at it tomorrow (late here now). – Chris Murphy Feb 22 '16 at 13:23
  • Sorry Chris - forgot that part! It's in the question now (here it is in all it's glory as a gist too: https://gist.github.com/addywaddy/2b2ecf4ca389afb0d202). – Adam Groves Feb 22 '16 at 14:07
  • I see from the state that a user has an email and a message has text. I would expect the components to reflect this i.e. User component to have :email in the query. Instead I see a reference to Property component, which is not in your question (as a component or in the state). – Chris Murphy Feb 22 '16 at 22:19
  • I may be wrong but if you look at other examples (for instance Kanban demo), you will see that ids are unique across all - so if you have a user with an id of 1, then the first message should have a different id, say 100. – Chris Murphy Feb 22 '16 at 23:57
  • Where `check` function is kept: https://github.com/chrismurrph/default-db-format – Chris Murphy Feb 07 '17 at 19:13

1 Answers1

3

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:

{:app/session [:session/by-id 1],
 :app/messages [[:message/by-id 100] [:message/by-id 101]],
 :app/users [[:user/by-id 200] [:user/by-id 201]],
 :message/by-id
            {100 {:id 100, :text "Message 1"}, 101 {:id 101, :text "Message 2"}},
 :user/by-id
            {200 {:id 200, :email "1@foo.com"},
             201 {:id 201, :email "2@foo.com"}},
 :session/by-id {1 {:id 1, :app/messages [[:message/by-id 100]]}}}

The components:

(defui Message
  static om/Ident
  (ident [this {:keys [id]}]
    [:message/by-id id])
  static om/IQuery
  (query [this]
    [:id :text]))

(defui User
  static om/Ident
  (ident [this {:keys [id]}]
    [:user/by-id id])
  static om/IQuery
  (query [this]
    [:id :email]))

(defui Session
  static om/Ident
  (ident [this {:keys [id]}]
    [:session/by-id id])
  static om/IQuery
  (query [this]
    [:id {:app/messages (om/get-query Message)}]))

(defui RootView
  static om/IQuery
  (query [this]
    [{:app/messages (om/get-query Message)}
     {:app/users (om/get-query User)}
     {:app/session (om/get-query Session)}]))

And the initial data (the input to tree->db):

(def init-data
  {:app/session {:id 1
                 :app/messages [{:id 100}]}
   :app/messages [{:id 100 :text "Message 1"}
                  {:id 101 :text "Message 2"}]
   :app/users [{:id 200 :email "1@foo.com"}
               {:id 201 :email "2@foo.com"}]})
Chris Murphy
  • 6,411
  • 1
  • 24
  • 42