      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) ->

  if @_single
    backup = @_cache[id]
    delete @_cache[id]
    try
      json = JSON.stringify @_cache, null, 2
    catch e
      @_cache[id] = backup
      return if cb? then cb e else e
    done = (err)=>
        if err?
          @_cache[id] = backup
          if cb? then cb err else err
        else cb? null
    if cb?
      fs.writeFile file, json, 'utf8', done
    else
      try
        done fs.writeFileSync file, json, 'utf8'
      catch e
        done e
  else
    done = (err) =>
      return (if cb? cb err else err) if err?


  all: (cb=->) ->
    if @_single
      getObjectFromFile.call @, undefined, cb
    else
      readIDs @_dir, (err, ids) =>
        return cb err if err?
