flat-file-db
Advanced tools
Comparing version 0.1.1 to 0.1.2
21
index.js
@@ -111,4 +111,5 @@ var fs = require('fs'); | ||
var Database = function(path) { | ||
var Database = function(path, opts) { | ||
events.EventEmitter.call(this); | ||
if (!opts) opts = {}; | ||
@@ -123,2 +124,3 @@ this.path = path; | ||
this._pending = 0; | ||
this._fsync = opts.fsync !== false; | ||
}; | ||
@@ -129,4 +131,3 @@ | ||
var writefd = function(self, buf, entry, oldPointer, oldFreelist, cb) { | ||
self._pending++; | ||
fs.write(self.fd, buf, 0, buf.length, entry.pointer, function(err) { | ||
var done = function(err) { | ||
if (!--self._pending) self.emit('drain'); | ||
@@ -136,2 +137,8 @@ if (err) return cb(err); | ||
cb(); | ||
}; | ||
self._pending++; | ||
fs.write(self.fd, buf, 0, buf.length, entry.pointer, function(err) { | ||
if (err || !self._fsync) return done(err); | ||
fs.fsync(self.fd, done); | ||
}); | ||
@@ -218,4 +225,4 @@ }; | ||
var open = function(path) { | ||
var db = new Database(path); | ||
var open = function(path, opts) { | ||
var db = new Database(path, opts); | ||
db.open(); | ||
@@ -225,4 +232,4 @@ return db; | ||
open.sync = function(path) { | ||
var db = new Database(path); | ||
open.sync = function(path, opts) { | ||
var db = new Database(path, opts); | ||
db.openSync(); | ||
@@ -229,0 +236,0 @@ return db; |
{ | ||
"name": "flat-file-db", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"repository": "git://github.com:mafintosh/flat-file-db.git", | ||
@@ -5,0 +5,0 @@ "author": "Mathias Buus <mathiasbuus@gmail.com>", |
@@ -8,2 +8,4 @@ # flat-file-db | ||
[](http://travis-ci.org/mafintosh/flat-file-db) | ||
## Usage | ||
@@ -41,3 +43,3 @@ | ||
} | ||
db.get('test'); // returns {count:9} which also the persisted value of 'test' | ||
console.log(db.get('test')); // {count:9} which also the persisted value of 'test' | ||
``` | ||
@@ -47,2 +49,6 @@ | ||
* `db = flatfile(path, opts)` Create a new db instance. Per default fsync is called on all puts. To disable this set `opts.fsync = false` | ||
* `db = flatfile.sync(path, opts)` Same as above except you do not need to wait for the open event | ||
* `db.put(key, val, [cb])` Insert or update new key | ||
@@ -70,2 +76,2 @@ | ||
MIT | ||
MIT |
9663
262
74