Skip to content

Commit 15dfeb1

Browse files
committed
chore: add migrateImages script
1 parent c438a49 commit 15dfeb1

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

scripts/migrateImages.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import mongoose from 'mongoose';
2+
import bluebird from 'bluebird';
3+
import _ from 'lodash';
4+
import {
5+
User,
6+
Poll,
7+
Vote,
8+
Feedback
9+
} from 'which-types';
10+
11+
import app from '../app';
12+
app.service('files').setup(app);
13+
14+
const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which';
15+
16+
mongoose.connect(MONGODB_URL, {
17+
useNewUrlParser: true,
18+
useUnifiedTopology: true,
19+
useCreateIndex: true,
20+
useFindAndModify: false,
21+
family: 4 // Use IPv4, skip trying IPv6
22+
});
23+
24+
const patchPoll = (poll: Poll): Promise<Poll> => {
25+
console.log(`Patching poll of user ${poll.author.username}`)
26+
return app.service('polls').patch(poll._id.toString(), {}, { user: poll.author, authenticated: true });
27+
};
28+
29+
const patchUser = (user: User): Promise<User> => {
30+
console.log(`Patching user ${user.username}`)
31+
return app.service('users').patch(user._id.toString(), {}, { user, authenticated: true });
32+
};
33+
34+
const update = async () => {
35+
const users = app.service('users').find();
36+
37+
await bluebird.mapSeries(users, async (user: User) => {
38+
await patchUser(user);
39+
const polls = await app.service('polls').find({ query: { authorId: user._id }});
40+
await bluebird.mapSeries(polls, (poll: Poll) => patchPoll(poll));
41+
return;
42+
});
43+
};
44+
45+
update();
46+

populateDb.ts renamed to scripts/populateDb.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
Feedback
99
} from 'which-types';
1010

11-
import app from './app';
11+
import app from '../app';
12+
app.service('files').setup(app);
1213

1314
const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which';
1415

@@ -20,7 +21,7 @@ mongoose.connect(MONGODB_URL, {
2021
family: 4 // Use IPv4, skip trying IPv6
2122
});
2223

23-
const POLLS_AMOUNT = 20;
24+
const POLLS_AMOUNT = 5;
2425

2526
const imageUrls: string[] = [
2627
// eslint-disable max-len
@@ -31,14 +32,14 @@ const imageUrls: string[] = [
3132
];
3233

3334
const names: string[] = [
34-
'Emma',
35-
'Elise',
36-
'Jack',
37-
'Oliver',
38-
'Jamie',
39-
'Adam',
40-
'Jordan',
41-
'William'
35+
'emma',
36+
'elise',
37+
'jack',
38+
'oliver',
39+
'jamie',
40+
'adam',
41+
'jordan',
42+
'william'
4243
];
4344

4445
const choices = [
@@ -101,5 +102,5 @@ const populate = async () => {
101102
});
102103
};
103104

104-
populate().finally(mongoose.disconnect);
105+
populate();
105106

0 commit comments

Comments
 (0)