Created
September 23, 2012 14:49
-
-
Save jchris/3771760 to your computer and use it in GitHub Desktop.
pretend code: launch the app, connects to couchbase, also some examples
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
var couchbase = require("couchbase"), | |
config = { | |
hosts : ["localhost:8091", "localhost:9000"], | |
bucket : "TapLibz", | |
password : "buzz-the-tower", | |
}; | |
// would be neat to have default connection level | |
// options (place to set observe and expiration, etc) | |
var defaultOptions = { | |
expiration : 1000 * 60 * 24 | |
} | |
couchbase.connect(config, defaultOptions, function(error, bucket) { | |
if (error) { | |
console.log("couldn't open connection to bucket", config) | |
process.exit(1); | |
} | |
bucket.on("error", function() { | |
// see start.js for more realistic error handling | |
}) | |
// would be nice to expose certain stuff here | |
var replication_factor = bucket.info.replication_factor; | |
// watch how we keep track of the new CAS that results from our SET | |
bucket.get("foo", function(error, doc, meta) { | |
doc.baz = "for now"; | |
meta.expiration = meta.expiration + 1000; | |
meta.ignores_crap = true; | |
bucket.cas(meta.id, doc, meta, function(err, newMeta) { | |
doc.edit_it_again = "ok"; | |
bucket.cas(newMeta.id, doc, newMeta, function(err, newNewMeta) { | |
console.log("got foo and updated it twice"); | |
}) | |
}) | |
}) | |
// lets do the same thing with OBSERVE | |
bucket.get("bazbar", function(error, doc, meta) { | |
doc.fizz = "yes"; | |
// note we are flattening the options and meta namespace... | |
meta.observe = { disk : 2, replica : -1}, | |
bucket.cas(meta.id, doc, meta, function(err, newMeta) { | |
doc.edit_it_again = "ok"; | |
newMeta.observe = { disk : 2, replica : -1}, | |
bucket.cas(newMeta.id, doc, newMeta, function(err, newNewMeta) { | |
console.log("got foo and updated it twice, lastest CAS", newNewMeta.cas); | |
}) | |
}) | |
}) | |
// view query | |
var query = bucket.query("myapp","myview",{reduce : false}, function(error, view) { | |
// this callback is called once the response is complete | |
}) | |
// or you can be more stream-based | |
query.on("start", function(info) { | |
console.log("there are", info.total_rows, "rows in the index") | |
console.log("your response starts at row", info.offset) | |
}) | |
query.on("error", function(error){ | |
if (error.query) { | |
// query connection level error | |
} else if (error.row) { | |
// row level error | |
} | |
}); | |
query.on("row", function(row) { | |
console.log(row.key); | |
}); | |
query.on("end", function() { | |
}) | |
}); |
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
var gameLogic = require("gameLogic"); | |
exports.start = function(bucket, appConfig) { | |
http.createServer(function (req, res) { | |
var body = ""; | |
req.on('data', function (chunk) { | |
// collect the HTTP POST data | |
body += chunk; | |
}); | |
req.on('end', function () { | |
console.log('POSTed: ' + body); | |
bucket.get("cookie:"+req.cookie.userid, function(err, userDoc, userMeta) { | |
// get current game doc | |
bucket.get(userDoc.games[0], function(err, gameDoc, gameMeta) { | |
// lets play the game! | |
var newGameDoc = gameLogic.updateGameFromMoves(gameDoc, userDoc, body); | |
// set the last_played (should we do this inside gameLogic?) | |
newGameDoc.last_played = new Date(); | |
bucket.set(gameMeta.id, gameDoc, function(err) { | |
res.writeHead(200); | |
res.end("<p>Your move is saved, "+userDoc.name+".</p>"); | |
}) | |
}) | |
}) | |
}); | |
}).listen(8080); | |
}; | |
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
var myApp = require("myApp"), | |
couchbase = require("couchbase"), | |
config = { | |
hosts : ["localhost:8091", "localhost:9000"], | |
bucket : "TapLibz", | |
password : "buzz-the-tower", | |
bucket : | |
}; | |
couchbase.connect(config, function(error, bucket) { | |
if (error) { | |
console.log("couldn't open connection to bucket", config) | |
process.exit(1); | |
} | |
bucket.on("error", function(error) { | |
// handle global connect errors, and any errors that | |
// can't be traced to a particular operation | |
if (error.fatal) { | |
// only exit if this is really a connection error | |
console.log("couchbase fatal connection", error) | |
process.exit(1); | |
} else { | |
console.log("couchbase bucket error", error) | |
// in real life you would probably start | |
// a graceful shutdown of the server and | |
// relaunch it here. | |
// wait for all open connections to complete | |
myApp.stopAcceptingConnections(function() { | |
console.log("clean shutdown") | |
process.exit(0); | |
}) | |
} | |
}); | |
bucket.get("app-config", function(error, doc, meta) { | |
if (error) { | |
console.log("couldn't get app config!"); | |
console.log(error); | |
process.exit(1); | |
} else { | |
console.log("starting myApp with app-config", doc); | |
var app = myApp.start(bucket, doc); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment