/
import-data.js
59 lines (52 loc) · 1.82 KB
/
import-data.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
const config = require('../config'),
fs = require('fs'),
mongodb = require('../services/mongodb');
async function doImport() {
const db = await mongodb.connect('rsscloud', config.mongodbUri);
if (fs.existsSync('./data/data.json')) {
const data = JSON.parse(fs.readFileSync('./data/data.json', 'utf8'));
await db.collection('resources').bulkWrite(
Object.keys(data.resources).map(id => {
return {
replaceOne: {
filter: { _id: id },
replacement: data.resources[id],
upsert: true
}
};
})
);
await db.collection('subscriptions').bulkWrite(
Object.keys(data.subscriptions).map(id => {
const subscriptions = {
_id: id,
pleaseNotify: Object.keys(data.subscriptions[id]).map(sid => {
const subscription = data.subscriptions[id][sid];
subscription.url = sid;
subscription.notifyProcedure = false;
subscription.protocol = 'http-post';
return subscription;
})
}
return {
replaceOne: {
filter: { _id: id },
replacement: subscriptions,
upsert: true
}
};
})
);
await mongodb.close('rsscloud');
} else {
await mongodb.close('rsscloud');
throw new Error('Cannot find ./data/data.json');
}
}
doImport()
.then(() => {
console.log('Imported ./data/data.json');
})
.catch(err => {
console.error(err);
});