Skip to content

@kennethkoontz /gist:4076060
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.