-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtortoiseDB.js
57 lines (49 loc) · 1.5 KB
/
tortoiseDB.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
const MongoShell = require('./db/mongoShell');
const SyncTo = require('./syncTo');
const SyncFrom = require('./syncFrom');
const setUpServer = require('./server/server');
class TortoiseDB {
constructor({ name = 'default', port = process.env.PORT, mongoURI = process.env.MONGODB_URI, batchLimit = 1000 } = {}) {
this.port = port;
this.mongoShell = new MongoShell(name, mongoURI);
this.server = setUpServer(this);
this.syncInProgress = false;
this.batchLimit = batchLimit;
}
start() {
this.server.listen(this.port);
console.log(`TurtleDB server ready to go on port ${this.port}!`);
}
startSyncSession() {
const checkSyncProgress = (resolve) => {
if (!this.syncInProgress) {
clearInterval(this.intervalObj);
this.syncInProgress = true;
this.syncFrom();
resolve();
} else {
console.log('Sorry another sync still in progress.');
}
};
return new Promise((resolve, reject) => {
if (!this.syncInProgress) {
this.syncInProgress = true;
this.syncFrom();
resolve();
} else {
console.log('Sorry another sync still in progress.');
this.intervalObj = setInterval(checkSyncProgress.bind(this, resolve), 200);
}
});
}
syncFrom() {
this.syncFromSession = new SyncFrom(this.mongoShell);
}
syncTo() {
this.syncToSession = new SyncTo(this.mongoShell, this.batchLimit);
}
dropDB() {
return this.mongoShell.dropDB();
}
}
module.exports = TortoiseDB;