|
| 1 | +import mongoose from 'mongoose'; |
| 2 | +import bluebird from 'bluebird'; |
| 3 | +import _ from 'lodash'; |
| 4 | +import app from './app'; |
| 5 | +import { UserSchema } from './models/users/user.schema'; |
| 6 | +import { PollSchema, ImageData } from './models/polls/poll.schema'; |
| 7 | + |
| 8 | +mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true }); |
| 9 | + |
| 10 | +const POLLS_AMOUNT = 20; |
| 11 | + |
| 12 | +const imageUrls: string[] = [ |
| 13 | + // eslint-disable max-len |
| 14 | + 'https://cdn.psychologytoday.com/sites/default/files/field_blog_entry_images/2019-06/pexels-photo-556667.jpeg', |
| 15 | + 'https://images.pexels.com/photos/556666/pexels-photo-556666.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500', |
| 16 | + 'https://i.pinimg.com/originals/50/91/3e/50913eeb04768a5b1fa9985c16704d96.jpg', |
| 17 | + 'https://grazia.wwmindia.com/photogallery/2017/apr/1_1491461089.jpg' |
| 18 | +]; |
| 19 | + |
| 20 | +const names: string[] = [ |
| 21 | + 'Emma', |
| 22 | + 'Elise', |
| 23 | + 'Jack', |
| 24 | + 'Oliver', |
| 25 | + 'Jamie', |
| 26 | + 'Adam', |
| 27 | + 'Jordan', |
| 28 | + 'William' |
| 29 | +]; |
| 30 | + |
| 31 | +const generateImageData = (): ImageData => ({ |
| 32 | + url: _.sample(imageUrls) || '', |
| 33 | + votes: Math.floor(Math.random() * 101) |
| 34 | +}); |
| 35 | + |
| 36 | +const createPoll = (authorId: string): Promise<PollSchema> => { |
| 37 | + return app.service('polls').create({ |
| 38 | + contents: { |
| 39 | + left: generateImageData(), |
| 40 | + right: generateImageData(), |
| 41 | + }, |
| 42 | + authorId |
| 43 | + }); |
| 44 | +}; |
| 45 | + |
| 46 | +const createUser = (name: string): Promise<UserSchema> => { |
| 47 | + return app.service('users').create({ |
| 48 | + avatarUrl: _.sample(imageUrls) || '', |
| 49 | + name |
| 50 | + }); |
| 51 | +}; |
| 52 | + |
| 53 | + |
| 54 | +const populate = async () => { |
| 55 | + const users = await bluebird.map(names, name => createUser(name)); |
| 56 | + |
| 57 | + for (let i = 0; i < POLLS_AMOUNT; i++) { |
| 58 | + const sampleUser = _.sample(users); |
| 59 | + await createPoll(sampleUser?._id); |
| 60 | + }; |
| 61 | +}; |
| 62 | + |
| 63 | +populate().finally(mongoose.disconnect); |
| 64 | + |
0 commit comments