When running a query on a postgres database on a clojure REPL, the timestamps fields are presented in UTC and I need them to be in timezone America/Sao_Paulo (UTC-3)
So far I have tried the following on Intellij's REPL:
Set -Duser.timezone=America/Sao_Paulo inside the file idea.vmoptions (intellij's)
Add :jvm-opts ["-Duser.timezone=America/Sao_Paulo"] to project.clj
Add -Duser.timezone=America/Sao_Paulo in Intellij's REPL configuration
export JAVA_OPTS="-Duser.timezone=America/Sao_Paulo:$JAVA_OPTS" inside ~/.zshrc
and the following on Leiningen REPL:
Add :jvm-opts ["-Duser.timezone=America/Sao_Paulo"] to project.clj
export JAVA_OPTS="-Duser.timezone=America/Sao_Paulo:$JAVA_OPTS" inside ~/.zshrc
None worked so far!
Sample code
(ns experiments
(:require [next.jdbc :as jdbc]))
(def db
{:dbtype "postgres"
:dbname "<dbname>"
:host "<host>"
:port 5432
:user "<user>"
:password "<pass>"})
(def ds (jdbc/get-datasource db))
(jdbc/execute! ds ["select current_timestamp"])
You did not mention any Postgres options. Please study this page carefully for info and options.
If the above does not solve your problem, it may be easiest to use java.time to do the conversion. I also have some helper/convenience functions available here. Unit tests show them in action, and the source code provides examples of java.time interop from clojure.
I would avoid the older Joda Time libraries as they are obsolete (replaced by java.time). I think that Java interop is the easiest & most straightforward way to access java.time.
Related
In my clojure Luminus/Compojure app I have this in routes.clj:
(def page-size 12)
(def images-path "/public/images/.....")
I need to move them to a config of some sort. Where is the best place? I'd like something simple and not to use any additional library on top on the ones that I'm already using which come with Luminus.
Luminus uses it's config library for configuration. You can put your configuration variables into appropriate config.edn files (per environment). Config values are available as a map stored in config.core/env. You can see an example in your <app>.core namespace:
(defn http-port [port]
;;default production port is set in
;;env/prod/resources/config.edn
(parse-port (or port (env :port))))
Ask yourself this question:
Would I ever want multiple deployments of my application where this setting differs?
If the answer to that question is "yes" then the configuration should be dictated by the environment running you application either by an edn file, Environ or other means.
If not... then you are talking about something I would categorize as an application constant, which helps avoiding magic numbers. In some situations it can improve readability by placing these in specific namespaces like so.
Constants namespace:
(ns my.app.constants)
(def page-size 12)
(def images-path "/public/images/.....")
Application:
(ns my.app.core
(:require [my.app.constants :as const)
;; now access the application constant like this
;; const/page-size
(defn image-url [image-name]
(str const/images-path "/" image-name))
I'm trying to truncate my dataomic database between tests. I see a lot of questions on google about excision and how to delete data, but none about just wiping a database clean.
I'm doing something along the following using core.test:
(with-redefs [db/uri "datomic:free://localhost:4334/test_om_asyn101_next"
db/conn (d/connect db/uri)]
(run-tests 'rtest.core-test))
I've been wiping out the DB by changing the URI and creating anew, but getting tired of doing that! Thanks!
There is a delete-database fn. See day of datomic tutorial for an example about how to create a new in memory db for each test.
Also, yeller has a nice example of how to use datomic's "what if" functionality to do unit testing.
I've been using (delete-database) as dAni suggests, with a clojure.test fixture:
(ns test-util)
(defn datomic-rollback-fixture [test-fn]
;; setup
(run-migrations)
;; run the tests
(test-fn)
;; clean up
(let [datomic-uri (get-in config/config [:datomic :uri])]
(when (string/starts-with? datomic-uri "datomic:mem:")
(d/delete-database datomic-uri))))
The (run-migrations) function loads our schema definitions from an EDN file, which we have defined via conformity. The bit about only destroying a datomic:mem database is because I'm paranoid about deleting databases out of production by accident.
Then in the test functions, we've got:
(use-fixtures :each test-util/datomic-rollback-fixture)
(deftest my-test ...)
Seems to work fine for me so far and is plenty fast enough with the memory database.
I am trying to create a web app in clojure.
i have used clojurescript om and react.
there are two files core.cljs and db.clj.
core.cljs contains ui for login page and db.clj contains all database connections.
Now i am trying to call a db.clj method add-user[username password] in
core.cljs.
In db.clj
(defn add-user [username,password]
(sql/with-connection db
(sql/insert-values :users [:username :password]
[username password])))
In core.cljs
(dom/button #js {:ref "submit"
:onClick (fn[e](add-user usname passwrd))}"submit")
But i am not able to call that method in core.cljs.
It shows some error message like
clojure.lang.ExceptionInfo : failed compiling file:src\login_page\core.cljs
clojure.lang.ExceptionInfo : No such namespace: login_page.db, could not locate login_page/db.cljs, login_page/db.cljc, or Closure namespace "login_page.db"
Rename db.clj to either db.cljs or db.cljc. That should get you past the 'No such namespace' error message.
That's the basic gist of it. Of course your dependencies on clj libraries will have to be removed - that might be the reason for the negative comment below. Alter your code so that you use a simple atom as your database. That should get you developing.
And you can wait for a much better answer than this one that will show you how to get the Client and Server communication setup. But it may not come because, as pointed out in the comments, there is already documentation for this, and unfortunately quite a few choices that have to be made. Another unfortunate thing is that the way to do it now may not be the way to do it early next year. Watch the Om-Next space!
I've never had any problems compiling .cljs or .cljc files. You just have to set up your lein project.clj file properly. There will be plenty of examples if you Google around, or you can take a look at the following small Github project: https://github.com/chrismurrph/passing-time - no need to worry about the code, just look at its project.clj file.
I am converting an older web app I made a few months ago from Noir to Compojure and I am using the Lib-Noir add-on. It appears that session/put! is either changed in some way I don't understand or it is bugging out for whatever reason.
Here, I can see that 4Clojure appears to be using it with no problems: See Line 51. I also found this thread that covers the same question but there doesn't appear to be a satisfactory response.
This should work (Noir):
user=> (require '[noir.session :as sesh])
nil
user=> (sesh/put! :user "me")
ClassCastException clojure.lang.Var$Unbound cannot be cast to clojure.lang.Atom
clojure.core/swap! (core.clj:2162)
The above is the same error that I am looking at on the webpage. Basically I'm stuck.
Edit to add
Appears I created a bit of confusion with the command line part: (put!) is not working in the program either. There's not much to write about it, except that it is (shesh/put! :uname user) and it appears that :uname isn't working. I'm confused as to why it would have worked before and not now when I am using the same tools as before. This is a rewrite of a site I build about 6 months ago. I'm just moving it to Compojure from Noir. The lib-noir session is, as far as I know, essentially the same as what was in Noir.
ANOTHER EDIT
I put the code up on github. This isn't the completed project, but hopefully someone can decipher what is going on here: https://github.com/dt1/SoloResume
If you run it from the REPL, there is no browser session registered in Noir. You can simulate this by using binding:
(binding [sesh/*noir-session* (atom {:somekey "somevalue"})]
(sesh/put! :user "borkdude"))
Use this only for testing/simulating to see what goes on in the session map, not in production code.
A fairly old question, but answering here as it was the first Google result when I had the same problem. I was using compojure:1.1.6, ring:1.2.1 and lib-noir:0.7.6
You need to use noir.session/wrap-noir-session when defining your app - e.g:
(def app
(-> (handler/site (routes app-routes ))
session/wrap-noir-session
wrap-base-url))
References:
https://groups.google.com/d/msg/clojure/XXgSGhF912I/luhN9wmMoi8J
I am using clj-webdriver to do some Selenium based testing on a Clojurescript web app. Sometimes, there is something in the app itself that I want to be able to fiddle with while the test are running. I see that clj-webdriver has something called (execute-script js args) that takes a string of Javascript code and runs it on the current testing browser. I have tested this and it seems to work. I would like to pass clojurescript code to execute-script though. I need something that will compile my Clojure form into Clojurescript code.
I see the following question that sort of relates. It says to use the js/emit function from clutch. I searched clutch and found it mentioned only in (view) in cljs-views.clj I have attempted the following in a repl:
user> (use 'com.ashafa.clutch.cljs-views)
nil
user> view
<core$comp$fn__4034 clojure.core$comp$fn__4034#ebd3f80>
user> js/emit
CompilerException java.lang.RuntimeException: No such namespace: js, #compiling (NO_SOURCE_PATH:1)
user>
This isn't terribly surprising, how could js be in a regular clojure namesapce? But how do I use this (or any other) system to generate Clojurescript (javascript) code that I can pass to execute-script?
Use the cljs.closure/build function:
(use '[cljs.closure :only [build]])
(build '(print "hello") {:optimizations :simple :pretty-print true})
There are more examples in a comment at the bottom of closure.clj. There are options for outputting to a file as well.