Om/Next 中的规范化和标识

Normalization and Idents in Om/Next

我正在努力解决规范化问题,并认为我正在取得进展,但我又犯了错误,我不确定我是否没有正确思考这个问题。如何规范当前用户的消息?

(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}}

我稍微更改了您的初始数据并设法让 tree->db 使我们看起来合理 default-db-format,其中 IDents 无处不在:

{: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]]}}}

组件:

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

和初始数据(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"}]})