Skip to content

Commit

Permalink
Add missing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
E.Azer Koçulu committed Sep 28, 2013
1 parent 6216184 commit 1af689c
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 42 deletions.
8 changes: 5 additions & 3 deletions README.md
@@ -1,6 +1,6 @@
## level-json [![Build Status](https://travis-ci.org/azer/level-json.png)](https://travis-ci.org/azer/level-json)

LevelDB JSON Wrapper
LevelDB wrapper to avoid repeating encoding fields for just JSON

## Install

Expand Down Expand Up @@ -36,10 +36,14 @@ io = require('level-json')('./data')

A wrapper function with following methods will be returned:

* `del`
* `get`
* `set`
* `destroy`
* `close`
* `batch`
* `isClosed`
* `isOpen`

The wrapper function can be called for getting and setting values like below:

Expand All @@ -55,5 +59,3 @@ io('foo', { foo: 123 }, function (error) {

})
```


103 changes: 71 additions & 32 deletions index.js
Expand Up @@ -6,68 +6,107 @@ var info = require("local-debug")("info");
var trace = require("local-debug")("trace");

module.exports = newIO;
module.exports.destroy = destroy;
module.exports.repair = repair;

function destroy (path, callback) {
info('Destroying %s', path);

down.destroy(path, callback);
}

function get (io, key, callback) {
function get (io, key, callback) { // OR (io, key, options, callback)
trace('Getting %s', key);

io.get(key, function (error, raw) {
if(error) return callback(error);
var options;

var parsed;
try {
parsed = JSON.parse(raw);
} catch (exc) {
return callback(exc);
}
if (arguments.length > 3) {
options = arguments[2];
callback = arguments[3];
}

io.get(key, options, callback);
}

function method (io, name, callback) {
return function () {
trace('Calling method: %s', name);
return io[name].apply(io, arguments);
};
}

callback(undefined, parsed);
});
function repair (location, callback) {
info('Repairing %s', location);
down.repair(location, callback);
}

function set (io, key, obj, callback) {
var stringified;
function set (io, key, value, callback) {
var options;

try {
stringified = JSON.stringify(obj);
} catch (exc) {
return callback(exc);
if (arguments.length > 4) {
options = arguments[3];
callback = arguments[4];
}

trace('Setting %s as %s', key, stringified.substring(0, 25) + '...');
trace('Setting %s. Encoding: %s', key, options ? options.valueEncoding : 'String');

io.put(key, stringified, callback);
io.put(key, value, options, callback);
}

function newIO (dir) {
function newIO (callback) {
var dir = DEFAULT_PATH;
var options;

dir || ( dir = DEFAULT_PATH);

var io = up(dir);
if (typeof callback == 'string') {
dir = callback;
callback = undefined;
}

info('Connected to %s', dir);
if (arguments.length > 1) {
dir = arguments[0];
callback = arguments[1];
}

wrapper.close = function (cb) {
info('Closing %s', dir);
io.close(cb);
};
if (arguments.length > 2) {
options = arguments[1];
callback = arguments[2];
}

wrapper.destroy = function (cb) {
destroy(dir, cb);
if (!options || options.encoding) {
options || (options = {});
options.encoding = {
encode: JSON.stringify,
decode: JSON.parse
};
}

var io = up(dir, options, callback);

info('Connected to %s', dir);

wrapper.get = function (key, cb) {
get(io, key, cb);
}
};

wrapper.set = function (key, value, cb) {
set(io, key, value, cb);
}
};

wrapper.batch = method(io, 'batch');
wrapper.close = method(io, 'close');
wrapper.del = method(io, 'del');
wrapper.isClosed = method(io, 'isClosed');
wrapper.isOpen = method(io, 'isOpen');

wrapper.destroy = function (callback) {
if (wrapper.isClosed()) return destroy(dir, callback);

wrapper.close(function (error) {
if (error) return callback(error);

destroy(dir, callback);
});
};

wrapper.dir = dir;

Expand Down
6 changes: 3 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "level-json",
"version": "0.0.1",
"description": "LevelDB JSON Wrapper",
"description": "LevelDB wrapper to avoid repeating encoding fields for just JSON",
"main": "index.js",
"scripts": {
"test": "fox"
Expand All @@ -21,8 +21,8 @@
"author": "Azer Koculu <azer@kodfabrik.com>",
"license": "BSD",
"dependencies": {
"levelup": "~0.15.0",
"leveldown": "~0.8.0",
"local-debug": "0.0.0"
"local-debug": "0.0.0",
"levelup": "~0.16.0"
}
}
58 changes: 54 additions & 4 deletions test.js
Expand Up @@ -4,23 +4,27 @@ var callAll = require("call-all");
var io1;
var io2;

afterEach(function(done){
before(function(done){
io1 = newIO();
io2 = newIO('./db2');
done();
});

beforeEach(function(done){
var fns = [];

if (io1) {
fns.push(io1.close);
fns.push(io1.destroy);
}

if (io2) {
fns.push(io2.close);
fns.push(io2.destroy);
}

callAll(fns)(done);
});

it('saves and reads JSON', function(done){
it('saves and reads JSON values', function(done){
io1 = newIO();

io1('foo', { foo: true, bar: 123, qux: { span: 'Eggs' } }, function (error, record) {
Expand All @@ -35,6 +39,52 @@ it('saves and reads JSON', function(done){
});
});

it('saves and reads with JSON keys', function(done){
io1 = newIO();

io1({ key: 'foo' }, { foo: true, bar: 123, qux: { span: 'Eggs' } }, function (error, record) {
if(error) return done(error);

io1({ key: 'foo' }, function (error, record) {
if (error) return done(error);
expect(record.foo).to.be.true;
expect(record.qux.span).to.equal('Eggs');
done();
});
});
});

it('saves and deletes a record', function(done){
io1 = newIO();

io1('foo', { foo: true, bar: 123, qux: { span: 'Eggs' } }, function (error, record) {
if(error) return done(error);

io1.del('foo', function (error) {
if (error) return done(error);

io1('foo', function (error, record) {
expect(error).to.exist;
expect(record).to.not.exist;
done();
});
});
});

});

it('checks if db is closed or open', function(done){
io1 = newIO(function () {
expect(io1.isOpen()).to.be.true;

io1.close(function () {
expect(io1.isClosed()).to.be.true;
done();
});
});
});


it('saves to a custom path', function(done){
io1 = newIO();

Expand Down

0 comments on commit 1af689c

Please sign in to comment.