Skip to content

Instantly share code, notes, and snippets.

@sbisbee
Created March 2, 2011 01:34
Show Gist options
  • Save sbisbee/850293 to your computer and use it in GitHub Desktop.
Save sbisbee/850293 to your computer and use it in GitHub Desktop.
A code example for http://www.saggingcouch.com
<?php
/*
* A real quick example to get and update a doc.
*/
require_once('./src/Sag.php');
$sag = new Sag('127.0.0.1', '5984');
// Select the database that holds our blog's data.
$sag->setDatabase('blog');
try {
//Get a blog post (a StdClass)...
$post = $sag->get('postID')->body;
//...update its info...
$post->views++;
//..and send it back to the couch.
if(!$sag->put($post->_id, $post)->body->ok) {
error_log('Unable to log a view to CouchDB.');
}
}
catch(SagCouchException $e) {
//The requested post doesn't exist - oh no!
if($e->getCode() == "404") {
$e = new Exception("That post doesn't exist.");
}
throw $e;
}
catch(SagException $e) {
//We sent something to Sag that it didn't expect.
error_log('Programmer error: '.$e->getMessage());
}
?>
<html>
<body>
<p>Check your console for output.
<script src="./src/sag.js"></script>
<script>
/*
* A real quick example of how to grab a document and update it with Sag-JS
* in the browser.
*/
// We are going to use a local web proxy, hence port 80.
var couch = sag.server('localhost', '80');
/*
* The local web proxy sends everything from http://localhost:80/db to
* http://localhost:5984/
*/
couch.setPathPrefix('/db');
// Set the database
couch.setDatabase('blog');
// Provide the credentials
couch.login({
user: 'admin',
pass: 'passwd',
type: couch.AUTH_BASIC
});
// Go and get the doc.
couch.get({
url: '/blogPostID',
callback: function(resp, success) {
var doc;
// Update the doc.
if(success) {
doc = resp.body;
}
else if(resp._HTTP.status == 404) {
doc = {
_id: 'blogPostID',
views: 0
};
}
else {
console.log('Error getting blog post: HTTP ' + resp._HTTP.status);
}
doc.views++;
// Send the updated doc.
couch.put({
id: doc._id,
data: doc,
callback: function(resp, success) {
if(success) {
console.log(resp);
console.log('Done!');
}
else {
console.log('Error updating blog post: HTTP ' + resp._HTTP.status);
}
}
});
}
});
</script>
</body>
</html>
/*
* A real quick example of how to grab a document and update it with Sag-JS in
* Node.JS.
*
* Assumes installed with npm, otherwise require('./src/sag.js');
*/
var sag = require('sag').server(); //use localhost defaults
// Set the database
sag.setDatabase('blog');
// Provide the credentials
sag.login({
user: 'admin',
pass: 'passwd',
type: sag.AUTH_BASIC
});
// Go and get the doc.
sag.get({
url: '/blogPostID',
callback: function(resp, success) {
var doc;
// Update the doc.
if(success) {
doc = resp.body;
}
else if(resp._HTTP.status == 404) {
doc = {
_id: 'blogPostID',
views: 0
};
}
else {
console.log('Error getting blog post: HTTP ' + resp._HTTP.status);
}
doc.views++;
// Send the updated doc.
sag.put({
id: doc._id,
data: doc,
callback: function(resp, success) {
if(success) {
console.log('Done!');
}
else {
console.log('Error updating blog post: HTTP ' + resp._HTTP.status);
}
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment