var mongodb = require('mongodb').MongoClient;
var objectId = require('mongodb').ObjectID;

var MongoDataProxy = function(collectionName) {
  this.connectionString = 'mongodb://localhost:27017/orderEntry';
  this.collectionName = collectionName;
};

MongoDataProxy.prototype.getAll = function(done) {
  var self = this;
  mongodb.connect(self.connectionString, function(err, db) {
    if (err) { done(err); }
    var collection = db.collection(self.collectionName);
    collection.find({}).toArray(function(err, data) {
      data.forEach((item) => {
        item.id = item._id;
        delete item._id;
      });
      db.close();
      done(err, data);
    });
  });
};

MongoDataProxy.prototype.getById = function(id, done) {
  var self = this;
  mongodb.connect(self.connectionString, function(err, db) {
    if (err) { done(err); }
    var collection = db.collection(self.collectionName);
    var oId = new objectId(id);
    collection.findOne({_id: oId}, function(err, data) {
      data.id = data._id;
      delete data._id;
      db.close();
      done(err, data);
    });
  });
};

MongoDataProxy.prototype.insert = function(data, done) {
  var self = this;
  mongodb.connect(self.connectionString, function(err, db) {
    if (err) { done(err); }
    var collection = db.collection(self.collectionName);
    collection.insert(data, function(err, result) {
      var entity = result.ops[0];
      entity.id = entity._id;
      delete entity._id;
      db.close();
      done(err, entity);
    });
  });
};

MongoDataProxy.prototype.update = function(data, done) {
  var self = this;
  mongodb.connect(self.connectionString, function(err, db) {
    if (err) { done(err); }
    var collection = db.collection(self.collectionName);
    collection.update({_id: objectId(data.id)}, data, function(err, result) {
      db.close();
      done(err, data);
    });
  });
};

MongoDataProxy.prototype.destroy = function(id, done) {
  var self = this;
  mongodb.connect(self.connectionString, function(err, db) {
    if (err) { done(err); }
    var collection = db.collection(self.collectionName);
    collection.remove({_id: objectId(id)}, function(err, data) {
      db.close();
      done(err, data);
    });
  });
};

module.exports = MongoDataProxy;

@@ -238,16 +238,43 @@
 onName;%0A
+  this._mongodb = mongodb;%0A
 %7D;%0A%0AMong
@@ -333,32 +333,38 @@
  self = this;%0A  
+self._
 mongodb.connect(
@@ -767,32 +767,38 @@
  self = this;%0A  
+self._
 mongodb.connect(
@@ -921,24 +921,36 @@
 ctionName);%0A
+    try %7B%0A  
     var oId 
@@ -969,16 +969,93 @@
 Id(id);%0A
+    %7D catch(ex) %7B%0A      return done(new Error(%22Invalid id supplied%22));%0A    %7D%0A
     coll
@@ -1095,32 +1095,52 @@
 on(err, data) %7B%0A
+      if (data) %7B%0A  
       data.id = 
@@ -1151,24 +1151,26 @@
 ._id;%0A      
+  
 delete data.
@@ -1170,24 +1170,32 @@
 e data._id;%0A
+      %7D%0A
       db.clo
@@ -1311,32 +1311,38 @@
  self = this;%0A  
+self._
 mongodb.connect(
@@ -1741,32 +1741,38 @@
  self = this;%0A  
+self._
 mongodb.connect(
@@ -2113,16 +2113,22 @@
 this;%0A  
+self._
 mongodb.

add error handling around objectid creation and expose mongodb to subclasses