CouchDB Wrapper
This module provides a simple wrapper class for couchdb requests.
Installation
Using npm:
$ npm install --save couchdb-wrapper
Using bower:
$ bower install --save couchdb-wrapper
Using Script-Tag: *
<script src="https://raw.githubusercontent.com/MCStreetguy/couchdb-wrapper/master/dist/couchdb-wrapper.min.js" charset="utf-8"></script>
Using short URL Script-Tag: *
<script src="https://goo.gl/1YrYHF" charset="utf-8"></script>
(* functionality is not granted!)
Usage
This is currently missing any documentation about the babel-compiled module.
Explanations below refer to the Node.js module provided in this package.
Require the module:
import CouchDB from 'couchdb-wrapper';
Create a new instance:
var db = new CouchDB({
host: 'your database server',
port: '5984 by default',
username: 'login username',
password: 'login password'
});
All options have to be set, otherwise an error will be thrown.
username
and password
can be empty strings if you don't need authentication on your CouchDB server.
Command Reference
All functions are available as synchronous and asynchronous versions.
If the callback parameter is provided and a function, the call will be made asynchronously. Otherwise the request is sent synchronously.
Please notice! Synchronous requests block the execution of code which creates "freezing" on the screen and an unresponsive user experience. Source (MDN)
But since this leads straightly into "callbacks of doom" (endless nesting of callback functions) a little hint: Firing the first function (info or allDbs is always a good start for this) asynchronously splits the execution from the main process. Therefore you may use synchronous calls within a callback without freezing.
The following object scheme is provided to the callback or returned by the function, depending on the asynchrony.
Key | Description |
---|
status | 'done' if the request succeeded, 'pending' if the request still has to be processed by the server or 'error' if the request failed. |
code | Exposes the requests http-status-code as number (e.g. for further comparison). |
msg | Contains a readable status message or an empty string if unnecessary. |
response | The response data or undefined . |
.info([callback])
Returns server information.
.allDbs([callback])
Recieve all accessible databases stored on the server.
.getDb(identifier,[callback])
Recieve a database object from the server.
identifier
: The name of the database.
.createDb(identifier,[callback])
Create a new database on the server.
identifier
: The name of the database.
.getDocs(identifier,[callback])
Recieve all documents within the given database.
identifier
: The name of the database
.postDoc(identifier,data,[callback])
Store an object in the given database.
identifier
: The name of the database
data
: The object that should be stored in the database. If it contains _id
the document will be stored under that ID, otherwise a new ID is generated and returned in the response.
.getDoc(identifier,docIdentifier,[callback])
Store an object in the given database.
identifier
: The name of the database
docIdentifier
: The uid of the document
.deleteDoc(identifier,docIdentifier,revision,[callback])
Store an object in the given database.
identifier
: The name of the database
docIdentifier
: The uid of the document
revision
: The latest revision id of the document
.updateDoc(identifier,docIdentifier,newdata,[callback])
Store an object in the given database.
identifier
: The name of the database
docIdentifier
: The uid of the document
newdata
: The new data to be written
(newdata.rev
: must contain the latest revision id)
Static Command Reference
The following functions are intended as helper methods and can only be called on the Class itself, not an instance of the Class.
.parseRevision(revision)
Parses a revision id into an integer value representing the version count.
revision
: The revision identifier
.validateRevision(revision)
Validates a revision id. Returns true if it matches the following RegExp:
/^([0-9]+-[a-z0-9]+)/
revision
: The revision identifier