Fork me on GitHub
#duct
<
2017-12-14
>
myguidingstar02:12:38

I can't find the source code of ig/init-key for :duct.module.web/api in http://github.com/duct-framework/module.web

myguidingstar02:12:24

@weavejester I did have :duct.module.web/api because I generated the template with +api. So the problem now is I don't know how to destructure json (or edn) params from http requests with ataraxy

weavejester02:12:11

@myguidingstar In the duct.module.web namespace

weavejester02:12:40

Have you checked the guide that’s halfway done? That gives examples of destructuring parameters.

weavejester02:12:35

The API module loads in Muuntaja’s wrap-format, which puts the parsed body in :body-params.

myguidingstar02:12:04

oh i was looking at duct.middleware.web 😄 silly me

myguidingstar02:12:22

I started with that guide. I was poking around the config file to get understanding

myguidingstar02:12:57

and in a moment of unconsciousness I changed :body-params to :json-params but never changed it back

myguidingstar02:12:42

I didn't know it's muuntaja that is underneat instead of ring-json

weavejester02:12:56

I’ll see about improving the docs to perhaps make it more obvious in future.

weavejester03:12:35

Muuntaja handles edn as well, and potentially other formats. It also handles content negotiation correctly.

myguidingstar03:12:28

how grateful I am as I see you've done a lot both in terms of code and docs

lovuikeng03:12:24

didn't know muuntaja is used underneath, cool, thank you weavejester 🙂

myguidingstar05:12:06

in dev namespace, how do I pull sql instance from system var so I can make real queries with it?

myguidingstar05:12:01

never mind, found it again in duct guide

myguidingstar05:12:10

but I need explanation for this adhoc function:

myguidingstar05:12:16

`(defn db [] (-> system (ig/find-derived-1 :duct.database/sql) val :spec))`

weavejester14:12:04

@myguidingstar The (ig/find-derived-1 m k) function looks for a single key in m that’s derived from k. So in the above case, it’s saying: “Find a key that inherits from :duct.database/sql

weavejester14:12:36

Like clojure.core/find, ig/find-derived-1 returns a key/value pair.

weavejester14:12:46

So we then use val to get the value.

weavejester14:12:42

That gives us the database boundary record (`duct.database.sql.Boundary`), and that has a key :spec that contains the database spec.

weavejester14:12:04

Oh, and in case it’s not obvious, system is a var that stores the running system in development.

weavejester14:12:57

We could have also have written:

(defn db []
  (-> system :duct.database.sql/hikaricp :spec))

weavejester14:12:41

Because :duct.database.sql/hikaricp is the actual database key. But if we changed that in any way, using a different connection pool library for instance, then the db function would need to be changed.

weavejester14:12:57

By using ig/find-derived-1 instead of looking up the key directly, we’re telling the function to find anything that looks like a SQL database connection.

weavejester14:12:25

If there’s multiple database connections, ig/find-derived-1 will throw an ambiguous key exception.

myguidingstar14:12:34

my guess is :duct.database.sql/hikaricp is unique so it can never cause ig/find-derived-1 throw an ambiguous key exception, while :duct.database/sql can. Is it correct?

myguidingstar14:12:19

btw, how do we call the relation between :duct.database/sql and :duct.database.sql/hikaricp? (I think that will help with understanding)

weavejester15:12:08

@myguidingstar Yes, unless you have multiple SQL databases in your application

weavejester15:12:22

:duct.database.sql/hikaricp derives from :duct.database/sql

weavejester15:12:45

Namespaced keywords in Clojure can be derived from one another. There’s an inheritance hierarchy.

weavejester15:12:35

So (find-derived m k) searches for all map entries in m where the key derives from k

weavejester15:12:55

And (find-derived-1 m k) looks for a single derived map entry, and throws an error if it’s ambiguous.