1

A clojure server reads a datetime column from a mysql database. By using jdbc this action usually returns an instance of java.sql.Timestamp

I'm bringing this data to a frontend via the transit format. One could coerce the date to a timestamp and parse it on the frontend for further processing for e.g. the library cljs-time. Is this the way to go or is there another more convenient approach?

Alex Miller
  • 69,183
  • 25
  • 122
  • 167
Anton Harald
  • 5,772
  • 4
  • 27
  • 61
  • If you're using JDBC you should be able to convert it automatically to a java.until.Date (or other class of your choosing) at query time, would that solve your problem? – Valentin Waeselynck Sep 28 '16 at 08:16
  • oh, I see now. java.util.Date is a transit type. good point. But to what would that be coerced at the client side? – Anton Harald Sep 28 '16 at 08:26
  • last question is answered. when using java.util.Date, it will become an js/Date instance at the frontend. However, if someonw knows a concise way to receive java.util.Date from a jdbc / honeysql query, I'd still be keen to hear that. – Anton Harald Sep 28 '16 at 11:27
  • not publishing it as an answer, but all you have to do is implement the IResultSetReadColumn (https://clojure.github.io/java.jdbc/#clojure.java.jdbc/IResultSetReadColumn) protocol on Timestamp. There's an example in Luminus. – Valentin Waeselynck Sep 28 '16 at 13:58

1 Answers1

1

You can see the default type mappings in transit-cljs here. By default Transit time values are mapped to JavaScript Dates. I much prefer to have the Dates mapped to goog.Date.UtcDateTime. There's docs on extending Transit Read and Write handlers, but here's what we use:

(def transit-readers
  {:handlers
   {"m" (transit/read-handler (fn [s] (UtcDateTime.fromTimestamp s)))
    "u" uuid}})

(def transit-writers
  {:handlers
   {UtcDateTime (transit/write-handler
                  (constantly "m")
                  (fn [v] (.getTime v))
                  (fn [v] (str (.getTime v))))}})
Daniel Compton
  • 13,878
  • 4
  • 40
  • 60