forked from leei/node-dbstore
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
114 lines (94 loc) · 2.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var addon = require("bindings")("addon.node");
// helpers
function decode(buf, opts={}) {
if (typeof opts == 'string') {
opts = { encoding: opts };
}
if (opts.encoding || opts.json) {
buf = buf.toString(opts.encoding || 'utf8');
}
if (opts.json) {
buf = JSON.parse(buf || null);
}
return buf;
};
function encode(val, opts={}) {
if (typeof opts == 'string') {
opts = { encoding: opts };
}
if (opts.json) {
val = JSON.stringify(val);
}
var buf = val;
if (typeof buf == 'string') {
buf = new Buffer(val, opts.encoding || 'utf8');
}
return buf;
}
function appendOptions(args, opts={}) {
var newArgs = args.concat(opts.txn ? opts.txn : null);
return opts.flags ? newArgs.concat(opts.flags) : newArgs;
}
// load refs to objects to customize
var Db = addon.Db;
var DbCursor = addon.DbCursor;
// db
Db.prototype.put = function (key, val, opts={}) {
key = key.toString('utf8');
val = encode(val, opts);
return this._put.apply(this, appendOptions([key, val], opts));
};
Db.prototype.del = function (key, opts={}) {
key = key.toString('utf8');
return this._del.apply(this, appendOptions([key], opts));
};
Db.prototype.get = function (key, opts={}) {
key = key.toString('utf8');
var val = this._get.apply(this, appendOptions([key], opts));
return decode(val, opts);
};
// dbcursor
DbCursor.prototype.put = function (val, opts={}) {
val = encode(val, opts);
return this._put(val);
};
DbCursor.prototype.del = function () {
return this._del();
};
DbCursor.prototype.set = function (key, opts={}) {
key = key.toString('utf8');
var res = this._set(key);
var buf = decode(res.value);
return {key, value: buf};
};
DbCursor.prototype.current = function (opts={}) {
var res = this._current();
var value = decode(res.value, opts);
var key = res.key ? res.key.toString('utf8') : res.key;
return {key, value};
}
DbCursor.prototype.next = function (opts={}) {
var res = this._next();
var value = decode(res.value, opts);
var key = res.key ? res.key.toString('utf8') : res.key;
return {key, value};
}
DbCursor.prototype.prev = function (opts={}) {
var res = this._prev();
var value = decode(res.value, opts);
var key = res.key ? res.key.toString('utf8') : res.key;
return {key, value};
}
DbCursor.prototype.first = function (opts={}) {
var res = this._first();
var value = decode(res.value, opts);
var key = res.key ? res.key.toString('utf8') : res.key;
return {key, value};
}
DbCursor.prototype.last = function (opts={}) {
var res = this._last();
var value = decode(res.value, opts);
var key = res.key ? res.key.toString('utf8') : res.key;
return {key, value};
}
module.exports = addon;