Last active
November 1, 2015 20:55
-
-
Save sbisbee/1515106 to your computer and use it in GitHub Desktop.
Sag-JS Docu Examples 1
This file contains hidden or 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
var docs = [ | |
{ _id: 'doc1', foo: 'bar' }, | |
{ _id: 'doc2', hi: 'there' } | |
]; | |
couch.bulk({ | |
docs: docs, | |
callback: function(resp, succ) { | |
//...do stuff | |
} | |
}); |
This file contains hidden or 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
//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 | |
} | |
}); | |
} | |
}); |
This file contains hidden or 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
couch.decode(false); | |
couch.get({ | |
url: '/someDoc', | |
callback: function(resp) { | |
console.log(typeof resp); //string | |
} | |
}); |
This file contains hidden or 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
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); | |
} | |
}); | |
} | |
} | |
}); |
This file contains hidden or 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
//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); | |
} | |
} | |
}); |
This file contains hidden or 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
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); | |
} | |
} | |
}); |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
couch.post({ | |
data: { | |
this: 'will become a document' | |
}, | |
callback: function(resp, success) { | |
//...do stuff | |
} | |
}); | |
This file contains hidden or 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
var doc = { | |
_id: 'something', | |
foo: 'bar' | |
}; | |
couch.put({ | |
id: doc._id, | |
data: doc, | |
callback: function(resp, success) { | |
//...do stuff | |
} | |
}); |
This file contains hidden or 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
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