diff options
| author | ilyayudovin <ilyayudovin123@gmail.com> | 2020-06-11 21:19:18 +0300 | 
|---|---|---|
| committer | ilyayudovin <ilyayudovin123@gmail.com> | 2020-06-11 21:19:18 +0300 | 
| commit | 2b876318d2aeca5183e0a57277904c7332a7aa0e (patch) | |
| tree | d7798ccefdcd53351dd9f7f9ca4b9d2ff5b11ead | |
| parent | 72a89f5eff0a7b93d5cc5cf81255b9a1efcf20f1 (diff) | |
| download | which-api-2b876318d2aeca5183e0a57277904c7332a7aa0e.tar.gz | |
feat: add populate function
| -rw-r--r-- | populateDb.ts | 42 | 
1 files changed, 38 insertions, 4 deletions
diff --git a/populateDb.ts b/populateDb.ts index 6f5beaf..732cb51 100644 --- a/populateDb.ts +++ b/populateDb.ts @@ -1,6 +1,8 @@  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 }); @@ -17,8 +19,40 @@ const users = [    { name: "Ethan" },  ]; -Promise.map(users, async user => { -  return await app.service('users').create(user); -}).catch(e => console.error(e)) -  .finally(() => mongoose.disconnect()); +const polls = [{ +  contents: { +    left:{ +      url: 'https://cdn.psychologytoday.com/sites/default/files/field_blog_entry_images/2019-06/pexels-photo-556667.jpeg', +      votes: 0 +    }, +    right:{ +      url: 'https://images.pexels.com/photos/556666/pexels-photo-556666.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500', +      votes: 0 +    } +  } +},{ +  contents: { +    left:{ +      url: 'https://cdn.psychologytoday.com/sites/default/files/field_blog_entry_images/2019-06/pexels-photo-556667.jpeg', +      votes: 0 +    }, +    right:{ +      url: 'https://images.pexels.com/photos/556666/pexels-photo-556666.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500', +      votes: 0 +    } +  } +}]; +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();
\ No newline at end of file  |