Skip to content

Instantly share code, notes, and snippets.

@sbisbee
Last active November 1, 2015 20:55
Show Gist options
  • Save sbisbee/1515106 to your computer and use it in GitHub Desktop.
Save sbisbee/1515106 to your computer and use it in GitHub Desktop.
Sag-JS Docu Examples 1
var docs = [
{ _id: 'doc1', foo: 'bar' },
{ _id: 'doc2', hi: 'there' }
];
couch.bulk({
docs: docs,
callback: function(resp, succ) {
//...do stuff
}
});
//get the doc we're going to overwrite
couch.get({
url: 'doc2',
callback: function(resp, succ) {
//and now do the overwrite
couch.copy({
srcID: 'doc1',
dstID: resp.body._id,
dstRev: resp.body._rev
callback: function(resp, succ) {
//do stuff
}
});
}
});
couch.decode(false);
couch.get({
url: '/someDoc',
callback: function(resp) {
console.log(typeof resp); //string
}
});
couch.get({
url: '/someDoc',
callback: function(resp, success) {
if(!success) {
console.log('it\'s already gone');
}
else {
couch.delete(resp.body._id, resp.body._rev, function(resp, success) {
if(success) {
console.log('deleted!');
}
else if(resp._HTTP.status == 404) {
console.log('someone deleted it before we could');
}
else {
console.log('issue deleting: HTTP ' + resp._HTTP.status);
}
});
}
}
});
//Access a doc
couch.get({
url: '/somedoc',
callback: function(resp, success) {
if(success) {
console.log(resp.body);
}
else {
console.log('ERROR! HTTP ' + resp._HTTP.status);
}
}
});
//Access a view
couch.get({
url: '/_design/app/_view/count',
callback: function(resp, success) {
if(success) {
//do something with the results in resp.body
}
else {
console.log('not so much');
console.log(resp.headers);
}
}
});
couch.head({
url: '/',
callback: function(resp, succ) {
if(succ) {
console.log('the db exists and is up!');
}
else if(resp._HTTP.status == 404) {
console.log('the db does not exist!');
}
else {
console.log('HTTP error ' + resp._HTTP.status);
}
}
});
// Initiating Sag-JS in the browser
<script src="./sag.js"></script>
<script>
var couch = sag.server('localhost', '5984');
</script>
// Initiating Sag-JS in Node
var sag = require('sag');
var couch = sag.server('localhost', '5984');
// Initiating Sag-JS with SSL in Node
var sag = require('sag');
var couch = sag.server('localhost', '443', true); //443 is the default HTTPs port
couch.post({
data: {
this: 'will become a document'
},
callback: function(resp, success) {
//...do stuff
}
});
var doc = {
_id: 'something',
foo: 'bar'
};
couch.put({
id: doc._id,
data: doc,
callback: function(resp, success) {
//...do stuff
}
});
couch.setDatabase('mydb', true, function(exists) {
if(exists) {
// the database was either just created or already existed
console.log('time to do work!');
}
else {
console.log('whoops, there was an error creating the db!');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment