       callback?()
 
   open: (databaseName, callback) ->
-    db = new Database databaseName
+    db = new Database(databaseName, ledger.storage.databases)
     db.load callback
     db
 

      clearTimeout @_scheduledFlush if @_scheduledFlush?
      serializedData = {}
      serializedData[@_name] = @_db
      ledger.storage.databases.set serializedData, callback

  scheduleFlush: () ->
    clearTimeout @_scheduledFlush if @_scheduledFlush?


class Database extends EventEmitter

  constructor: (name, store) ->
    @_name = name
    @_store = store

  load: (callback) ->
    @_store.get @_name, (json) =>
      try
        @_db = new loki(@_name)
        @_db.loadJSON JSON.stringify(json[@_name]) if json[@_name]?
