public
Created

unified db

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
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;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.