-
Notifications
You must be signed in to change notification settings - Fork 10
/
timestreamdb.js
49 lines (42 loc) · 1.29 KB
/
timestreamdb.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
module.exports = TimestreamDB
module.exports.open = function (path, options, callback) {
// TODO force valueEncoding = json?
if (callback) {
levelup(path, options, function (err, db) {
if (err) return callback(err)
callback(err, TimestreamDB(db, options))
})
}
else
return TimestreamDB(levelup(path, options), options)
}
var levelup = require("levelup")
var Version = require("level-version")
var through2map = require("through2-map")
var ts = require("timestream")
var toTimestream = through2map.ctor({objectMode: true},
function _transform(record) {
var tsRecord = record.value
tsRecord._t = record.version
tsRecord._key = record.key
return tsRecord
}
)
/**
* Create a new TimestreamDB
* @param {LevelUp} instance A LevelUp instance
* @param {object} options Configuration options for level-version
*/
function TimestreamDB(instance, options) {
if (!(this instanceof TimestreamDB))
return new TimestreamDB(instance, options)
var db = Version(instance, options)
db.ts = function (key, options) {
var opts = {reverse: true}
if (options.start) opts.minVersion = options.start
if (options.until) opts.maxVersion = options.until
return ts(db.versionStream(key, opts).pipe(toTimestream()))
}
db.timeStream = db.ts
return db
}