-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[init] First commit for couchdb starter.
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"url": "avianflu.iriscouch.com", | ||
"db": "testdb1" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |