Fork me on GitHub
Александр Стоянов02:10:11

Hello! I need some help. How can i connect to postgresql from clojurescript? Basic operations such as create, read update and delete


clojurescript compiles to javascript; and as you know, JS can run on both the browser and servers/computers using the v8 engine. But, if you are using cljs for the frontend web, then there's no way you can directly connect postgres (or any other database) with browser clojurescript (only with nodejs clojurescript). You must have a server that you make http requests to and it should handle all the CRUD operations. If you're referring to cljs for nodejs, then you can use the "pg" npm package directly from clojurescript.


Hi everyone I'm trying to program a simple spa with hiccup and cljs but I'm totally stuck now. I'm working with a nested map and I'm not able to show the vals on a webpage. Is there anyone with an idea whats going wrong?

(def liste4 {:rooms {:kueche {:name "Kueche" :m2 "1"}
                     :wohnzimmer {:name "Wohnzimmer" :m2 "2"}
                     :bad {:name "Badezimmer" :m2 ""}

(defn inputPage [rNames rSizes]
  [:div {:id "inputLine"}
   [:p rNames ": " rSizes "m2" ]])

(doseq [[k v] (get-in liste4 [:rooms])] (inputPage (v :name) (v :m2)))
Thanks to gon I finally got the solution.
(for [ [%1 %2] (get-in liste4 [:rooms])]
       (inputPage (%2 :name) (%2 :m2) ))


doseq does not returns anything, it is for side effects... use map or for


You are awesome. Thanks for the hint.

AJ Jaro13:10:00

I am pretty sure there is a document that describes the preferred CLJS syntax for JS Interop. Where is the JS interop syntax documented for CLJS related to things like these examples? Ex:

(-> event .-target .value)
or maybe another way
(.-target.value event)


I think the idomatic way of accessing JS props is via goog.object, like goog.object/get and goog.object/getValueByKeys


(TLDR for reasoning is that Closure compiler might mangle the property names if there is no externs provided)


With autoinferencing you don't really need goog.object/get - you can just use regular interop.


.-target.value is technically incorrect but it works and is likely to work forever. There's also (.. event -target -value).

AJ Jaro15:10:39

@U2FRKM4TW Thanks for the response. Do you know where this interop is documented? I couldn’t find it this morning, but I swore it existed


I couldn't find any "official" link, but some resources link to this article:

👍 3

I think these are official docs: > The following are also supported…


Hello everyone, I have a best practices question regarding CSS: I'm playing around with a re-frame template that uses shadow-cljs and seem to have successfully gotten a JS library called ag-grid-react imported to display a table. What is the best way to handle the related CSS files when importing libraries in this way? I ended up running node-sass on the .scss files I wanted in node_modules and put the converted CSS files into resources/public/css and referenced them in index.html. Is there a better way? Some other process or framework I should be using? For reference, so you know my starting point, I've npm installed the needed libraries and done this before using the component/adapter later:

  ["ag-grid-react" :as ag-grid :refer (AgGridReact)]
  [reagent.core :as r])

(def ag-adapter (r/adapt-react-class AgGridReact))


That's pretty much it. I haven't seen any widely discussed alternative.


Ok, thanks! I will tentatively continue in this way for now then.


Another question: Say, hypothetically, you were going to build a medium to large in complexity front-end SPA and you were evaluating using CLJS over all the alternatives. Would you pick shadow-cljs or figwheel, or some kind of hybrid (or something else)? Would it matter much if you later changed your mind? I, that is to say ehm, you, would largely be working alone or with one to two teammates. The context is that this would be an internal enterprise application, and you would have to maintain it until your untimely demise or until you no longer worked for company X.


I'd pick shadow-cljs, it makes it much easier to use npm packages for the frontend if needed (and when building an SPA, chances are you will need to do so, at least that was my experience when using Fulcro)

👍 3
✔️ 3

(Though for an internal application especially, I'd do my best to convince them to not make it a web app, but we know how that one goes these days...)


Curious, what kind of desktop application would you push for? JavaFx?


Yes, JavaFX, probably with cljfx. Because I pretty much discontinued my wrapper ClojureFX. But for as long as it is so cumbersome to native-compile Clojure, that won't really be an option for mobile yet. (Gluon has a Maven plugin that compiles JavaFX applications cross-platform for Windows, Mac, Linux, iOS, Android, but it uses GraalVM native-compile)


We're trying this at work (converting an internal CLJS web thing to be a cljfx desktop app.) Not done yet, but looking promising so far.


Hello @U08JKUHA9, may I ask what the reason for the switch was? Was there some particular road-block that was too painful to do with CLJS in the web? My motives for asking is that I am seriously considering using CLJS for my next project at work and I'm trying to gain more insight into its current state.


@U01D0UM4277 Well we went from wanting to manage some metadata in the database to managing it as data files in our source control repository instead. For just changing text files on your developer laptop, a webapp doesn't seem like a natural fit, even though it is possible. So no problem with ClojureScript + re-frame, we're still using that heavily for the non-internal screens.

👍 3

@U08JKUHA9 I see, yes that makes sense, thank you for taking the time to answer and for the additional encouragement in using ClojureScript + re-frame.


Thanks @zilti, I hear your point about not making it a web app, though as you said, and based on how the application is going to be accessed (and some other factors), I have little say in this matter.

Александр Стоянов21:10:47

Hello! How can i execute command (like 'ls' or 'ps') from clojurescript and get back answer? Or how can i execute function from clj file or whole file?


Do you execute your CLJS in a browser or on NodeJS? If the former, then you will have to have some kind of a backend because browsers don't have any access to the shell commands. Not sure about your second question. You mean executing a CLJ function from CLJS? If so, same answer - you have to be running some backend server and communicate the command and the result client <-> server.

Александр Стоянов21:10:30

I created lein re-frame template wtih shadow-cljs and didn't understand how to run backend. I just need to connect postgresql and don't know how to do it in this case. Can you help me please?


Don't get me wrong, I'd like to help, but that question is almost like the "how do I write a program?" one. There's a great number of approaches, each with its own pros and cons, and ultimately you're the one that has to choose one. Seems like this tutorial makes the choice for you and guides you along the way, so maybe it'll help you:

Александр Стоянов22:10:44

My problem a little bit smaller: i can run frontend (by shadow-cljs) but cannot run backend. I have error like "project/server.clj couldnt locate on classpath". So i enter the "server" ns in repl and run (-main) command and it says something like "unable to resolve symbol". What i can do to run backend in this case?


But that's exactly what I'm saying. shadow-cljs doesn't have any backend built into it that can run arbitrary code. It's just a build tool that has a convenient web server that just serves static files, that's it. What is the server namespace that you use? Where did you get the idea about "entering the ns" in running (-main)?

Александр Стоянов22:10:35

It means that i need a second project to run backend or how? What i do: 1. run frontend (lein deps && lein watch) 2. run "lein run" - get an error 3. run "lein repl" 4. go to "server" namespace 5. run (-main) - get an error too

Александр Стоянов22:10:53

In my first project sometimes i did run backend with (-main) in repl, it worked. But then i havent frontend part

Александр Стоянов22:10:21

Now i have frontend without backend. I just dont know how to run frontend and backend in one project although i can write clojure-code.


I don't understand what the item 4 means. And no, you don't need a second project - your project structure can be any you want. Either way, you should definitely follow that tutorial. Alternatively, try asking in the #beginners channel.