NAME

couch_docs - Manage CouchDB views and documents from the filesystem.

Author: Chris Strom

github.com/eee-c/couch_docs

(formerly couch_design_docs)

DESCRIPTION

I have two primary use cases: uploading fixture data for integration testing and editing CouchDB documents in my favorite editor. It could also be useful as a cheap back-up mechanism, though it likely would not work well with large datasets (see PROBLEMS/FUTURE).

Fixture Strategy

Before an integration test, I create a new CouchDB database with a randomly generated name. I use couch_docs to upload fixture data to that database:

CouchDocs.put_dir(DB_URL, DIRECTORY)

Then, I allow the test run to proceed normally. At the end of the run, I teardown the test database.

Since CouchDB database creation / destruction is so cheap, this allows for quite fast integration tests – even including necessary design documents.

Editing with Emacs

I much prefer editing JSON documents in Emacs (lesser editors should work) over using the Futon web interface built into CouchDB. To initiate this, I watch the directory containing the JSON document(s):

couch-docs push -w localhost:5984/db

The -w option uses directory watcher to watch for any changes on the file system. When a change occurs, the updated document is loaded to CouchDB immediately. This is very useful for rapid editing/prototyping of documents that are subsequently used in another medium (e.g. for a blog post).

FEATURES

  • Upload JSON documents stored on the filesystem into a CouchDB database.

    • Map .js files stored on the filesystem (e.g. _design/recipes/count_by_month/map.js) into CouchDB design documents.

      • Support for the !code macro from couchapp (useful for DRYing up map/reduce views as well as list/show documents.

  • Dump documents stored in CouchDB to the filesystem.

    • Attachments are stored as real files (not inline, mime64 encoded attributes on the JSON document)

    • De-resolution of !code macros.

  • Command line script (couch-docs) to push / dump CouchDB database.

    • Multiple options including a directory watcher for uploading directory changes

SYNOPSIS

From the command line:

# For dumping the contents of a CouchDB database to the filesystem
couch-docs dump http://localhost:5984/db

# For loading documents from the filesystem into CouchDB
couch-docs push http://localhost:5984/db

In code:

DB_URL = "http://localhost:5984/db"
DIRECTORY = "/repos/db/couchdb/"

# /repos/db/couchdb/_design/lucene/transform.js
# /repos/db/couchdb/foo.json

CouchDocs.put_dir(DB_URL, DIRECTORY)

# => lucene design document with a "transform" function containing
#    the contents of transform.js
#     - AND -
#    a document named "foo" with the JSON contents from the foo.json
#    file

CouchDocs.dump(DB_URL, "/repos/db/bak")

# => JSON dump of every document at DB_URL

REQUIREMENTS

  • CouchDB

  • JSON

  • RestClient

  • DirectoryWatcher

INSTALL

  • sudo gem install couch_docs

PROBLEMS/FUTURE

  • Does not honor CouchDB document revisions. Instead it deletes previous revisions and uploads an entirely new one.

  • A progress bar would be helpful.

  • Unit testing of view javascript would be very nice.

  • Will almost certainly not work for large datasets (>10k documents).

LICENSE

(The MIT License)

Copyright © 2010

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.