This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-23
Channels
- # 100-days-of-code (2)
- # aws (1)
- # beginners (105)
- # boot (36)
- # calva (4)
- # cider (56)
- # clara (37)
- # cljdoc (16)
- # cljs-dev (19)
- # clojure (44)
- # clojure-dev (20)
- # clojure-italy (24)
- # clojure-nl (3)
- # clojure-serbia (2)
- # clojure-spec (15)
- # clojure-uk (44)
- # clojurescript (41)
- # code-reviews (3)
- # core-async (12)
- # cursive (24)
- # datomic (4)
- # emacs (1)
- # figwheel-main (10)
- # fulcro (168)
- # funcool (2)
- # hyperfiddle (15)
- # jobs (2)
- # jobs-discuss (79)
- # juxt (19)
- # lein-figwheel (1)
- # leiningen (2)
- # luminus (14)
- # mount (8)
- # nrepl (9)
- # off-topic (9)
- # other-languages (1)
- # pathom (32)
- # reitit (6)
- # ring-swagger (3)
- # shadow-cljs (10)
- # slack-help (11)
- # spacemacs (20)
- # sql (29)
- # tools-deps (28)
- # vim (29)
- # yada (4)
the problem is that, sometimes, I don't have only one table, but a query joining two+ tables.. so think I'll manage using db-query-with-resultset
function
If you think of ways the clojure.java.jdbc
API could support your use case better, feel free to open a JIRA issue here https://dev.clojure.org/jira/browse/JDBC
I have a jdbc query question. How do a construct a query or function which will not return nil values. For example,
(jdbc/query db-spec ["SELECT * from feedback"])
some of the columns have nil values. How do I exclude those from the results?Hmmm… I can use filter for a single value:
(into {} (filter #(not (nil? (val %))) (first (jdbc/query db-spec ["SELECT * from feedback WHERE id = 1"]))))
@mark.davidson Can you explain your use case? Nil-punning generally means not needing to do what you're trying to do there...
@seancorfield I’m trying to read from a database, validate the rows with schema and then transform it to json for a REST api
I think I almost have it:
(jdbc/query db-spec ["SELECT * from feedbacks_resolution"] {:row-fn remove-nil})
What's wrong with nil
flowing into the JSON as null
?
(our REST APIs can accept and return null
in the JSON at work)
That would work. I’m not sure how to use s/defschema to accept nil values:
(s/defschema Resolution
{:id s/Int
:feedbacks_id Integer
:device_serial_number String
(s/optional-key :developer_notes) s/Any
:device_server_ip s/Any
:commands_sent Integer
:disconnects Integer
(s/optional-key :debug_server_ip) s/Any
(s/optional-key :debug_commands_sent) Integer
:debug_disconnects Integer
(s/optional-key :category_id) s/Any
(s/optional-key :resolution_id) s/Any
:is_resolved Boolean
})
In spec it would be s/nilable
No idea about Schema
yea, I would rather use spec but I don’t have any great examples in here: https://github.com/metosin/compojure-api
Looks like s/maybe
Given a schema which defines a column with a string type, if nil is in the column then it seems to break validation
;; maybe
(s/validate (s/maybe s/Keyword) :a)
(s/validate (s/maybe s/Keyword) nil)
If your DB schema allows NULL
, then your Schema for the data needs to reflect that.
@seancorfield Using s/maybe did the trick! Thanks for your help!
Cool! Glad that worked.
@mark.davidson since latest c-api alpha, you can use substitute Schemas with Specs, will work 80% of the time ok.
Example app with Schema, Specs and data-specs: https://github.com/metosin/c2
Thanks I’ll take a look. I’m having a bit of a nightmare with schema and compojure-api. I’m not sure I really understand coercion but those options will radically change my results when do various gets on the similar test cases.
nightmare, can't be good. There is also reitit that does both Schema & Spec coercion and has better docs:https://github.com/metosin/reitit.
Thanks for the help. I’ll take a look at the libraries. I’m a clojure novice but I really enjoy the platform and community. I’m still going though a ramp up but I’m trying to use clojure as much as I can.
So the depths of my “nightmare” can be paraphrased as follows: changing the {:coercion <value>}
will change the results when I call my endpoint depending on the schema. On some configurations, the call will 400, others will give a 200 and no headers or body. I’m trying to narrow the use case to isolate the problem.
@U055NJ5CC can you recommend an article or book to help me understand the concept of coercion?
@mark.davidson there is a small guide in https://metosin.github.io/reitit/ring/coercion.html