Fork me on GitHub
#om
<
2016-09-05
>
mjhamrick02:09:34

I went through the https://github.com/omcljs/om/wiki/DataScript-Integration-Tutorial and found it made a lot of sense. But when trying to write my own code using datascript, i'm running into a lot of confusion when trying to get the pulls to work correctly. Are there other examples out there of using datascript with om-next?

mjhamrick02:09:35

I have a feeling the problem I'm having has something to do with how the idents work, but I'm not sure. I'm trying to have these queries get a list of transactions, but I'm missing something when it comes to how the query and key come in

(defui Transaction
  static om/IQuery
  (query [this]
         [:transaction/date :transaction/account :transaction/payee
          :transaction/memo :transaction/outflow :transaction/inflow]) ...

(defui TransactionView
  static om/IQuery
  (query [this]
         [{:account/transaction (om/get-query Transaction)}]) ...

(defmulti read om/dispatch)
(defmethod read :account/transaction
  [{:keys [state query] :as env} _ _]
  (pprint/pprint [query])
  {:value (d/q '[:find [(pull ?e ?selector) ...]
                 :in $ ?selector
                 :where [?e :account/transaction]]
               (d/db state) query)})

;; And my datascript db, if that helps...
(def conn (d/create-conn {:account/transaction {:db/valueType :db.type/ref
                                                :db/cardinality :db.cardinality/many}}))

(d/transact! conn [{:db/id -1
                    :account/name "My Account"
                    :account/transaction [{:transaction/inflow "100.00"
                                           :transaction/date "yesterday"}
                                          {:transaction/outflow "13.00"
                                           :transaction/date "yesterday"}]}])

mjhamrick02:09:28

I put in a console.log for (om/get-props this) on the Transaction view and I get {:account/transaction [nil]}

urbanslug09:09:17

Is something along these lines allowed?

(om/update! app-state {:foo bar :foobar baz})

urbanslug09:09:58

Where it should mutate the current value if it exists in app-state or create the key value pair if it doesn’t exist.

iwankaramazow10:09:18

@urbanslug: I think you're looking at a combination of swap! (mutate an atom) and assoc (update key in a map)

(defmethod mutate 'mutation/example
  [{:keys [state]} key params]
  {:action #(swap! state (fn [st]
                           (-> st
                               (assoc :foo :bar)
                               (assoc :foobaz :baz))))})

tmulvaney12:09:06

@urbanslug you could also do (swap! app-state merge {:foo :bar :foobar :baz})

mjhamrick14:09:54

@hkjels thanks! I didn't realize you could search github like that.

hkjels17:09:00

@mjhamrick: your welcome 😀

iwankaramazow19:09:16

Figwheel + fix/continue with om/from-history never gets old 😌