Search code examples
htmldatabaseclojureclojurescriptdatascript

How do I fill and display a list by Clojure Datascript?


i'm full my db with this

(def fixtures [
  [:db/add 0 :system/group :all]

  {
  :product/name "Donut Keurig"
  :product/category "snack"
  :product/brand "Grocery&GourmetFood"
  :product/height "2.1"
  :product/width "3.2"
  :product/notes "The Original Donut Shop Keurig Single-Serve K-Cup Pods, Regular Medium Roast Coffee"
  }

  {
  :product/name "Ferrero Rocher Hazelnut Chocolates"
  :product/category "Candy"
  :product/brand "Candy&Chocolate"
  :product/height "3.4"
  :product/width "2"
  :product/notes "A tempting combination of smooth chocolaty cream surroiunding a whole hazelnut within a delciate, crisp wafer all enveloped in milk chocolate and finely chopped hazelnuts"
  }
  ])

(def conn (d/transact! conn u/fixtures))

but show me this error:

Uncaught Error: Assert failed: (conn? conn) at Function.datascript.core.transact_BANG_.cljs$core$IFn$_invoke$arity$3

Besides i want to show the result of the db like this:

(defmulti read om/dispatch)

(defmethod read :product/name
  [{:keys [state query]} _ _]
  {:value (d/q '[:find [(pull ?e ?selector) ...]
                 :in $ ?selector
                 :where [?e :product/name]]
            (d/db state) query)})

(defui product-view
  static om/IQuery
  (query [this] [{:product/name [:db/id :product/name]}])
  Object
    (render [this]
      (let [{:keys [product/name] :as entity}
            (get-in (om/props this) [:product/name ""])]
        (dom/tr nil
          (dom/td nil name)))))

(defui products-view
  static om/IQuery
  (query [this] [{:product/name [:db/id :product/name]}])
  Object
    (render [this]
      (dom/table #js {:className "table table-bordered"}
        (dom/thead nil
          (dom/tr #js {:className "row-garden"}
            (dom/th nil "Product Name")
            (dom/th nil "Category")
            (dom/th nil "Brand")
            (dom/th nil "Height")
            (dom/th nil "Width")
            (dom/th nil "Notes")))
        (dom/tbody
          (let [{:keys [product/name] :as entity}
            (get-in (om/props this) [:product/name ""])]
            (println conn)
             (dom/tr nil
              (dom/td nil name)))))))

(om/add-root!
  reconciler
  products-view
  (gdom/getElement "table-products"))

But doesn't work :(

Thanks!


Solution

  • Author of DataScript here.

    First, transact! returns transaction report, not a connection. It mutates content of conn passed in as an argument:

    (def conn (d/create-conn schema))
    ...
    (d/transact! conn u/fixtures)
    

    Second, you can’t use dynamically-bound pull pattern in query. Use normal pull API:

    (let [db     (d/db state)
          datoms (d/datoms db :aevt :product/name)
          eids   (map :e datoms)]
      (d/pull-many db query eids))
    

    I can’t help you with Om.Next part, unfortunately. If there are any errors, I wouldn’t know.

    Hope it helps.