Skip to content
A simple ClojureScript CouchDB client
Clojure Shell JavaScript
Find file
Pull request Compare This branch is 1 commit ahead, 22 commits behind pandeiro:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
couchapp-template
script
src/jaki
.gitignore
README.md

README.md

Jaki 0.0.1

Browser-based CouchDB applications with ClojureScript

Jaki's principal components are

  • src/jaki/req.cljs: a request client that automatically converts Clojure and JavaScript datatypes
  • src/jaki/couch.cljs: a basic library covering most of the CouchDB API using Clojure idioms
  • couchapp-template & script/jaki: a template and script for generating new projects (to be used with the couchapp upload tool).

Usage

Just include the src/jaki directory in your ClojureScript project, and reference it:

(ns myapp.core
  (:require [jaki.couch :as couch]))

(couch/get-all-dbs (fn [dbs] (js/alert (apply str (interpose ", " dbs)))))

Couch CRUD

Jaki abstracts CRUD operations to three main functions: get-docs, post-docs, and delete-docs.

get-docs

At its simplest, Jaki guesses the current database (or taps a default database if set), and requests all documents:

(get-docs (fn [resp] (js/alert (str (-> resp :rows count) " documents found!"))))

There's also some sugar for limiting the number of results:

(get-docs 10 (fn [resp] 
               (js/alert (apply str (map #(str %2 ". " (:id %1) "\n") (:rows resp) (iterate inc 1))))))

And there's sugar for specifying just the document(s) you want by id, like so:

(get-docs ["_design/app" "_design/test"]
          (fn [docs] (js/alert (str (count (map #(->> % :views keys) docs)) " total views found"))))

For more granular control, specify a view-map with a database and (optionally) design document, view, and options:

(get-docs {:db "articles" :design "blog" :view "most-recent" :descending true :limit 10}
          (fn [resp] (js/alert (->> resp :rows first :title))))

post-docs

You can save a document (map) or vector of documents, with or without a callback, and with or without specifying the database:

(post-docs {:_id "b9725ae4542ce6252937" :_rev "3-a2362326892374879692"} (fn [resp] (js/alert "Updated!")))

(post-docs "albums" [{:title "St. Louis Blues" :album "Sunshine of my Soul" :recorded -68508000000}
            {:title "Parisian Thoroughfare" :album "The Jaki Byard Experience" :recorded -40683600000}])

delete-docs

Likewise, documents can be deleted in the same way they are posted:

(delete-docs {:_id "b9725ae4542ce6252937" :_rev "3-a2362326892374879692"})

(delete-docs "albums" [{:_id "ce672987ad32919732523b6" :_rev "2-ab4452cd382236274346}
                       {:_id "ce672987ad32919732527f9" :_rev "1-f32353a25bc544574232}]
             (fn [resp] (js/alert "Deleted!")))

There's also sugar for when you don't have the rev handy, in which case you can just use the id string, though this has a performance penalty of an extra request behind-the-scenes to retrieve the _rev:

(delete-docs ["ce672987ad32919732523b6" "ce672987ad32919732527f9"])

App Generator Script

To generate an application skeleton, edit jaki/script to use the appropriate template location, make sure it's executable, place the script somewhere in your $PATH, and run it:

jaki new myapp

This will create a new subdirectory myapp in the current directory, containing app and src subdirectories. Inside app will be a basic couchapp structure with an index.html that already contains a reference to your app and the entrypoint invocation myapp.start().

From here, simply specify authentication credentials and target database in .couchapprc and compile the src subdirectory and you are ready to push your couchapp.

API Todo

  • Listening for _changes

  • Replication

Contributing

This is my first real attempt at writing a library and using ClojureScript. I am very grateful to anyone who wants to use, test, or contribute to this project. You can get in touch with me here on github or on freenode's #clojure and #couchdb channels.

License

Copyright (C) 2011 Murphy McMahon

Distributed under the Eclipse Public License, the same as Clojure.

Something went wrong with that request. Please try again.