Search code examples
clojureclojurescriptom

How to access db.clj methods in core.cljs in clojure


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"

Solution

  • 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.