-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (39 loc) · 1.1 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
var Store = require('level-store');
var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits;
var debug = require('debug')('level-stay');
module.exports = stay;
function stay (db, butts) {
var ee = new EventEmitter();
var store = Store(db);
if (typeof butts == 'string') {
var name = arguments[1];
var model = arguments[2];
butts = {};
butts[name] = model;
}
var names = Object.keys(butts);
var toSync = names.length;
names.forEach(function (name) {
var butt = butts[name];
store.exists(name, function (err, exists) {
debug('exists', exists);
if (err || !exists) {
onSync();
} else {
var ws = butt.createWriteStream();
ws.on('sync', onSync);
store.createReadStream(name).pipe(ws);
}
function onSync() {
butt.createReadStream()
.pipe(store.createWriteStream(name, { append : true }));
if (!--toSync) process.nextTick(function () {
debug('sync');
ee.emit('sync');
});
}
});
});
return ee;
}