Advertisement
Guest User

Untitled

a guest
Jun 4th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2.  
  3. const friendSchema = mongoose.Schema({
  4. user : {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
  5. status: {type: String, required: true},
  6. },{ _id : false });
  7.  
  8.  
  9. const User = mongoose.model('User', mongoose.Schema({
  10. _id : mongoose.Schema.Types.ObjectId,
  11. name : { type : String, required: true},
  12. username : {
  13. type : String,
  14. required: true,
  15. unique: true
  16. },
  17. image_url : {type: String, required: false},
  18. password : {type: String, required: true},
  19. friends : [friendSchema],
  20. friendsRequests : [friendSchema],
  21. token : {type : String, required: false}
  22. }));
  23.  
  24. const Post = mongoose.model('Post', mongoose.Schema({
  25. _id: mongoose.Schema.Types.ObjectId,
  26. message: {type : String, required: true},
  27. image_url: {type: String, default: ''},
  28. created_at: { type: Date},
  29. username: {type: String},
  30. user_id: mongoose.Schema.Types.ObjectId,
  31. user_image_url: {type: String}
  32. }));
  33.  
  34.  
  35. mongoose.connect('mongodb://localhost:27017/social_network_test2');
  36.  
  37.  
  38. const USERS_TO_CREATE = 10000;
  39. const POSTS_PER_USER = 10;
  40.  
  41. generateRandomString = () =>{
  42.  
  43. return Math.random().toString(36).substring(7);
  44.  
  45. };
  46.  
  47. generateRandomMessage = () => {
  48.  
  49. let string = '';
  50. for(let i = 0; i < 10; i ++){
  51. string +=Math.random().toString(36).substring(7);
  52. string +=" ";
  53. }
  54. return string;
  55. }
  56.  
  57.  
  58. createFriends = (userIDs) =>{
  59.  
  60. console.log('pilinha');
  61. for(let index = 0; index < USERS_TO_CREATE; index ++)
  62. {
  63. var friendCount = Math.floor(Math.random() * (20 - (0) + 1)) + (0);
  64.  
  65. var addedFriends = [userIDs[index]];
  66.  
  67. //console.log(friendCount);
  68. for(let j = 0; j < friendCount; j ++){
  69.  
  70. var friendIndex = Math.floor(Math.random() * ((USERS_TO_CREATE - 1) - (0) + 1)) + (0);
  71.  
  72. if(! addedFriends.some(element => element == userIDs[friendIndex])){
  73.  
  74. addedFriends.push(userIDs[friendIndex]);
  75. User.findOneAndUpdate({_id: userIDs[index]}, {$addToSet: {'friends': {user: userIDs[friendIndex],
  76. status: 'accepted'}}}
  77. ).then(result => {
  78. //console.log("----Result----");
  79. //console.log(result);
  80.  
  81. User.findOneAndUpdate({_id: userIDs[friendIndex]}, {$addToSet: {'friends': {user: userIDs[index],
  82. status:'accepted'}}});
  83. })
  84. }
  85. }
  86.  
  87. }
  88. }
  89.  
  90.  
  91. var userIDs = [];
  92.  
  93. var firstName = '';
  94. var lastName = '';
  95.  
  96.  
  97. var counter = 0;
  98.  
  99. for(let i = 0; i < USERS_TO_CREATE; i ++){
  100.  
  101. firstName = generateRandomString();
  102. lastName = generateRandomString();
  103.  
  104. const id = new mongoose.Types.ObjectId();
  105. const user = new User({
  106. _id: id,
  107. name: firstName + " " + lastName,
  108. image_url: '',
  109. username: firstName,
  110. password: '$2b$10$yku6sQsQY5lB1wgG4eBEKu..shUi7zo4mgcwrB7pnq14yk9Sy3ili',
  111. token: '',
  112. });
  113. user.save()
  114. .then(result => {
  115.  
  116. console.log(i + "-" + user.name);
  117.  
  118. userIDs.push(user._id);
  119.  
  120. for(let dayDiff = 2; dayDiff >= 0; dayDiff--){
  121.  
  122. var created_at = new Date();
  123. created_at.setDate(new Date(Date.now()).getDate() - dayDiff);
  124. var hourDiff = Math.floor(Math.random() * (10 - (-10) + 1)) + (-10);
  125. created_at.setHours(created_at.getHours() + hourDiff);
  126.  
  127. const post_id_1 = new mongoose.Types.ObjectId();
  128. const post1 = new Post({
  129. _id: post_id_1,
  130. message: generateRandomMessage(),
  131. image_url: 'https://s3.eu-west-2.amazonaws.com/cloudcomputing-iaas-images/profile_images/unknown.png',
  132. created_at: created_at,
  133. username: user.username,
  134. user_id: user._id,
  135. user_image_url: ''
  136. });
  137. post1.save()
  138. .then(result => {
  139.  
  140. hourDiff = Math.floor(Math.random() * (10 - (-10) + 1)) + (-10);
  141. created_at.setHours(created_at.getHours() + hourDiff);
  142. const post_id_2 = new mongoose.Types.ObjectId();
  143. const post2 = new Post({
  144. _id: post_id_2,
  145. message: generateRandomMessage(),
  146. image_url: '',
  147. created_at: created_at,
  148. username: user.username,
  149. user_id: user._id,
  150. user_image_url: ''
  151. });
  152.  
  153. post2.save()
  154. .then(result => {
  155. counter ++ ;
  156.  
  157. if(counter == USERS_TO_CREATE){
  158. createFriends(userIDs);
  159. }
  160. });
  161.  
  162. });
  163. }
  164. });
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement