juxt / clip

Light structure and support for dependency injection
MIT License
228 stars 15 forks source link

What am I missing ? #22

Open olymk2 opened 3 years ago

olymk2 commented 3 years ago

Not tried using a state management library, I have followed the examples and have everything working.

What I do not get is how I reference state, so say I bring up the db in start how do I then access the db connection, normally I would (defonce crux-db (run-crux)) then reference crux-db in my code, I have found clip/ref but that seems not to return an instance of the command run in startup of clip.

how would I get the connection for a db like postgres or crux ?

The only examples I could find seem to pass the connection in to all functions instead of setting it globally.

SevereOverfl0w commented 3 years ago

You would configure clip to run your functions. For example, you'd create a closure over your ring handler(s) to pass it the db. Here's an example of doing that in the todo example linked in the README: https://github.com/prestancedesign/todo-backend-reitit/blob/c3d5974f679203f200089ca10f40fda75b7e6e33/resources/config.edn#L12

olymk2 commented 3 years ago

lol I really don't get something, it looks like its being passed to the handler ie todo-backend.core/app-routes but looking at the code here

https://github.com/prestancedesign/todo-backend-reitit/blob/14fc57490fe03ac67d9e4ffd70eb129841640b47/src/todo_backend/core.clj#L26

it does not take a parameter, I guess ring/ring-handler could be storing a fn in app-routes which takes db.

then in this piece of code, it looks like its actually setting up the db from env and storing the instance in db rather than using clip at which point its global scope in that file and makes me wonder why its being used in clip.

https://github.com/prestancedesign/todo-backend-reitit/blob/3d56222cae188f653cf863c8019770c68e3b607d/src/todo_backend/store.clj#L9

I admit something in there are unfamiliar like nextjdbc, but to me the point is I use clip to start my db and http server in order and some how make the value available, I think mount for example inserts namespaces which you can import to get the reference to your instances.