Note that there are some explanatory texts on larger screens.

plurals
  1. POclojure, protocols for inserting database items
    text
    copied!<p>Still learning clojure and doing an experiment on trying to wrap my head around clojure and protocols. My main question is my code the correct way to extract away the low-level database interactions in order to provide a cleaner way of adding specific datatypes to my database?</p> <pre><code>(defn- db-insert [table record] "Inserts record based on table/record map" (sql/with-connection rosay-db (sql/transaction (sql/insert-record table record)))) ;; public interface (defprotocol DBFactory (add-item [_] "Adds item to db")) (defrecord Page [name description keywords frontpage client_id pages_type_id] DBFactory (add-item [_] (db-insert :pages {:name name :description description :keywords keywords :frontpage frontpage :client_id client_id :pages_type_id pages_type_id}))) (defrecord Client [username password email domain] DBFactory (add-item [_] (db-insert :clients {:username username :password password :email email :domain domain}))) </code></pre> <p>Then in my nrepl I can do the following:</p> <pre><code> rosay.server=&gt; (use 'rosay.models) nil rosay.server=&gt; (add-item (-&gt;Client "booyaka" "fark" "mailzer" "domain.com")) {:updated_on nil, :created_on #inst "2012-12-04T04:25:22.672462000-00:00", :email "mailzer", :domain "domain.com", :password "fark", :username "booyaka", :id 10} </code></pre> <p>I guess im just curious if my way of attacking this problem is correct or even makes sense. So far I can do both Clients and Pages by simply calling my protocol with my record type and everything seems to work. Perhaps I'm overthinking the problem and just writing a few functions is all?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload