aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-06-12 04:52:24 +0300
committereug-vs <eug-vs@keemail.me>2020-06-12 04:52:24 +0300
commit56c9c3355c6e27e2b014315583e0c90cc073937f (patch)
treee1f7a7a362d6474d762940cd7c3796c0647ecaf1
parentf054a45cbefae5a6e7736af85e173fc7214dcc2c (diff)
downloadwhich-api-56c9c3355c6e27e2b014315583e0c90cc073937f.tar.gz
refactor: improve populate script
-rw-r--r--models/polls/poll.schema.ts2
-rw-r--r--package-lock.json2
-rw-r--r--populateDb.ts94
3 files changed, 52 insertions, 46 deletions
diff --git a/models/polls/poll.schema.ts b/models/polls/poll.schema.ts
index ea91051..83de688 100644
--- a/models/polls/poll.schema.ts
+++ b/models/polls/poll.schema.ts
@@ -1,7 +1,7 @@
import { Document, Schema, Types } from 'mongoose';
import { User } from '../users/user.schema'
-interface ImageData {
+export interface ImageData {
url: string;
votes: number;
}
diff --git a/package-lock.json b/package-lock.json
index 35cc880..034f4ee 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,5 +1,5 @@
{
- "name": "@oneflow/which-api",
+ "name": "which-api",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
diff --git a/populateDb.ts b/populateDb.ts
index 88cb6a6..8000111 100644
--- a/populateDb.ts
+++ b/populateDb.ts
@@ -1,58 +1,64 @@
import mongoose from 'mongoose';
-import Promise from 'bluebird';
+import bluebird from 'bluebird';
import _ from 'lodash';
import app from './app';
import { UserSchema } from './models/users/user.schema';
+import { PollSchema, ImageData } from './models/polls/poll.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_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 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 names: string[] = [
+ 'Emma',
+ 'Elise',
+ 'Jack',
+ 'Oliver',
+ 'Jamie',
+ 'Adam',
+ 'Jordan',
+ 'William'
+];
-const populate = async () => {
- const createdUsers = await Promise.map(users, async user => {
- return await app.service('users').create(user);
+const generateImageData = (): ImageData => ({
+ url: _.sample(imageUrls) || '',
+ votes: Math.floor(Math.random() * 101)
+});
+
+const createPoll = (authorId: string): Promise<PollSchema> => {
+ return app.service('polls').create({
+ contents: {
+ left: generateImageData(),
+ right: generateImageData(),
+ },
+ authorId
});
- console.log(createdUsers);
- await Promise.map(polls, async poll => {
- const user = _.sample(createdUsers);
- return await app.service('polls').create({...poll, authorId: user._id});
+};
+
+const createUser = (name: string): Promise<UserSchema> => {
+ return app.service('users').create({
+ avatarUrl: _.sample(imageUrls) || '',
+ name
});
- mongoose.disconnect();
};
-populate(); \ No newline at end of file
+
+const populate = async () => {
+ const users = await bluebird.map(names, name => createUser(name));
+
+ for (let i = 0; i < POLLS_AMOUNT; i++) {
+ const sampleUser = _.sample(users);
+ await createPoll(sampleUser?._id);
+ };
+};
+
+populate().finally(mongoose.disconnect);
+