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

var cachedDb = null;

module.exports = function(state, action, callback) {
  var dbaction = {
    type: 'MongoDB initialize',
    input: {}
  }
  var changes = action.input.Changes

  state.dispatch(dbaction, function(err, db) {
    for (var collection in changes) {
      for (var id in changes[collection]) {
        var query = { _id: id }
        var updates = changes[collection][id]
        db.collection(collection).update(query, { $set: updates }, { upsert: true })
      }
    }
    callback()
  })
}
