1

For development, I want to keep the various components (views, lists, shows) that make up a couchDB design document as separate files alongside my other development code in a dir called couchDB for example. When I am happy with changes I want to use couchapp.push to send to database, whilst also being able to version control the design doc component files.

I'm thinking maybe using the node module 'couchapp' and node to scan a directory and its sub-dirs for .js files then using them to construct the design docs and upload to couchDB. Perhaps with sub dirs structured like so:

designDocsJs
  |-- views
  |     |--view1.js
  |     |--view2.js
  |-- shows
  |-- lists

Is it possible to put map and reduce in the same file eg. 'view1.js' or would they need to be in separate files 'view1-map.js' and 'view1-reduce.js' ?

Curretnly I am using the following, but I am not sure how to go about reading the .js files using node and dynamically adding them to the ddoc.views{} ? Any guidance would be appreciated, particularly with a javascript/node solution.

var couchapp = require('couchapp')
    , path = require('path');

  ddoc = {
      _id: '_design/entitiesNew'
    , views: {}
    , lists: {}
    , shows: {} 
  }

  // node module exports
  module.exports = ddoc;

  // REPLACE ME WITH SOME WAY OF READING A
  // STRUCTURED DIR OF .JS FILES AND COMBINING
  // AS NECESSARY
  ddoc.views.byType = {
    map: function(doc) {
      emit(doc.type, null);
    },
    reduce: '_count'
  }

  couchapp.loadAttachments(ddoc, path.join(__dirname, '_attachments'));

and then:

$ cd path/to/here
$ mkdir _attachments
$ couchapp push updateCouchDbDesigndocs.js http://localhost:5984/databaseName
johowie
  • 2,475
  • 6
  • 26
  • 42
  • What is exactly your problem with the actual structure (where map and reduce are distinct files in a folder) required by the `couchapp` tool? Does it really worth writing your own tool? – Aurélien Bénel Feb 16 '13 at 13:28
  • I want to know how i can manage my couchDB '_design/doc/_view' source files as single docs in a similar way to how i manage all my other script files in my web development workflow: with git version control, and build/make/grunt scripts. – johowie Feb 16 '13 at 15:45
  • By seeing your comment to the answer, I realize that you used `couchapp` without understanding how to use it. Have fun with `couchapp`... and never forget to RTFM ;) – Aurélien Bénel Feb 18 '13 at 08:20
  • It is true I am not really using couchapp to make 'couchapps' i am using it simply as a way to synchronise my design docs from my sourceCode -> couchDB – johowie Mar 03 '13 at 01:45
  • 1
    I just tried to explain that couchapp does already what you wanted, but David explained it better in its answer. – Aurélien Bénel Mar 03 '13 at 08:11
  • [this link](http://www.blog.dannygagne.com/archives/43) describes a similar use of couchapp to what I was asking in this question sometime ago – johowie May 02 '13 at 08:04

1 Answers1

4

The couchapp tool does just this. I push using couchapp and no other scripts.

Here is the directory structure:

mycouchappdir
|-- views/
    |-- myview1/
       |-- map.js
       |-- reduce.js
|-- shows/
    |-- myshow1.js
    |-- myshow2.js
|-- _attachments/
    |-- myattachment.png

Your map.js file will look like the following:

function(doc) {
   emit(doc.type, null);
}

Then just run couchapp push from the mycouchappdir.


You may also wish to consider putting your couchapps in their own repositories. Running couchapp push always creates a new revision to the design document. So if you have a different repository for your couchapp then when you make a change to the repository it is a change to the couchapp and you can push it knowing that the new revision is different than the old.

David V
  • 11,531
  • 5
  • 42
  • 66