diff options
author | eug-vs <eug-vs@keemail.me> | 2020-08-12 21:11:37 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-08-12 21:11:37 +0300 |
commit | 15dfeb17076f1a25f564fbf5f694c3bfafb25c45 (patch) | |
tree | d09e0f71c845edc4113655a3c2e7326d3d928d8d | |
parent | c438a491088494cda5553fd2d9a3024dec4f2277 (diff) | |
download | which-api-15dfeb17076f1a25f564fbf5f694c3bfafb25c45.tar.gz |
chore: add migrateImages script
-rw-r--r-- | scripts/migrateImages.ts | 46 | ||||
-rw-r--r-- | scripts/populateDb.ts (renamed from populateDb.ts) | 23 |
2 files changed, 58 insertions, 11 deletions
diff --git a/scripts/migrateImages.ts b/scripts/migrateImages.ts new file mode 100644 index 0000000..771880e --- /dev/null +++ b/scripts/migrateImages.ts @@ -0,0 +1,46 @@ +import mongoose from 'mongoose'; +import bluebird from 'bluebird'; +import _ from 'lodash'; +import { + User, + Poll, + Vote, + Feedback +} from 'which-types'; + +import app from '../app'; +app.service('files').setup(app); + +const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which'; + +mongoose.connect(MONGODB_URL, { + useNewUrlParser: true, + useUnifiedTopology: true, + useCreateIndex: true, + useFindAndModify: false, + family: 4 // Use IPv4, skip trying IPv6 +}); + +const patchPoll = (poll: Poll): Promise<Poll> => { + console.log(`Patching poll of user ${poll.author.username}`) + return app.service('polls').patch(poll._id.toString(), {}, { user: poll.author, authenticated: true }); +}; + +const patchUser = (user: User): Promise<User> => { + console.log(`Patching user ${user.username}`) + return app.service('users').patch(user._id.toString(), {}, { user, authenticated: true }); +}; + +const update = async () => { + const users = app.service('users').find(); + + await bluebird.mapSeries(users, async (user: User) => { + await patchUser(user); + const polls = await app.service('polls').find({ query: { authorId: user._id }}); + await bluebird.mapSeries(polls, (poll: Poll) => patchPoll(poll)); + return; + }); +}; + +update(); + diff --git a/populateDb.ts b/scripts/populateDb.ts index 1565f44..b7d83c0 100644 --- a/populateDb.ts +++ b/scripts/populateDb.ts @@ -8,7 +8,8 @@ import { Feedback } from 'which-types'; -import app from './app'; +import app from '../app'; +app.service('files').setup(app); const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which'; @@ -20,7 +21,7 @@ mongoose.connect(MONGODB_URL, { family: 4 // Use IPv4, skip trying IPv6 }); -const POLLS_AMOUNT = 20; +const POLLS_AMOUNT = 5; const imageUrls: string[] = [ // eslint-disable max-len @@ -31,14 +32,14 @@ const imageUrls: string[] = [ ]; const names: string[] = [ - 'Emma', - 'Elise', - 'Jack', - 'Oliver', - 'Jamie', - 'Adam', - 'Jordan', - 'William' + 'emma', + 'elise', + 'jack', + 'oliver', + 'jamie', + 'adam', + 'jordan', + 'william' ]; const choices = [ @@ -101,5 +102,5 @@ const populate = async () => { }); }; -populate().finally(mongoose.disconnect); +populate(); |