Skip to content

Commit

Permalink
[init] First commit for couchdb starter.
Browse files Browse the repository at this point in the history
  • Loading branch information
AvianFlu committed Oct 28, 2011
1 parent 1bc19f5 commit c3ba130
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
20 changes: 20 additions & 0 deletions couchdb/bin/couchdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var hello = require('../lib/couchdb');

hello.insertDoc({
id: 'user3',
type: 'regular person',
skills: 'hamburgers'
},
function (err, body) {
if (err) {
return console.error(err.message);
}
console.dir(body);
hello.getAllDocs(function (err, body) {
if (err) {
return console.error(err.message);
}
console.dir(body);
});
});

5 changes: 5 additions & 0 deletions couchdb/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"url": "avianflu.iriscouch.com",
"db": "testdb1"
}

87 changes: 87 additions & 0 deletions couchdb/lib/couchdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@


var request = require('request'),
nconf = require('nconf'),
user, pass, url, db,
connectionString;

nconf.use('file', { 'file': './config.json' });
nconf.load();

user = nconf.get('user');
pass = nconf.get('pass');
url = nconf.get('url');
db = nconf.get('db');

if (user && pass) {
connectionString = 'http://' + user + ':' + pass + '@' + url + '/' + db;
}
else {
connectionString = 'http://' + url + '/' + db;
}

var hellorequest = exports;

hellorequest.createDB = function (callback) {
request.put(connectionString, function (err, res, body) {
if (err) {
return console.error(err.message);
}
body.statusCode = res.statusCode;
callback(null, body);
});
}

hellorequest.insertDoc = function (doc, callback) {
var testDoc;

if (typeof callback === 'undefined') {
callback = doc;
doc = null;
}

testDoc = {
id: 'You',
type: 'Node Ninja',
skills: 'Node.js'
};

request({
uri: connectionString,
method: 'POST',
json: doc || testDoc
},
function (err, res, body) {
if (err) {
return console.error(err.message);
}
body.statusCode = res.statusCode;
callback(null, body);
});
}

hellorequest.getDoc = function (docID, callback) {
request({
uri: connectionString + '/' + docID
},
function (err, res, body) {
if (err) {
return callback(err);
}
body.statusCode = res.statusCode;
callback(null, body);
});
}

hellorequest.getAllDocs = function (callback) {
request({
uri: connectionString + '/' + '_all_docs?include_docs=true'
},
function (err, res, body) {
if (err) {
return callback(err);
}
body.statusCode = res.statusCode;
callback(null, body);
});
}

0 comments on commit c3ba130

Please sign in to comment.