This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-02-09
Channels
- # announcements (26)
- # babashka (4)
- # beginners (17)
- # calva (21)
- # cider (13)
- # clerk (17)
- # clj-commons (23)
- # clj-kondo (3)
- # cljdoc (47)
- # cljsrn (10)
- # clojure (123)
- # clojure-belgium (2)
- # clojure-dev (25)
- # clojure-europe (34)
- # clojure-gamedev (2)
- # clojure-italy (1)
- # clojure-nl (3)
- # clojure-norway (4)
- # clojure-uk (4)
- # clojurescript (86)
- # cursive (12)
- # datahike (2)
- # datomic (2)
- # emacs (4)
- # fulcro (6)
- # funcool (15)
- # instaparse (1)
- # integrant (11)
- # jobs (1)
- # joyride (9)
- # kaocha (3)
- # membrane (8)
- # off-topic (1)
- # pathom (4)
- # practicalli (2)
- # quil (1)
- # rdf (1)
- # reagent (9)
- # remote-jobs (1)
- # shadow-cljs (27)
- # spacemacs (4)
- # specter (1)
- # sql (11)
- # tools-deps (55)
- # vim (1)
I have configured db using below code, Is there a way where I can have single connection var in integrant ?
(defmethod ig/init-key :db/postgres
[_ config]
(println "\n Configured DB")
(:jdbc-url config))
(defn -main
[]
(let [system (ig/init a/config)])
system)
init-key should return either a connection pool or connection. in your example it returns just db url.
@U04V4KLKC where should I define a connection object? during startup ?
or I can define in routes
(defn routes
[env]
[["/"
{:get {:handler (fn [req]
{:status 200
:body "Hello Apple"})}}]
["/v1"
(recipe/routes env)]])
database has nothing to do with routes
(defmethod ig/init-key :db/postgres
[_ config]
(println "\n Configured DB")
(create-connection-pool (:jdbc-url config)))
(defmethod ig/halt-key! :db/postgres [_ db]
(.close db))
create-connection-pool
should be a function that takes jdbc-url and creates connection pool. Then you can include the key :db/postgres
into system config and after you start you application the reference to this key in any other component will have a real connection pool.we need to store connection in some var right, to make use it in other namespace. So should I do it as code below? or we have any other way
(def conn (atom {}))
(defn create-connection-pool [jdbc-config]
(with-open [conn-system (jdbc/get-connection jdbc-config)]
(reset! conn conn-system)))
no, if you are using integrant than it would be better to start using it how it suppose to.
(defmethod ig/init-key :db/postgres
[_ config]
(println "\n Configured DB")
(create-connection-pool (:jdbc-url config)))
(defmethod ig/halt-key! :db/postgres [_ db]
(.close db))
(defmethod ig/init-key :services/handler [_ {:keys [db]}]
(start-service db))
(def config
{:db/postgres {:jdbc-url "jdbc:postgres://..."}
:services/handler {:db (ig/ref :db/postgres)}})
(defn -main [& args]
(ig/init config))
:services/handler
can be defined in any namespace but it will always have access to the connection pool through db reference.those are two different functions