CouchDB CRUD service for FeathersJS using cradle
npm install cradle feathers-couchdb --save
Please refer to the cradle for more details about connection options and Feathers database adapter.
Create CouchDB service:
var cradle = require('cradle');
var service = require('feathers-couchdb');
var app = feathers();
//var Connection = new(cradle.Connection)('http://192.168.1.79:5984');
var Connection = new(cradle.Connection)(
'user.cloudant.com', 443,
{
secure: true,
auth: {
username: 'user',
password: '<pass>'
},
cache: true
}
);
var opts = {
connection: Connection,
Model: 'messages'
};
app.service('/messages', service(opts));
This will create a messages
endpoint and connect to a local messages
database. Each model represents each database in CouchDB that will created automatically if not exist.
To insert new document(s), provide an array as body.
[{
"name": "Luke Skywalker",
"force": "light"
},{
"name": "Han Solo",
"force": "neutral"
},{
"name": "Yoda",
"force": "light"
}]
For single document, you can provide _id
as key rather than generated uuid
by CouchDB.
{
"_id": "vader",
"name": "Darth Vader",
"force": "dark"
}
To update and delete you need this _id
.
To add _design
document for view query, create as normal Create Document with _id
start with _design
{
"_id": "_design/hero",
"views": {
"all": {
"map": "function (doc) { if (doc.name) emit(doc.name, doc); }"
}
},
"validate_doc_update": "function (newDoc, oldDoc, usrCtx) {if (! /^(light|dark|neutral)$/.test(newDoc.force)) throw({forbidden: {error: 'invalid value', reason: 'force must be dark, light, or neutral'}})}"
}
will create .view('_design/hero/_views/all')
map-reduce function.
To query data, simply FIND to _design
document giving its namespace as param q
/messages/?q=hero/all
can also add other params as well $skip
, $limit
, $select
NOTE This plugin also provide non-designed view and try to create temporary view inside database. If _temp_view
option is not available, like cloudant, this will create new view design and remove it immediately after.
Please be aware this process slower than having saved design doc.
{
"$skip": 0,
"$limit": 10,
"$select": ['name', 'force'],
"$or": [
{ "force": 'light' },
{ "force": 'neutral' },
{
"name": {
"$in": ['han','solo','yoda']
}
}
]
};
$sort
is not implemented yet.
Adapted from original works by FeathersJS team
Licensed under the MIT license.