Fork me on GitHub
#clojure
<
2017-10-25
>
ag00:10:35

can someone help me. I'm trying to generate csv file on the fly. using . and it works when I'm doing it with get request. In my case though I have to do it with post request (because data gets composed via om.next/parser). So this, kinda works:

(defn download-csv [req]
  (let [result (run-om-query (:body req) (:user req))
        query-k (-> req :body ffirst)
        data (get-in result [query-k :data])]
    {:status 200
     :headers {"Content-Type" "text/csv"
               "Content-disposition" "attachment; filename=filename.csv"}
     :body  (ring-io/piped-input-stream
              (fn [out]
                (let [columns (-> data first keys)]
                  (with-open [wtr (io/writer out)]
                    (csv/write-csv wtr [columns])
                    (doseq [row data]
                      (csv/write-csv wtr [(map row columns)]))))))}))
but browser doesn't "recognize it". no "dowload to" popup comes up, etc.

thedavidmeister03:10:18

i use https://github.com/eligrey/FileSaver.js but that is browser side, don't know if that helps

thedavidmeister03:10:23

has a cljsjs wrapper

ag00:10:08

what's missing?

ag00:10:22

I have a feeling that this might be not possible in the same request ;(

ag00:10:27

if it's "post"

devn00:10:37

If you just drop a valid csv string in there does it work?

ag00:10:16

the content it returns from server is fine - it is correct csv. but browser doesn't recognize it as "file download"

ag00:10:51

as I said - if I ignore the om.parser part, and generate just random csv and use get, instead of post it works just fine - I don't even have to set the correct content-type and other headers

ag00:10:57

but I need to use post

antique01:10:31

@ag have you tried setting the content-type to application/download

ag01:10:49

I did. it's still didn't work

Arno Rossouw09:10:10

I like using emacs for clojure, is there a way to do code autocompletion, i'm using inferior-clojure module

tatut09:10:12

yes, by using CIDER 😉

Arno Rossouw09:10:34

ok, cool. Will check

Oliver George10:10:42

How about treating vars like ns alias so we can use shorthand for creating keywords associated with a fn like ::myfn/props

joshkh10:10:00

does anyone have any experience with hugsql (https://www.hugsql.org/)? i'm trying to debug some tricky sql statements and i'd like to see what sql statement hugsql is generating under the hood.

delaguardo10:10:42

;; For most HugSQL usage, you will not need the sqlvec functions.
;; However, sqlvec versions are useful during development and
;; for advanced usage with database functions.
(hugsql/def-sqlvec-fns "princess_bride/db/sql/characters.sql")
take a look at def-sqlvec-fns

delaguardo10:10:05

For myself I found it very useful

igrishaev12:10:05

@joshkh the best option would be to check your DB logs. Configure your database to log all the queries into a file. Or you may run the database not as a service but as process. So you’ll see all the queries in the console. For postgres, start it with postgres -E -D /usr/local/var/postgres (on Mac)

joshkh12:10:39

elegant solution! thanks

qqq13:10:45

what is the clojure equiv of C's FILE__ ?

qqq13:10:58

I want some debug msgs to dump FILE__ so it's easy to figure out where they are coming from

delaguardo13:10:30

You can use (meta #'any-function-or-variable) ;; => {:line 13, :column 1, :file "path/to/file.clj", :name var-name, :ns #namespace[namespace-where-var-defined]} :file is related to your classpath

delaguardo13:10:04

https://clojure.org/reference/metadata there are more information about this

dominicm14:10:14

@qqq you mean like how tools.logging does?

qqq15:10:52

@dominicm: not quite tools.logging: it's ... I'm looking at this cljs app, I see this div, whose body says "todo" -- I want to kow where this "todo" is coming from -- so I grep for "todo" in my source tree -- and I get many matches, so I'd really prefer is that div said "todo: foobart.clj: line 74" instead of just "todo"

dominicm15:10:30

@qqq you need a macro which looks at *ns*, which is how this is done in tools logging

qqq15:10:47

@dominicm, @delaguardo: going with @delaguardo’s meta solution 🙂

dominicm15:10:05

That means they need a reference to the function they are in, no?

qqq18:10:47

I know what I ca ndo (io/copy in out) -- however, is there a nice way to inject a "gunzip" in the middle of it?

qqq18:10:59

so the input is a *.gz stream, and for the output, I want an uncompressed stream

noisesmith18:10:11

very easy to use in my experience

qqq18:10:26

nice, I did not reailze you could just 'wrap' java.util.zip.GZIPInputStream. and have it take care of the rest

mcall2519:10:58

I am looking for advice to connect to a Mysql Database. I am using jbdc and mysql/mysql-connector-java plugin to run a simple query to my database. however, the connection does not seem to connect (to a my local environment) and not able to pull any data. Does anyone know any helpful tutoriasl or walk-throughs to help me connect to a db. error messages: 1. {user=, password=}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ] 2. Cannot JSON encode object of class: class [B: [B@2466ff66

hiredman19:10:06

I would look at the stacktrace for 2, my bet is you have other library loaded that globally extends jdbc to automatically json encode things

hiredman19:10:40

1 looks like something from a connection pooling library, which again leads me to believe you are dealing with a much larger pile of code then your question suggests

the2bears20:10:03

Does anyone know if there's a 'Storm' channel? Specifically using Clojure spouts/bolts with Storm.

seancorfield21:10:52

There is not. You might as well ask questions here and see if anyone responds...

admay22:10:31

Hey guys, I’m looking for some resources/examples of speccing and unit testing database transactions in Clojure w/ clojure.java.jdbc (for now). Does anyone have a project or resource they can point me to for testing functions for reads, writes, and deletes?

seancorfield22:10:30

@admay Perhaps start with clojure.java.jdbc's own test suite? Your question is a bit vague so I'm not sure what you're really asking.

seancorfield22:10:19

(I would also say that "unit testing" and "database transactions" don't really go together -- that's more of an "integration test")

admay22:10:40

So I have a bunch of functions like (fetch-from-db ...), (insert-into-db! ...), (delete-from-db! ...), and (update-in-db!)` that I need to test in order to pass my companies ‘code coverage’ standards and I’m not quite sure how to go about it. Like you said, ‘unit testing’ and ‘database transactions’ don’t really go together. I’m absolutely open to another approach given I’m very new to unit/integration testing as long as they’re automated. The automation is non-negotiable as per company standards.

noisesmith22:10:40

if you separate all logic into functional data to data transforms without side effects, the part where you talk to the db can be a very small stub that should already be tested by the library you are using

noisesmith22:10:31

if they ask you to test interaction with the db lib, then you need integration testing instead of unit testing, and someone needs to hook up a db for that testing environment

seancorfield22:10:03

This also highlights the disadvantage of writing wrappers for things that clojure.java.jdbc already does since you are now creating extra code that you need to "test" to pass an arbitrary code coverage rule 🙂

noisesmith22:10:47

(as well as actually creating a layer of logic where regressions can occur)

seancorfield22:10:17

In order to test that insert-into-db! "does the right thing" you'd need to read the data back in and assert it matches your expectations -- which means that your tests rely on fetch-from-db which is a bit circular. And how would you test that fetch-from-db is doing the right thing? (would you insert-into-db! and then fetch-from-db and compare the result? Again, circular)