Skip to content

Instantly share code, notes, and snippets.

@kennethkoontz
Created November 15, 2012 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennethkoontz/4076060 to your computer and use it in GitHub Desktop.
Save kennethkoontz/4076060 to your computer and use it in GitHub Desktop.
unified db
var mongo = require('mongodb');
var ObjectId = require('mongodb').ObjectID;
var Server = mongo.Server;
var Db = mongo.Db;
var server = new Server('localhost', 27017, {auto_reconnect: true});
var Database = function(name) {
db = new Db(name, server, {safe: true});
db.open(function(error, client) {
if (error)
console.log(error);
else
this.client = client;
});
};
Database.prototype.ObjectId = function(string) {
return (string) ? ObjectId(string) : new ObjectId().toString();
}
/* Create a document.
*
* @params
* options {
* collection: {string} - name of collection to create document,
* document: {object} - object representing a document
* }
* callback - function to return
*
* @returns
* callback - {function} (error, result)
*/
Database.prototype.create = function(options, callback) {
// XXX Add checks to verify correct options are passed in
this.client.createCollection(options.collection, function(error, collection) {
collection.insert(options.document, {safe: true}, function(error, result) {
if (error)
return callback(error, null);
return callback(null, result);
});
});
};
.
.
.
module.exports = Database;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment