-
Notifications
You must be signed in to change notification settings - Fork 194
/
migrate2to3.js
39 lines (33 loc) · 1.02 KB
/
migrate2to3.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
/* eslint no-underscore-dangle: 0 */
import fs from 'fs';
import path from 'path';
export default store => {
console.log('migrating');
const file = path.resolve(__dirname, '..', 'persist', 'sessions');
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
console.error('Cannot read file: ', err);
return;
}
const sessions = JSON.parse(data);
const ids = Object.keys(sessions);
const promises = ids.map(id => {
const session = sessions[id];
session.id = id;
delete session._id;
session.posts.forEach(p => {
p.likes = [];
p.dislikes = [];
for (let i = 0; i < p.votes; i++) {
p.likes.push('?');
}
});
return store.set(session);
});
Promise.all(promises).then(() => {
console.log('Migration done');
}).catch(err2 => {
console.error(err2);
});
});
};