MySQL on NodeJS

188 views
Skip to first unread message

Jonathon McKitrick

unread,
Sep 22, 2017, 1:33:07 PM9/22/17
to ClojureScript
Hi all,

I'm slowly but surely weaseling Clojure and Clojurescript into our company's codebase. I'm tasked with a script which will read from a MySQL database and generate a very simple report.

So far, I have a NodeJS REPL project up and running, but I'm not quite sure how to get Node's MySQL library into the project and namespace. Any thoughts?


Jonathon McKitrick

unread,
Sep 25, 2017, 7:16:20 AM9/25/17
to ClojureScript
Here's what I have so far:

(defproject foo "0.1.0-SNAPSHOT"
  :description "FIXME"
  
  :dependencies [[org.clojure/clojure "1.8.0"]
                 [org.clojure/clojurescript "1.9.908"]
                 [com.cemerick/piggieback "0.2.2"]]

  :plugins [[lein-cljsbuild "1.1.7"]
            [lein-ancient "0.6.12"]]
  :repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}

  :source-paths ["src"]

  :cljsbuild
  {:builds
   [{:source-paths ["src"]
     :compiler
     {:target :nodejs
      :output-to "check_templates.js"
      :optimizations :none
      :npm-deps {:mysql "2.14.1"}
      :install-deps true
      :main check-templates.core
      :parallel-build true}}]})

and

(ns check-templates.core
  (:require [cljs.nodejs :as nodejs]
            #_[mysql :as sql]))

(nodejs/enable-util-print!)

(defn -main [& args]
  (.log js/console "Hello world")
  (println "Hello world"))

(set! *main-cli-fn* -main)

I run `lein cljsbuild once` and I get the node_modules directory populated with dependencies.
I can run `lein repl` and then use piggieback to get to the CLJS repl. But I seem unable to get the mysql dependency imported.

If I enable the `require` form to import mysql in the source file and then start the repl and require the project namespace, I get this error:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: Can't resolve 'url' in '/Users/devmbp42/arc/check-templates/node_modules/mysql/lib'
    at onError (/Users/devmbp42/arc/check-templates/node_modules/enhanced-resolve/lib/Resolver.js:61:15)

So it's finding the modules and attempting to load them. But I don't know enough about NodeJS to understand exactly what is broken or how to fix it.

Jonathon McKitrick

unread,
Sep 25, 2017, 7:56:16 AM9/25/17
to ClojureScript
Update: it turns out several of the dependencies of mysql had to be imported at the project level. At least, that's what I did to get past the dependency errors. Now I'm stuck here:

path.js:7
    throw new TypeError('Path must be a string. Received ' + inspect(path));
    ^

TypeError: Path must be a string. Received { './support/isBuffer.js': './support/isBufferBrowser.js' }
    at assertPath (path.js:7:11)
    at Object.join (path.js:1211:7)
    at Deps.<anonymous> ([eval]:115:32)
    at emitOne (events.js:96:13)
    at Deps.emit (events.js:188:7)
    at /Users/devmbp42/arc/check-templates/node_modules/@cljs-oss/module-deps/index.js:338:18

It looks like I got past the dependency issues, but I'm not sure where to go from here....

Jonathon McKitrick

unread,
Sep 25, 2017, 9:27:16 AM9/25/17
to ClojureScript
So I've come to the conclusion that the REPL and CIDER do not pick up any of the NodeJS namespaces. I have to build the project and run it with node instead. Not sure exactly why this is the case....

Phill Wolf

unread,
Sep 25, 2017, 7:32:15 PM9/25/17
to ClojureScript
Are you using Figwheel with Node?

Your project sounds ambitious.  If your program must connect to MySQL, perhaps you could use the real (JVM) Clojure instead with less bother.  But I imagine you have your reasons for jousting with ClojureScript-on-Node-with-nREPL-and-CIDER.  At any rate, it makes good reading, so I look forward to the next installment.

jmcki...@gmail.com

unread,
Sep 26, 2017, 6:28:20 AM9/26/17
to ClojureScript
I'm not using Figwheel with Node, though that sounds like a good next step since CIDER isn't picking up the NodeJS imports.

I decided to try NodeJS since it has a lot of support around the office, and this is a script which should have a short startup time. So far, so good.

On Mon, Sep 25, 2017 at 7:32 PM Phill Wolf <phill...@gmail.com> wrote:
Are you using Figwheel with Node?

Your project sounds ambitious.  If your program must connect to MySQL, perhaps you could use the real (JVM) Clojure instead with less bother.  But I imagine you have your reasons for jousting with ClojureScript-on-Node-with-nREPL-and-CIDER.  At any rate, it makes good reading, so I look forward to the next installment.

--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/T_27DClDpUY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at https://groups.google.com/group/clojurescript.
--
Jonathon McKitrick

Kinley Dorji

unread,
Sep 26, 2017, 6:40:09 AM9/26/17
to ClojureScript
Have you tried Lumo? Might be worth a shot.
Reply all
Reply to author
Forward
0 new messages