This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # admin-announcements (25)
- # beginners (60)
- # boot (277)
- # cider (5)
- # cljs-dev (3)
- # cljsrn (2)
- # clojure (82)
- # clojure-art (15)
- # clojure-berlin (2)
- # clojure-boston (1)
- # clojure-italy (40)
- # clojure-russia (118)
- # clojurebridge (1)
- # clojurecup (1)
- # clojurescript (82)
- # component (3)
- # cursive (31)
- # datavis (9)
- # datomic (39)
- # editors (1)
- # editors-rus (9)
- # emacs (15)
- # hoplon (50)
- # ldnclj (2)
- # leiningen (4)
- # off-topic (9)
- # om (123)
- # re-frame (28)
- # reagent (7)
- # vim (1)
- # yada (3)
I would recommend using https://github.com/stuartsierra/component to set your application up at run time.
is there an example (ideally with comments) of a system of components that JUST uses datomic and jetty
Easier, but in the pejorative sense, would be to do something like:
(defonce datomic-uri (atom "")) (defn get-conn  (d/connect @datomic-uri))
ok so where might I look for the simplest example of a datomic/jetty set of components
(defprotocol IConnect (-connect [this])) (defrecord Datomic [uri] IConnect (-connect [_] (d/connect uri))) (component/system-map :datomic (->Datomic (get-in my-config [:datomic :uri])))
You would initialize something like that^ in your
-main, then you pass
(:datomic system-map) into any function that needs to connect to datomic.
And that function can look like:
(defn my-fn [datomic] (let [conn (-connect datomic)] …))
So I guess I’m missing a conceptual bridge between the problem and the solution. The problem as I understand it is that by having a (def conn (d/connect uri)), java is going to try to trigger the connection whenever I want to compile my app into an uberjar. What I am trying to do with a component is to hide that connection inside a protocol (which is sort of like an object..). The component returns a map with the connection, and then everywhere where In my query functions I would be calling ‘conn’ I instead reach into the component for the value of conn?
I guess the point where I’m confused is — if this mental model is right — why wouldn’t initializing the component in main still cause the same problems
So I don’t have very long, but I’ll try to explain a bit. I try to bbl to answer any additional questions you have.
So fundamentally you don’t want to have a connection attached to your namespace (via def).
Problem is, you need the URI to connect, so you have to pass that information around somehow.
So one perfectly fine solution is the “easy” example above, where you
defonce a URI, then call
(get-conn) whenever you need a conn.
But that kind of introduces another problem: what if you want to use a different URI? (e.g. you want to test something, or you have multiple databases)
So we can do better by defining the URI at the top-most level, then passing it down to anyone that needs to call connect.
The protocols are just so you can have multiple implementations of a function. So you can have a
(->TestDatomic) or something that has a completely different implementation.
You don’t have to use them. You could just pass the URI around. But they do make it easier to have a completely different implementation of things.
But if you’re not really familiar w/ protocols, you can pretty easily go without at first. Just remember when you hit a situation where things don’t quite fit or you feel like you need to use a switch, you should look into protocols.
If you're looking into a switch and protocols seem to be a bit hard to understand, multimethods are really nice
@frankk38506: It’s a type hint http://clojure.org/java_interop#Java%20Interop-Type%20Hints