      console.error e
      cb e
 
+saveObjectToFile = (o, file, cb) ->
+  try
+    json = JSON.stringify o, null, 2
+    if cb?
+      fs.writeFile file, json, 'utf8', cb
+    else
+      try
+        fs.writeFileSync file, json, 'utf8'
+      catch e
+        e
+  catch e
+    if cb? then cb e else e
+
 id2fileName = (id, dir) -> path.join dir,"#{id}.json"
 
 save = (id, o, cb) ->


  all: (cb=->) ->
    if @_single
      fs.readFile @_getFileName(), "utf8", (err, content) ->
        return cb err if err?
        try
          cb null, JSON.parse content
        catch e
          cb e
    else
      readIDs @_dir, (err, ids) =>
        return cb err if err?

  if @_single
    backup = @_cache[id]
    delete @_cache[id]
    done = (err)=>
      if err?
        @_cache[id] = backup
        if cb? then cb err else err
      else cb? null
    if cb?
      saveObjectToFile @_cache, file, done
    else
      err = (o = saveObjectToFile @_cache, file) instanceof Error
      done err, o
  else
    done = (err) =>
      return (if cb? cb err else err) if err?
