Reputation: 1
I am developing a restful application that uses Clojure on the back and Angular on the front. I have the following tables: Customer, Order, Item, OrderItems. I am using korma for communication with the MySql database. When I am making CRUD operations for all entities everything works fine. But I don't have any idea how can I insert multiple rows in the database? Should I use transactions in korma?
(declare customer)
(declare customerorder)
(declare item)
(declare orderitems)
(schema/defschema OrderItem
{
:OrderItemId schema/Int
:OrderId schema/Int
:ItemId schema/Int
:Quantity schema/Int
})
(schema/defschema Customer
{
:CustomerId schema/Int
:Name schema/Str
:Contact schema/Str
})
(schema/defschema UpdateCustomer
{
:Name schema/Str
:Contact schema/Str
})
(schema/defschema NewCustomer
{
:Name schema/Str
:Contact schema/Str
})
(schema/defschema Order
{
:OrderId schema/Int
:CustomerId schema/Int
:PMethod schema/Str
:GTotal schema/Num
})
(schema/defschema Item
{
:ItemId schema/Int
:Name schema/Str
:Price schema/Num
})
(defentity customer
(pk :CustomerId)
(has-many customerorder {:fk :CustomerId})
(entity-fields :CustomerId :Name :Contact))
(defentity customerorder
(pk :OrderId)
(has-many orderitems {:fk :OrderId})
(belongs-to customer {:fk :CustomerId})
(entity-fields :PMethod :GTotal :OrderId)
)
(defentity item
(pk :ItemId)
(entity-fields :ItemId :Name :Price)
(has-many orderitems {:fk :ItemId}))
(defentity orderitems
(pk :OrderItemId)
(belongs-to item {:fk :ItemId})
(belongs-to customerorder {:fk :OrderId})
(entity-fields :OrderItemId :ItemId :Quantity))
And here is my query to select customers that are paying with Cash with their orders and order items:
(defn get-customersbypmethod [PMethod]
(select customerorder (with customer (with orderitems)) (where {:PMethod PMethod})))
My question is how can I insert order with order items?
Upvotes: 0
Views: 306
Reputation: 11
The Korma documentation says the following:
Insert queries use the function (values) to add records. It takes either a single map or a collection of maps and returns the id of the first inserted record.
;; You can insert a single value:
(insert users
(values {:first "john" :last "doe"}))
;; or a collection of several:
(insert users
(values [{:first "john" :last "doe"}
{:first "jane" :last "doe"}]))
;; You can also compose inserts:
(def base (-> (insert* users)
(values {:first "john" :last "doe"})))
(-> base
(values {:first "jane" :last "doe"})
(insert))
;; Same thing as the collection insert
You can do transactions in Korma simply by using the (transaction) macro, which ensures that all queries executed within it are part of a single transaction. You can then use the (rollback) function to force the transaction to rollback if necessary.
(transaction
(insert users
(values {:first "cool"}))
(insert address
(values {:address1 "cool"})))
(transaction
(if-not (valid?)
(rollback)
(do-complicated-query))
(when-not (is-rollback?)
(println "success!")))
Hope this helps. Korma is a stable and well-documented library.
Upvotes: 1