Created
November 22, 2009 02:55
-
-
Save ato/240401 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns clojars.couch | |
| (:use [com.reasonr.scriptjure :only (js)]) | |
| (:require (couchdb [client :as couch]) | |
| (clojars [maven :as maven]) | |
| (clojure.contrib [error-kit :as kit]))) | |
| (def *db* "clojars") | |
| (defn get-doc | |
| "Wrapper for couch/document-get that's shorter to type and just | |
| returns nil if it doesn't find a document instead of blowing up with | |
| an ugly exception." | |
| ([db id] | |
| (kit/with-handler | |
| (couch/document-get db id) | |
| (kit/handle couch/DocumentNotFound [] | |
| nil))) | |
| ([db id rev] | |
| (kit/with-handler | |
| (couch/document-get db id rev) | |
| (kit/handle couch/DocumentNotFound [] | |
| nil)))) | |
| (defn update-view [design-doc view-name code] | |
| (let [doc-id (str "_design/" design-doc) | |
| old (get-doc *db* doc-id) | |
| new (merge {:language "javascript"} | |
| old | |
| {:views (assoc (:views old) | |
| view-name code)})] | |
| (if old | |
| (when (not= ((old :views) view-name)) code | |
| (couch/document-update *db* doc-id new)) | |
| (couch/document-create *db* new)))) | |
| (defmacro defview | |
| ([design-doc view-name params & body] | |
| `(update-view ~design-doc ~view-name | |
| {:map (js (fn ~params ~@body))}))) | |
| (defview :users :all [doc] | |
| (if (== doc.type "user") | |
| (emit nil doc))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment