Skip to content

Commit

Permalink
feat: allow folders in dbname
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Before, all datbases have been stored in `.fsdown`, configureable by `require(fsdown).directory`. Besides that, all database names have been sanitized, so `myfolder/dbname` would become `myfolderdbname`

`require(fsdown).directory` has been removed, and dbname can now contain folders. It is split using `.split(require(path).sep)` and all parts are sanitized separately.
  • Loading branch information
gr2m committed Sep 2, 2016
1 parent 1ba12dc commit 7fdfd98
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
8 changes: 3 additions & 5 deletions lib/localstorage-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var path = require('path');
var sanitizeFilename = require('sanitize-filename');
var collections = new require('pouchdb-collections');
var debounce = require('lodash.debounce');
var mainExportedObject = require('../');
var mkdirp = require('mkdirp');

var dbs = new collections.Map();
Expand All @@ -30,8 +29,7 @@ function callbackify(callback, fun) {

function LocalStorageCore(dbname) {
var self = this;
var directory = mainExportedObject.directory || '.fsdown';
var filename = path.join(directory, sanitizeFilename(dbname + '.json'));
var filename = path.join.apply(null, dbname.split(path.sep).map(sanitizeFilename)) + '.json';
if (dbs.has(dbname)) {
self._db = dbs.get(dbname);
} else {
Expand All @@ -41,7 +39,7 @@ function LocalStorageCore(dbname) {
try {
self._db.store = JSON.parse(fs.readFileSync(filename, 'utf-8'));
} catch (e) { /* ignore if the file isn't there */ }
mkdirp.sync(directory);
mkdirp.sync(path.dirname(filename));
}

// Periodically write to disk. Yes this is prone to failure, but this module isn't
Expand Down Expand Up @@ -100,4 +98,4 @@ LocalStorageCore.destroy = function (dbname, callback) {
});
};

module.exports = LocalStorageCore;
module.exports = LocalStorageCore;
32 changes: 16 additions & 16 deletions tests/custom-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var levelup = require('levelup');

module.exports.setUp = function (leveldown, test, testCommon) {
test('setUp common', testCommon.setUp);
test('setUp db', function (t) {
Expand All @@ -15,8 +15,8 @@ module.exports.all = function (leveldown, tape, testCommon) {
module.exports.setUp(leveldown, tape, testCommon);

tape('test .destroy', function (t) {
var db = levelup('destroy-test', {db: leveldown});
var db2 = levelup('other-db', {db: leveldown});
var db = levelup('.fsdown/destroy-test', {db: leveldown});
var db2 = levelup('.fsdown/other-db', {db: leveldown});
db2.put('key2', 'value2', function (err) {
t.notOk(err, 'no error' );
db.put('key', 'value', function (err) {
Expand All @@ -26,9 +26,9 @@ module.exports.all = function (leveldown, tape, testCommon) {
t.equal(value, 'value', 'should have value');
db.close(function (err) {
t.notOk(err, 'no error');
leveldown.destroy('destroy-test', function (err) {
leveldown.destroy('.fsdown/destroy-test', function (err) {
t.notOk(err, 'no error');
var db3 = levelup('destroy-test', {db: leveldown});
var db3 = levelup('.fsdown/destroy-test', {db: leveldown});
db3.get('key', function (err, value) {
t.ok(err, 'key is not there');
db2.get('key2', function (err, value) {
Expand All @@ -45,9 +45,9 @@ module.exports.all = function (leveldown, tape, testCommon) {
});

tape('test .destroy with multiple dbs', function (t) {
var db = levelup('a', {db: leveldown});
var db2 = levelup('b', {db: leveldown});
var db3 = levelup('c', {db: leveldown});
var db = levelup('.fsdown/a', {db: leveldown});
var db2 = levelup('.fsdown/b', {db: leveldown});
var db3 = levelup('.fsdown/c', {db: leveldown});
db.put('1', '1', function (err) {
t.notOk(err, 'no error');
db2.put('1', '1', function (err) {
Expand All @@ -58,12 +58,12 @@ module.exports.all = function (leveldown, tape, testCommon) {
t.notOk(err, 'no error');
db2.put('3', '3', function (err) {
t.notOk(err, 'no error');
leveldown.destroy('b', function (err) {
leveldown.destroy('.fsdown/b', function (err) {
t.notOk(err, 'no error');
db3.get('1', function (err, res) {
t.notOk(err, 'no error');
t.equal(res, '1');
db2 = levelup('b', {db: leveldown});
db2 = levelup('.fsdown/b', {db: leveldown});
db2.get('3', function (err) {
t.ok(err);
t.end();
Expand All @@ -78,8 +78,8 @@ module.exports.all = function (leveldown, tape, testCommon) {
});

tape('test escaped db name', function (t) {
var db = levelup('bang!', {db: leveldown});
var db2 = levelup('bang!!', {db: leveldown});
var db = levelup('.fsdown/bang!', {db: leveldown});
var db2 = levelup('.fsdown/bang!!', {db: leveldown});
db.put('!db1', '!db1', function (err) {
t.notOk(err, 'no error');
db2.put('db2', 'db2', function (err) {
Expand All @@ -88,7 +88,7 @@ module.exports.all = function (leveldown, tape, testCommon) {
t.notOk(err, 'no error');
db2.close(function (err) {
t.notOk(err, 'no error');
db = levelup('bang!', {db: leveldown});
db = levelup('.fsdown/bang!', {db: leveldown});
db.get('!db2', function (err, key, value) {
t.ok(err, 'got error');
t.equal(key, undefined, 'key should be null');
Expand Down Expand Up @@ -193,8 +193,8 @@ module.exports.all = function (leveldown, tape, testCommon) {
});

tape('iterate past end of db', function (t) {
var db = leveldown('aaaaaa');
var db2 = leveldown('bbbbbb');
var db = leveldown('.fsdown/aaaaaa');
var db2 = leveldown('.fsdown/bbbbbb');
var noerr = function (err) {
t.error(err, 'opens crrectly');
};
Expand Down Expand Up @@ -223,7 +223,7 @@ module.exports.all = function (leveldown, tape, testCommon) {
});

tape('next() callback is dezalgofied', function (t) {
var db = leveldown('aaaaaa');
var db = leveldown('.fsdown/aaaaaa');
var noerr = function (err) {
t.error(err, 'opens crrectly');
};
Expand Down
4 changes: 2 additions & 2 deletions tests/testCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

var dbidx = 0;
var theLocation = function () {
return '_leveldown_test_db_' + dbidx++;
return '.fsdown/_leveldown_test_db_' + dbidx++;
};

var lastLocation = function () {
return '_leveldown_test_db_' + dbidx;
return '.fsdown/_leveldown_test_db_' + dbidx;
};

var cleanup = function (callback) {
Expand Down

0 comments on commit 7fdfd98

Please sign in to comment.