Fork me on GitHub
#datomic
<
2023-05-18
>
Can09:05:23

(I am new in Clojure and Datomic) Hello everyone, In this function my transact functionalities are not working. (ı just separated codes in another namespace to make my code more readable and clean then after that I started facing that problem). When I run transact functions manually in other namespaces where my db stands then everything works fine. I think my sell-all-items-in-cart function is wrong or maybe "for" is wrong. Can anyone take a look, please? Thanks for separeting time.

Can09:05:00

I guess my text snippet does not show the code. So let me copy it here.

(identity @!my-cart)
;=> [["bariscan" "mammut" 3] ["bariscan" "lacoste" 7] ["bariscan" "mammut" 1] ["bariscan" "canada goose" 3]]
(for [[username label order-size] @!my-cart]
  (pr username label order-size)
  )
;"bariscan" "mammut" 3"bariscan" "lacoste" 7"bariscan" "mammut" 1"bariscan" "canada goose" 3=> (nil nil nil nil)

(defn sell-all-items-in-cart [db conn]
  (def db (d/db conn))                                      ;;refresh database
  (for [[username label order-size] @!my-cart]
    (if (>= (stock-check-by-label db label) order-size)
      (if (user-validation-check-by-id db (get-user-id-by-username db username))
        ((d/transact conn {:tx-data [{:stock/product (get-entity-id-by-label db label)
                                      :stock/amount  (- (stock-check-by-label db label) order-size)}]})
         (d/transact conn {:tx-data [{:order/product (get-entity-id-by-label db label)
                                      :order/user    (get-user-entity-id-by-userid db (get-user-id-by-username db username))
                                      :order/size    order-size}]}))
        (print "UNKNOWN USER!!")
        )
      (print "OUT OF STOCK!!
    Stock size is: " (stock-check-by-label db label))
      )
    )
  (swap! !my-cart remove-all-elements-in-cart)
  (def db (d/db conn))                                      ;;refresh database
  )

Can09:05:57

all of small functions inside code is working, I tested everything one by one.

chrisetheridge09:05:38

for is lazy, so any side effecting functions (like d/transact) won’t run until you realise the sequence for produces. you can use doseq in this instance, or wrap your for in doall

(doall
   (for [... ...]))
you also have an additional parenthesis wrapping your first d/transact call which will break when the seq is realised 🙂

Can09:05:24

Hello, thank you for your answer. I going to work on your advice. Secondary, which parenthesis is wrong still cant find it. I used "if" check, so that's why I added one more parenthesis to "true block".

(if (user-validation-check-by-id db (get-user-id-by-username db username))
  (;true block
(d/transact conn {:tx-data [{:stock/product (get-entity-id-by-label db label)
                                :stock/amount  (- (stock-check-by-label db label) order-size)}]})
   (d/transact conn {:tx-data [{:order/product (get-entity-id-by-label db label)
                                :order/user    (get-user-entity-id-by-userid db (get-user-id-by-username db username))
                                :order/size    order-size}]}))
  (;false block
print "UNKNOWN USER!!")
  )

chrisetheridge10:05:20

ah yeah, what you need to use there is (do (d/transact …) …), instead of the additional parenthesis around (; true block …)

Can13:05:55

Understand, everything works as I expected 🙂 Thanks!

shane18:05:04

Are there any plans to move ions deps to maven? For some reason I assumed opening up datomic-cloud would include ions but that doesn't seem to be the case as I don't see them there yet.

🙏 4
jaret19:05:47

Datomic Cloud is still being Freed. We're moving in that direction, but have some decisions and work to do

👍 7
fmnoise20:05:17

Great news, thanks guys! 🎉 I was waiting for these fixes to start transition to new version 🚀