couchilla is a bundler for packing design documents for CouchDB.
Report a bug or suggest a feature
Install couchilla
globally with:
npm install @tetsuo/couchilla -g
Use couchilla
to bundle a CouchDB design document from a directory of JavaScript files.
An example directory structure looks like this:
.
├── filters
│ └── quu.js
├── views
│ ├── foo.map.js
│ └── bar.reduce.js
└── validate_doc_update.js
If you navigate to the example and run couchilla .
in this folder, this will print out a design document (compatible with 3.x
) with the contents of your folder.
Exported function formats should conform to the following spec.
Files that contain view functions are located in the views
folder.
Files whose names end with .map.js
or only .js
are transformed into map functions.
Simply emit
the document ID and optionally a value.
views/foo.map.js
/* global emit */
export default doc => emit(doc._id, 42)
Files whose names end with .reduce.js
are transformed into reduce functions.
Take sum of mapped values:
views/foo.reduce.js
/* global sum */
export default (keys, values, rereduce) => {
if (rereduce) {
return sum(values)
} else {
return values.length
}
}
Files that contain filter functions are located in the filters
folder.
Filter by field:
filters/foo.js
export default (doc, req) => {
if (doc && doc.title && doc.title.startsWith('C')) {
return true
}
return false
}
Just add a validate_doc_update.js
file in the top-level of your project folder.
/* global log */
export default (newDoc, oldDoc, userCtx, secObj) => {
log(newDoc)
log(oldDoc)
log(userCtx)
log(secObj)
throw { forbidden: 'not able now!' }
}
You can opt to use Erlang native functions with the builtin
annotation. For example the sum function above can be rewritten as:
views/foo.reduce.js
/* builtin _sum */
During compilation this will be replaced with a call to the builtin _sum
function of CouchDB.
All code should be inside the exported default function, including your require()
calls. couchilla
can require node modules.