Created
November 15, 2012 01:29
-
-
Save kennethkoontz/4076060 to your computer and use it in GitHub Desktop.
unified db
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 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