Fork me on GitHub
#sql
<
2020-04-30
>
seancorfield02:04:57

I've updated the Getting Started page in the docs (on master) to convert the explicit Component example over to the new component function, showing that you don't need to create a record with start/`stop` methods.

👏 4
nikolavojicic14:04:12

Spec for opts, e.g. in get-connection can be nilable, with nil it works just fine. Isn't map? too restrictive? It forces me to do (or opts {}) just to be able to use instrumentation. (This may be question for clojure-spec channel though)

seancorfield15:04:17

@nikolavojicic Can you be a bit more specific about what you're asking? Which library? Which spec? Which file/line?

seancorfield15:04:59

Both clojure.java.jdbc and next.jdbc provide their own specs and both have :opts as an optional parameter for get-connection:

;; clojure.java.jdbc/get-connection:
(s/fdef sql/get-connection
        :args (s/cat :db-spec ::db-spec
                     :opts    (s/? ::connection-options))
        :ret  ::connection)
;; next.jdbc/get-connection:
(s/fdef jdbc/get-connection
        :args (s/cat :spec ::db-spec
                     :opts (s/? ::opts-map)))

seancorfield15:04:16

If you are using those libraries, you should be using their specs.

nikolavojicic16:04:25

I'm using next.jdbc. My question is would it be ok to change get-connection spec to accept nil opts, i.e: :opts (s/? (s/nilable ::opts-map)) ? Use case is when opts for connection is taken from another map, and it may be absent -> nil. With nil opts, get-connection works the same as with {} opts.

nikolavojicic16:04:57

Ofc I can override it for my project so it's not a big deal.

seancorfield17:04:10

@nikolavojicic Sorry for the delay. I'm on vacation today so I'm away from my computer quite a bit. I really don't like optional parameters that can also be nil or the intended value, which is why the specs are as they are. I understand that nil "works" in place of opts in some (all?) API functions in next.jdbc (and clojure.java.jdbc) but that's just an artifact of the implementation and not something I'm prepared to support as an official part of the API -- so the specs will remain stricter than the implementation here. Since you are fetching your options from another map, you can supply a "not found" value of {} at the point of access: (:jdbc-opts my-map {}) or (get my-map :jdbc-opts {}) and then the intent is clear.

👍 8
seancorfield17:04:06

(and if that key in your map can be nil then I'd say you have an upstream problem: finding nil values in a Clojure hash map is a code/design smell as far as I'm concerned -- except in very specific cases where nil has a specific semantic meaning and you need to distinguish between contains? and an actual nil value)

💯 4