From 15dfeb17076f1a25f564fbf5f694c3bfafb25c45 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 12 Aug 2020 21:11:37 +0300 Subject: chore: add migrateImages script --- populateDb.ts | 105 ---------------------------------------------- scripts/migrateImages.ts | 46 ++++++++++++++++++++ scripts/populateDb.ts | 106 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 105 deletions(-) delete mode 100644 populateDb.ts create mode 100644 scripts/migrateImages.ts create mode 100644 scripts/populateDb.ts diff --git a/populateDb.ts b/populateDb.ts deleted file mode 100644 index 1565f44..0000000 --- a/populateDb.ts +++ /dev/null @@ -1,105 +0,0 @@ -import mongoose from 'mongoose'; -import bluebird from 'bluebird'; -import _ from 'lodash'; -import { - User, - Poll, - Vote, - Feedback -} from 'which-types'; - -import app from './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 POLLS_AMOUNT = 20; - -const imageUrls: string[] = [ - // eslint-disable max-len - 'https://cdn.psychologytoday.com/sites/default/files/field_blog_entry_images/2019-06/pexels-photo-556667.jpeg', - 'https://images.pexels.com/photos/556666/pexels-photo-556666.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500', - 'https://i.pinimg.com/originals/50/91/3e/50913eeb04768a5b1fa9985c16704d96.jpg', - 'https://grazia.wwmindia.com/photogallery/2017/apr/1_1491461089.jpg' -]; - -const names: string[] = [ - 'Emma', - 'Elise', - 'Jack', - 'Oliver', - 'Jamie', - 'Adam', - 'Jordan', - 'William' -]; - -const choices = [ - 'left', - 'right' -]; - - -const createPoll = (authorId: string): Promise => { - const generateImageData = () => ({ - url: _.sample(imageUrls) || '' - }); - - return app.service('polls').create({ - contents: { - left: generateImageData(), - right: generateImageData() - } - }, { user: { _id: authorId }, authenticated: true }); -}; - -const createUser = (username: string): Promise => { - return app.service('users').create({ - avatarUrl: _.sample(imageUrls) || '', - password: 'supersecret', - username - }); -}; - -const createVote = (authorId: string, pollId: string): Promise => { - return app.service('votes').create({ - pollId, - which: _.sample(choices) - }, { user: { _id: authorId }, authenticated: true }); -}; - -const createFeedback = (userId: string): Promise => { - return app.service('feedback').create({ - version: 'v1.0.0', - score: _.sample([1, 2, 3, 4, 5]), - contents: 'Absolutely amazing!' - }, { user: { _id: userId }, authenticated: true }); -}; - -const populate = async () => { - const users = await bluebird.map(names, name => createUser(name)); - - const polls = await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => { - const user = _.sample(users); - return createPoll(user?._id || ''); - }); - - await bluebird.map(users, user => { - return createFeedback(user?._id || ''); - }); - - await bluebird.map(users, user => { - const pollsToVote = _.sampleSize(polls, _.random(0, POLLS_AMOUNT)); - return bluebird.map(pollsToVote, poll => createVote(user?._id || '', poll?._id || '')); - }); -}; - -populate().finally(mongoose.disconnect); - 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 => { + 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 => { + 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/scripts/populateDb.ts b/scripts/populateDb.ts new file mode 100644 index 0000000..b7d83c0 --- /dev/null +++ b/scripts/populateDb.ts @@ -0,0 +1,106 @@ +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 POLLS_AMOUNT = 5; + +const imageUrls: string[] = [ + // eslint-disable max-len + 'https://cdn.psychologytoday.com/sites/default/files/field_blog_entry_images/2019-06/pexels-photo-556667.jpeg', + 'https://images.pexels.com/photos/556666/pexels-photo-556666.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500', + 'https://i.pinimg.com/originals/50/91/3e/50913eeb04768a5b1fa9985c16704d96.jpg', + 'https://grazia.wwmindia.com/photogallery/2017/apr/1_1491461089.jpg' +]; + +const names: string[] = [ + 'emma', + 'elise', + 'jack', + 'oliver', + 'jamie', + 'adam', + 'jordan', + 'william' +]; + +const choices = [ + 'left', + 'right' +]; + + +const createPoll = (authorId: string): Promise => { + const generateImageData = () => ({ + url: _.sample(imageUrls) || '' + }); + + return app.service('polls').create({ + contents: { + left: generateImageData(), + right: generateImageData() + } + }, { user: { _id: authorId }, authenticated: true }); +}; + +const createUser = (username: string): Promise => { + return app.service('users').create({ + avatarUrl: _.sample(imageUrls) || '', + password: 'supersecret', + username + }); +}; + +const createVote = (authorId: string, pollId: string): Promise => { + return app.service('votes').create({ + pollId, + which: _.sample(choices) + }, { user: { _id: authorId }, authenticated: true }); +}; + +const createFeedback = (userId: string): Promise => { + return app.service('feedback').create({ + version: 'v1.0.0', + score: _.sample([1, 2, 3, 4, 5]), + contents: 'Absolutely amazing!' + }, { user: { _id: userId }, authenticated: true }); +}; + +const populate = async () => { + const users = await bluebird.map(names, name => createUser(name)); + + const polls = await bluebird.mapSeries(new Array(POLLS_AMOUNT), async () => { + const user = _.sample(users); + return createPoll(user?._id || ''); + }); + + await bluebird.map(users, user => { + return createFeedback(user?._id || ''); + }); + + await bluebird.map(users, user => { + const pollsToVote = _.sampleSize(polls, _.random(0, POLLS_AMOUNT)); + return bluebird.map(pollsToVote, poll => createVote(user?._id || '', poll?._id || '')); + }); +}; + +populate(); + -- cgit v1.2.3