<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Assertion Through Structure &#187; riot</title>
	<atom:link href="http://www.blog.dannygagne.com/archives/category/riot/feed" rel="self" type="application/rss+xml" />
	<link>http://www.blog.dannygagne.com</link>
	<description>Manipulating the future one day at a time</description>
	<lastBuildDate>Sun, 15 Jan 2012 16:14:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>CouchDB &amp; CouchApp Managing Design Documents</title>
		<link>http://www.blog.dannygagne.com/archives/43</link>
		<comments>http://www.blog.dannygagne.com/archives/43#comments</comments>
		<pubDate>Mon, 11 May 2009 08:06:55 +0000</pubDate>
		<dc:creator>danny</dc:creator>
				<category><![CDATA[couchdb]]></category>
		<category><![CDATA[riot]]></category>

		<guid isPermaLink="false">http://www.blog.dannygagne.com/?p=43</guid>
		<description><![CDATA[I've been working with CouchDB and I was never really happy with how I was managing/version controlling my design documents.  Luckily, I read about CouchApp -- I had initially thought it was a tool for developing CouchDB hosted appilcations, but now know that it can be used to at a very basic level to make [...]]]></description>
			<content:encoded><![CDATA[<p>I've been working with <a href="http://couchdb.apache.org/" onclick="pageTracker._trackPageview('/outgoing/couchdb.apache.org/?referer=');">CouchDB</a> and I was never really happy with how I was managing/version controlling my design documents.  Luckily, I read about <a href="http://github.com/jchris/couchapp/tree/master" onclick="pageTracker._trackPageview('/outgoing/github.com/jchris/couchapp/tree/master?referer=');">CouchApp</a> -- I had initially thought it was a tool for developing CouchDB hosted appilcations, but now know that it can be used to at a very basic level to make it easy to control my desgin documents.  Here are a few notes that might make it easy for others to get started; I'm still learning this tool, so this may not be the optimal procedure (also, I'm not using any of the advanced features of CouchApp [e.g. macros], but will in the future).</p>
<p>There are two ways of using CouchApp  to push design documents to CouchDb: 1.  Generating the design document from multiple files (preferred), and 2. Generating the views from a single JSON file.</p>
<p><strong>Generating the Design Document from its Component Pieces<br />
</strong></p>
<ol>
<li>Install CouchDB &amp; CouchApp - Both are easy to install and well documented, so need for any additional notes.</li>
<li>The directory structure used is used to hold the atomic pieces of the design document and should have a structure as follows:
<ul>
<li>{myapp}  - app directory</li>
<li>{myapp}/{sampledesign} -- design document name</li>
<li>{myapp}/{sampledesign}/views -- directory that holds views</li>
<li>{myapp}/{sampledesign}/views/{viewname}/ -- view name</li>
<li>{myapp}/{sampledesign}/views/{viewname}/map.js -- map function</li>
<li>{myapp}/{sampledesign}/views/{viewname}/reduce.js -- reduce function</li>
</ul>
<p>We create a directory for the application<em> {myapp}</em>, we  then create a directory to hold our design document <em>{sampledesign}</em>, and last we create a directory for our view<em> {viewname}</em>.  Each view must contain one map.js file, and can (optional) contain one reduce.js file.</li>
<li>In this example we're only going to create a map function, so map.js should look like:

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">        <span style="color: #006600; font-style: italic;">//This is a simple function that doesn't do anything</span>
        <span style="color: #006600; font-style: italic;">//useful, but will work on all CouchDB databases.</span>
        <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
           emit<span style="color: #009900;">&#40;</span>doc._id<span style="color: #339933;">,</span> doc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>To generate the design document and push it into CouchDB: Go to the <em>{myapp} </em>directory run the following command (for this example assume: {myapp} = myapp and {sampledesing} = mydesigndoc ):
<p style="text-align: center;"><strong>couchapp push mydesigndoc http://127.0.0.1:5984/myapp </strong></p>
</li>
<li>To view the generate design document you can go to  <a href="http://localhost:5984/_utils/document.html?myapp/_design/mydesigndoc#source" onclick="pageTracker._trackPageview('/outgoing/localhost_5984/_utils/document.html?myapp/_design/mydesigndoc_source&amp;referer=');">http://localhost:5984/_utils/document.html?myapp/_design/mydesigndoc#source</a> (the following url is for a db named <em>myapp</em> with a design document named<em> mydesigndoc</em>)</li>
</ol>
<p>And that's all there is to it.  You can develop and version control the design documents.</p>
<p><strong>Alternate: Generating the views from a single JSON file.</strong></p>
<ol style="text-align: left;">
<li>Install CouchDB &amp; CouchApp - Both are easy to install and well documented, so need for any additional notes.</li>
<li>Create a directory to store the design documents: "myapp/_design"  (this directory can be anything)</li>
<li>Create a directory for each design document: "myapp/_desgin/design_document"</li>
<li>Inside the design document create a JSON file named views (views.json) : "myapp/_design/design_document/views.json"</li>
<li>The contents of thee view document should be a JSON document containing view objects with map and reduce functions. For example, see the code listing below we have the view test with a map function.  Note: This is a useless view, but it will work on any CouchDB database.

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
   <span style="color: #3366CC;">&quot;views&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">&quot;map&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;function(doc) {<span style="color: #000099; font-weight: bold;">\n</span>    emit(doc._id, doc);<span style="color: #000099; font-weight: bold;">\n</span>}<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
       <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>From the "_design" directory run the following command:
<p style="text-align: center;"><em><strong> </strong></em><strong>couchapp push {desgin_document_dir} http://127.0.0.1:5984/{<em>dbname</em>} </strong></p>
<p style="text-align: left;">where {dbname} is the name of your db and {design_document_dir} is the name of the design document directory , so if my database was named: "myapp"  and the design document dir was named: "sampledesign" it would look like:</p>
<p style="text-align: center;"><strong>couchapp push sampledesign http://127.0.0.1:5984/myapp </strong></p>
<p style="text-align: left;">Note: If you are unsure of what the view json structure is you can create your views using <a href="http://couchdb.apache.org/screenshots.html" onclick="pageTracker._trackPageview('/outgoing/couchdb.apache.org/screenshots.html?referer=');">Futon</a> which is installed at: <a href="http://localhost:5984/_utils/" onclick="pageTracker._trackPageview('/outgoing/localhost_5984/_utils/?referer=');">http://localhost:5984/_utils/</a> by creating a temporary view, saving it, and then navigating to the design document it created.  <a href="http://localhost:5984/_utils/document.html?myapp/_design/sampledesign#source" onclick="pageTracker._trackPageview('/outgoing/localhost_5984/_utils/document.html?myapp/_design/sampledesign_source&amp;referer=');">http://localhost:5984/_utils/document.html?myapp/_design/sampledesign#source</a> (the following url is for a db named <em>myapp</em> with a design document named<em> sampledesign</em>)</p>
</li>
</ol>
<p>After following those steps you can easily push json documents to couchdb and use whatever version controll system that you'd like; however, there is a better way of laying out the files.</p>
<p style="text-align: left;">So there you have it, a quick introduction to the two ways of using CouchApp to manage CouchDB Documents.<strong><br />
</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.dannygagne.com/archives/43/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

