Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first arg to Store now is a dict. added 'json' option.

  • Loading branch information...
commit ffadf5c03009f1809ba4ffdca249927c0b942f3c 1 parent e87ce39
@pkrumins authored
Showing with 16 additions and 8 deletions.
  1. +16 −8 lib/store.js
View
24 lib/store.js
@@ -3,13 +3,18 @@ var EventEmitter = require('events').EventEmitter;
module.exports = Store;
Store.prototype = new EventEmitter;
-function Store(filename, cb) {
+function Store(opts, cb) {
if (!(this instanceof Store)) return new Store(filename, cb);
if (cb === undefined) cb = function () {};
var self = this;
+ if (opts.filename === undefined)
+ throw new Error('Filename was not specified');
+
+ opts.json = opts.json || false;
+
var db = new SQLite();
- db.open(filename, function (error) {
+ db.open(opts.filename, function (error) {
if (error) {
self.emit('error', error);
cb(error);
@@ -57,7 +62,7 @@ function Store(filename, cb) {
if (cb === undefined) cb = function () {}
db.query(
"INSERT OR REPLACE INTO store (key, value) VALUES (?, ?)",
- [key, value],
+ [key, opts.json ? JSON.stringify(value) : value],
function (error) {
if (error) {
self.emit('error', error);
@@ -86,7 +91,7 @@ function Store(filename, cb) {
cb();
}
else {
- cb(undefined, row.value, row.key);
+ cb(undefined, opts.json ? JSON.parse(row.value) : row.value, row.key);
hadRow = true;
}
}
@@ -125,8 +130,11 @@ function Store(filename, cb) {
else if (row === undefined) {
done();
}
- else if (pred(row.key, row.value)) {
- cb(undefined, row.key, row.value);
+ else {
+ var value = opts.json ? JSON.parse(row.value) : row.value;
+ if (pred(row.key, value)) {
+ cb(undefined, row.key, value);
+ }
}
}
);
@@ -162,7 +170,7 @@ function Store(filename, cb) {
done();
}
else {
- cb(undefined, row.key, row.value);
+ cb(undefined, row.key, opts.json ? JSON.parse(row.value) : row.value);
}
}
);
@@ -178,7 +186,7 @@ function Store(filename, cb) {
cb(error);
}
keys.push(key);
- vals.push(val);
+ vals.push(opts.json ? JSON.parse(val) : val);
},
function () {
cb(undefined, keys, vals);
Please sign in to comment.
Something went wrong with that request. Please try again.