forked from nest-desktop/nest-desktop
/
sync.js
118 lines (109 loc) · 4.22 KB
/
sync.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
115
116
117
118
"use strict"
const request = require('request');
const PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-upsert'));
const hash = require('object-hash');
const fs = require('fs');
const jsonfile = require('jsonfile');
var sync = {};
sync.remoteDB = () => {
var configApp = app.config.app();
if (configApp.db.remote.host === "") {
app.message.log('Host of remote DB is empty.')
return false
}
let {
host,
port
} = configApp.db.remote;
return request.get('http://' + host + ':' + port)
.on('error', (err) => {
// console.log(err)
app.message.show('Warning', 'The connection to the remote DB failed.')
return false
})
.on('response', (response) => {
// console.log(response)
if (!(response.headers.server.startsWith('CouchDB'))) {
app.message.show('Warning', 'No CouchDB on server found.')
return false
}
configApp.groups.map((group) => {
// console.log(group)
if (group.sync == false) return
var remoteDB = new PouchDB('http://' + host + ':' + port + '/' + group.id);
remoteDB.allDocs({
include_docs: true
}).then((remoteDocs) => {
if (remoteDocs.total_rows == 0) return
// console.log(remoteDocs.rows)
remoteDocs.rows.map((remoteRow) => {
var remoteDoc = remoteRow.doc;
if (remoteDoc._id.startsWith('_design')) return
if (remoteDoc.deleted) return
// console.log(remoteDoc)
app.db.localDB.findOne({
_id: remoteDoc._id
}).exec((err, doc) => {
if (doc) {
if (!doc.updatedAt) return
if (Date(remoteDoc.updatedAt) <= Date(doc.updatedAt)) return
}
app.db.localDB.update({
_id: remoteDoc._id,
}, remoteDoc, {
upsert: true
}, () => {
app.db.localDB.findOne({
_id: remoteDoc._id
})
})
})
})
})
})
var remoteDB = new PouchDB('http://' + host + ':' + port + '/' + configApp.user.group);
app.db.filter({
user: configApp.user.id
}).exec((err, localDocs) => {
if (localDocs.length == 0) return
// console.log(localDocs)
localDocs.map((localDoc) => {
if (localDoc.user != configApp.user.id) return
if (localDoc.deleted) return
// console.log(localDoc)
remoteDB.upsert(localDoc._id, (doc) => {
doc = localDoc
return doc
}).catch(function(err) {
// error
remoteDB.putIfNotExists(localDoc._id, localDoc)
});
})
});
app.message.log('Remote DB synchronized.')
return true
})
}
sync.init = () => {
var simulation_list = fs.readdirSync(path.join(app.dataPath, 'simulations'))
simulation_list.filter((filename) => {
return filename.endsWith('.json')
}).map((file) => {
var name = file.split('.')[0]
var sim = jsonfile.readFileSync(path.join(app.dataPath, 'simulations', name + '.json'))
app.db.localDB.findOne({
_id: sim._id
}).exec((err, docs) => {
sim.version = app.config.app().version;
if (docs == null) {
app.db.localDB.insert(sim)
} else if (app.hash(docs) != app.hash(sim)) {
app.db.localDB.update({
_id: docs._id
}, sim)
}
})
})
}
module.exports = sync;