sql

Samuel Ludwig 2023-04-25T22:08:06.047119Z

I'm a little confused about what's happening behind the scenes wrt next.jdbc's built-in HikariCP-component support. When I evaluate (connection/component HikariDataSource <my-dbspec>), I get an empty map, just {}. It clearly works, as invoking that connection/component with (component/start (...)) gives me a valid datasource that I can use to query, but I'm just very lost as of course running (component/start {}) is not going to give me a proper datasource. Whats really happening here?

👍 1
Samuel Ludwig 2023-04-26T12:57:58.172129Z

much appreciated all-around! this is definitely eye-opening: A new branch in the Clojure Skill-Tree has been revealed 😄

lukasz 2023-04-25T22:11:03.455809Z

See this:

(meta (connection/component HikariDataSource db-spec))
The component function uses new-ish feature of implementing protocols via metadata - so all the relevant bits are not in the map itself, but the map's metadata. See more here: https://clojure.org/reference/protocols#_extend_via_metadata

Samuel Ludwig 2023-04-25T22:15:43.827089Z

Whoahh, I see, appreciate it, meta stuff is definitely still something I haven't looked at enough yet

lukasz 2023-04-25T22:18:47.060399Z

Happy to help!

dpsutton 2023-04-25T22:27:02.192519Z

do (set! *print-meta* true) and then evaluate them again

👍 1
seancorfield 2023-04-25T23:18:25.885909Z

Perhaps also a little mind-blowing in this case is that while connection/component gives you a hash map with metadata, when you start it, you get back a function with metadata. If you call the function, you get the underlying datasource out of the component. If you stop the function, you'll get back the empty hash map with metadata (so you can start it again).

seancorfield 2023-04-25T23:19:55.055799Z

Sierra's Component library has been able to work with just metadata for a while. That means that any Clojure object that can carry metadata can be a "Component" if it has no dependencies. For a "Component" with dependencies, you must have an object that is associative by key (keyword) so you must have a hash map or a record.