Here is a Clojurescript database connection in NodeJS and below it the JS I ported it from. 2 questions:

1. I believe core.async is the way instead of replicating NodeJS nested callbacks. What is the equivalent using core.async? 2. I couldn't get ((.rows result) 0) to work for result.rows[0]. result.rows worked as-is in cljs but I don't understand why.

gvim



(ns ndpg.core
  (:require [cljs.nodejs :as n]))

(n/enable-util-print!)

(def pg (n/require "pg"))
(def con "postgres://admin:p@localhost:5432/astro")
(def client (pg.Client. con))

(.connect client
  (fn [err]
    (if err
      (.error js/console "Couldn't connect" err)
      (.query client "select * from births"
        (fn [err result]
          (if err
            (.error js/console "Query error" err)
            (do
              (.log js/console result.rows)
              (.end client))))))))

(defn -main []
  (println (range 1 100)))

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

;;;;;;;;;;;;;;;;;;;;;;; JS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

var con = "postgres://admin:p@localhost:5432/db";

var client = new pg.Client(con);
client.connect(function(err) {
  if(err) {
    return console.error("Couldn't connect", err);
  }
  client.query('SELECT * FROM births', function(err, result) {
    if(err) {
      return console.error('Query error', err);
    }
    console.log(result.rows[0]);
    client.end();
  });
});

--
Note that posts from new members are moderated - please be patient with your 
first post.
--- You received this message because you are subscribed to the Google Groups "ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to