This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-28
Channels
- # announcements (1)
- # aws (1)
- # babashka (15)
- # beginners (19)
- # biff (4)
- # clojars (24)
- # clojure (22)
- # clojure-europe (17)
- # clojure-norway (3)
- # clojurescript (10)
- # emacs (5)
- # gratitude (1)
- # introduce-yourself (2)
- # jobs (1)
- # malli (3)
- # off-topic (2)
- # re-frame (1)
- # remote-jobs (3)
- # shadow-cljs (71)
- # vim (4)
What better way to build a database connection with Korma in a Pedestal project with Omniconf? I’m building up an API server with Postgres as Database. With the help of Omniconf, I want to override the Database configuration during dev, staging and production. I found I had to define the connection after I loaded configurations with Omniconf, and then I came up with a solution below:
;; server.clj of Pedestal
(defn run-dev
"The entry-point for 'lein run-dev'"
[& args]
(println "\nCreating your [DEV] server...")
;; initialize configuration with Omniconf
(cfg/init-config {:cli-args args})
;; define a database connection with Korma
(defdb db
(postgres {:db (cfg/get-config :db-name)
:user (cfg/get-config :db-user)
:password (cfg/get-config :db-pass)}))
;; ... other functions
Is there any better way to do this?Maybe? Probably? I haven’t worked with korma or pedestal, but the gist of your problem is basically that defdb
expects your configuration to be available when it is compiled. You could just manually do the steps it’s doing inside a function that you call once you know your config is ready.
(defn db-spec []
{:db (cfg/get-config :db-name) ...})
(defn make-db []
(defonce db (korma.db/create-db (postgres (db-spec))))
(korma.db/default-connection db))
Then call make-db
after configuration has been loaded.Thanks again for the detailed hints.
I’m unfamiliar with macros, as I’m still new to Clojure.
I guess that’s why I came into this issue when using defdb
.
I refactor my code with your suggestions, and it looks better now 🙂 , as I can easily reuse it in both endpoints (dev and production) and reduce typos.
The better way is not to use Korma. It’s a very problematic library, partly because the db argument isn’t an argument but ambient global state.
Could you suggest any other library to handle DB with?
Or It’s better to use the basic clojure.java.jdbc
library?
I have the following multimethod:
(defmulti compare-part (fn [left right] [(coll? left) (coll? right)]))
(defmethod compare-part [false false] [left right]
(if (= left right) :undetermined (< left right)))
(defmethod compare-part [true false] [left right]
(compare-part left [right]))
(defmethod compare-part [false true] [left right]
(compare-part [left] right))
(defmethod compare-part [true true] [left right]
(if (not= (count left) (count right))
(< (count left) (count right))
(->> (map compare-part left right)
(filter boolean?)
first)))
No matter whether left and right are vectors or numbers (the only two options) it always enters the first option and often explodes because seqs can't be compared with <
what did I do wrong?REPL shenanigans (can't reload multimethods just by sending them to the repl 😞)
I sometimes put a (comment (def bla-bla nil)) before (defmulti bla-bla...) to make it easier to remember to clobber it before re-executing it.
you can remove symbols from a namespace, how to do so will be editor/repl dependant. there is probably a special form that dose so as well.
Hey, I'm quite new to clojure and looking for a bit of help. I'm trying to filter a list of hashmaps by a search term, but I'm trying to pass in multiple keys that can be used to filter. I could use or
but I want to make a reusable function that can take data search-term & keys
, so if I have an example input of:
[{:country "England" :city "London"} {:country "England" :city "Frimley"} {:country "France" :city "Paris"} {:country "France" :city "Marseille"}]
I want to filter the list by country and city, so if my search-term is "Fr" it would return:
[{:country "England" :city "Frimley"} {:country "France" :city "Paris"} {:country "France" :city "Marseille"}]
Appreciate any help, I've been googling and trying to do something for a while but just been struggling with itA function that given a list of keys and a search term, returns a list of functions that take a map and return true or false
A function that takes a list of functions and a map and returns true if some of the functions return true when applied to the map
@U0NCTKEV8 so I've gotten a function to start with that can work with 1 key:
(defn filter-by-search [items term key]
(filter #(str/includes (key %) term) items))
I just don't really understand how to change it if I turn key to & keys and do the same, it isn't really making sense
@U6N4HSMFW thanks, I will try in there tooI strongly recommend not trying to write a single function, but decompose the problem into sub problems until you can solve them, and solve each sub problem as a function, and solve the overall problem using the functions you wrote for subproblems
Thanks @U0NCTKEV8 I've got something that works now