aboutsummaryrefslogtreecommitdiff
path: root/populateDb.ts
blob: 88cb6a6cd1f42857fc78e300f525ad682a9f0369 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import mongoose from 'mongoose';
import Promise from 'bluebird';
import _ from 'lodash';
import app from './app';
import { UserSchema } from './models/users/user.schema';

mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true });

const users = [
  { name: "Emma" },
  { name: "Elise" },
  { name: "Jack" },
  { name: "Oliver" },
  { name: "Jamie" },
  { name: "Aidan" },
  { name: "Jordan" },
  { name: "Erin" },
  { name: "William" },
  { name: "Ethan" },
];

const polls = [{
  contents: {
    left:{
      url: 'https://cdn.psychologytoday.com/sites/default/files/field_blog_entry_images/2019-06/pexels-photo-556667.jpeg',
      votes: Math.floor(Math.random() * 101)
},
    right:{
      url: 'https://images.pexels.com/photos/556666/pexels-photo-556666.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
      votes: Math.floor(Math.random() * 101)
}
  }
},{
  contents: {
    left:{
      url: 'https://cdn.psychologytoday.com/sites/default/files/field_blog_entry_images/2019-06/pexels-photo-556667.jpeg',
      votes: Math.floor(Math.random() * 101)
},
    right:{
      url: 'https://images.pexels.com/photos/556666/pexels-photo-556666.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
      votes: Math.floor(Math.random() * 101)
}
  }
}];

const populate = async () => {
  const createdUsers = await Promise.map(users, async user => {
    return await app.service('users').create(user);
  });
  console.log(createdUsers);
  await Promise.map(polls, async poll => {
    const user = _.sample(createdUsers);
    return await app.service('polls').create({...poll, authorId: user._id});
  });
  mongoose.disconnect();
};

populate();