diff --git a/README.md b/README.md index a3b0dcb..d1eadc7 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,11 @@ # README -JSON FileSystem Database is a JSON database such as MongoDB backed by FileSystem.
+JSON FileSystem Database is a JSON Document database, such as MongoDB.
+All methods are asynchronous and accessing / filtering is executed in parallel using [async](https://github.com/caolan/async).

Currently supports 2 types of data drivers: **Memory** and **Disk**.
Memory driver is very performant. It is possible to flush data to disk (configurable).
-Disk driver is less performant an is implemented with **Pessimistic Transaction Locking** approach.
-All methods are asynchronous and accessing / filtering is executed in parallel using [async](https://github.com/caolan/async).
+Disk driver is less performant. Holds all data in disk, and reads/writes everytime you interact with the collection. Is implemented with **Pessimistic Transaction Locking** approach.

Based on [Jalalhejazi](https://github.com/Jalalhejazi), [jsonfs](https://github.com/Jalalhejazi/jsonfs). # Dependencies @@ -113,7 +113,7 @@ Support for [Query and Projection Operators](https://docs.mongodb.org/manual/ref ... ``` -Support provided by [json-criteria](https://github.com/mirek/node-json-criteria). +Support provided using [json-criteria](https://github.com/mirek/node-json-criteria). # Options @@ -146,10 +146,10 @@ database.Collection.remove(criteria, {multi: false}, callback); When initializing the Driver you can pass 4 options: ```bash -options.path - The path to store the collection files. Accepts a String. Defaults to '/tmp/'. +options.path - The path to store the db collection files. Defaults to '/tmp/'. options.driver - One of ['memory', 'disk'], defaults to disk -options.memory.flush - if you want to flush it to disk, defaults to false, only used if driver is 'memory' -options.memory.flushInterval - time interval to flush memory to disk defaults to 10s, only used if driver is 'memory' +options.memory.flush - if you want to flush it to disk, only used if driver is 'memory'. Defaults to false +options.memory.flushInterval - Time interval to flush memory to disk, only used if driver is 'memory'. Defaults to 10000ms (10s) ``` ## Collections options diff --git a/index.js b/index.js index 1448061..3129bff 100644 --- a/index.js +++ b/index.js @@ -87,8 +87,8 @@ module.exports.connect = function (collections, options, callback) { } // in parallel initialize each collection async.each(collections, function attachOrCreate(collection, next) { + console.log('Collection \'%s\' is about to be attached', collection); var filePath = path.join(self._db._path, collection + '.json'); - console.log('\'%s\' Collection is about to be attached', collection); util.fileSystem.exists(filePath, function afterCheck(exists) { if (!exists) { util.fileSystem.write(filePath, function afterWriteFile(err) { diff --git a/lib/memory.js b/lib/memory.js index fb3d0df..c57806a 100644 --- a/lib/memory.js +++ b/lib/memory.js @@ -25,6 +25,13 @@ 'use strict'; var util = require('./util'); +var async = require('async'); + +function defaultCallback(err) { + if (err) { + console.log(err); + } +} /** * Provides in memory storage and regular flush to disk. @@ -49,7 +56,7 @@ function Memory(options) { } self.set(content); if (self.flush) { - console.log('Driver will flush data every %sms', self.flushInterval); + console.log('\'Memory\' driver will flush data every %sms', self.flushInterval); // set interval to flush setInterval(function flushToDisk() { util.fileSystem.lock(self.flushFile, function afterLock(err) { @@ -81,14 +88,14 @@ function Memory(options) { * @param callback */ Memory.prototype.set = function set(content, callback) { - callback = callback || function afterUnlockFile(err) { - if (err) { - console.log('Oopss we couldn\'t release the lock!'); - throw err; + callback = callback || defaultCallback; + var self = this; + async.series([ + function(next) { + self.memoryTable = util.clone(content); + return next(undefined, self.memoryTable); } - }; - this.memoryTable = util.clone(content); - return callback(undefined, this.memoryTable); + ], callback); }; /** @@ -97,6 +104,7 @@ Memory.prototype.set = function set(content, callback) { * @param callback */ Memory.prototype.get = function get(callback) { + callback = callback || defaultCallback; return callback(undefined, this.memoryTable); }; @@ -106,6 +114,7 @@ Memory.prototype.get = function get(callback) { * @param callback */ Memory.prototype.lock = function lock(callback) { + callback = callback || defaultCallback; return callback(undefined); }; @@ -115,12 +124,7 @@ Memory.prototype.lock = function lock(callback) { * @param callback */ Memory.prototype.unlock = function unlock(callback) { - callback = callback || function afterUnlockFile(err) { - if (err) { - console.log('Oopss we couldn\'t release the lock!'); - throw err; - } - }; + callback = callback || defaultCallback; return callback(undefined); }; diff --git a/package.json b/package.json index c0329b8..a4db9b3 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "jsondbfs", "description": "JSON Filesystem Database.", "author": "Manuel Martins ", - "version": "0.4.1", + "version": "0.4.2", "license": "Apache-2.0", "engine": "node >= 0.12.0", "main": "./index", diff --git a/test/disk-test.js b/test/disk-test.js index e0900ad..2cd24fa 100644 --- a/test/disk-test.js +++ b/test/disk-test.js @@ -98,7 +98,7 @@ describe('JSONDBFS Disk Driver', function testSpec() { }); it('should fail creating a connection to an invalid path', function test(done) { - JSONDBFSDriver.connect(['Collection'], { + JSONDBFSDriver.connect(['DiskCollection'], { path: '/invalid' }, function afterConnect(err, db) { assert.notEqual(err, undefined); @@ -140,7 +140,7 @@ describe('JSONDBFS Disk Driver', function testSpec() { }); it('should create a new collection passing a non array as collection', function test(done) { - JSONDBFSDriver.connect('CollectionAsString', function afterConnect(err, db) { + JSONDBFSDriver.connect('DiskCollectionAsString', function afterConnect(err, db) { assert.equal(err, undefined); assert.notEqual(db, undefined); return done(); @@ -156,7 +156,7 @@ describe('JSONDBFS Disk Driver', function testSpec() { }); it('should create a new collection using override options', function test(done) { - JSONDBFSDriver.connect(['Override'], { + JSONDBFSDriver.connect(['DiskOverride'], { path: '/tmp/', driver: 'disk' }, function afterConnect(err, db) { diff --git a/test/memory-test.js b/test/memory-test.js index 468dc68..695f2ca 100644 --- a/test/memory-test.js +++ b/test/memory-test.js @@ -100,7 +100,7 @@ describe('JSONDBFS Memory Driver', function testSpec() { }); it('should fail creating a connection to an invalid path', function test(done) { - JSONDBFSDriver.connect(['Collection'], { + JSONDBFSDriver.connect(['MemoryCollection'], { path: '/invalid', driver: 'memory' }, function afterConnect(err, db) { @@ -151,7 +151,7 @@ describe('JSONDBFS Memory Driver', function testSpec() { }); it('should create a new collection passing a non array as collection', function test(done) { - JSONDBFSDriver.connect('CollectionAsString', { + JSONDBFSDriver.connect('MemoryCollectionAsString', { driver: 'memory' }, function afterConnect(err, db) { assert.equal(err, undefined); @@ -171,7 +171,7 @@ describe('JSONDBFS Memory Driver', function testSpec() { }); it('should create a new collection using override options', function test(done) { - JSONDBFSDriver.connect(['Override'], { + JSONDBFSDriver.connect(['MemoryOverride'], { path: '/tmp/', driver: 'memory' }, function afterConnect(err, db) {