Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require('mongoose');
- const friendSchema = mongoose.Schema({
- user : {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
- status: {type: String, required: true},
- },{ _id : false });
- const User = mongoose.model('User', mongoose.Schema({
- _id : mongoose.Schema.Types.ObjectId,
- name : { type : String, required: true},
- username : {
- type : String,
- required: true,
- unique: true
- },
- image_url : {type: String, required: false},
- password : {type: String, required: true},
- friends : [friendSchema],
- friendsRequests : [friendSchema],
- token : {type : String, required: false}
- }));
- const Post = mongoose.model('Post', mongoose.Schema({
- _id: mongoose.Schema.Types.ObjectId,
- message: {type : String, required: true},
- image_url: {type: String, default: ''},
- created_at: { type: Date},
- username: {type: String},
- user_id: mongoose.Schema.Types.ObjectId,
- user_image_url: {type: String}
- }));
- mongoose.connect('mongodb://localhost:27017/social_network_test2');
- const USERS_TO_CREATE = 10000;
- const POSTS_PER_USER = 10;
- generateRandomString = () =>{
- return Math.random().toString(36).substring(7);
- };
- generateRandomMessage = () => {
- let string = '';
- for(let i = 0; i < 10; i ++){
- string +=Math.random().toString(36).substring(7);
- string +=" ";
- }
- return string;
- }
- createFriends = (userIDs) =>{
- console.log('pilinha');
- for(let index = 0; index < USERS_TO_CREATE; index ++)
- {
- var friendCount = Math.floor(Math.random() * (20 - (0) + 1)) + (0);
- var addedFriends = [userIDs[index]];
- //console.log(friendCount);
- for(let j = 0; j < friendCount; j ++){
- var friendIndex = Math.floor(Math.random() * ((USERS_TO_CREATE - 1) - (0) + 1)) + (0);
- if(! addedFriends.some(element => element == userIDs[friendIndex])){
- addedFriends.push(userIDs[friendIndex]);
- User.findOneAndUpdate({_id: userIDs[index]}, {$addToSet: {'friends': {user: userIDs[friendIndex],
- status: 'accepted'}}}
- ).then(result => {
- //console.log("----Result----");
- //console.log(result);
- User.findOneAndUpdate({_id: userIDs[friendIndex]}, {$addToSet: {'friends': {user: userIDs[index],
- status:'accepted'}}});
- })
- }
- }
- }
- }
- var userIDs = [];
- var firstName = '';
- var lastName = '';
- var counter = 0;
- for(let i = 0; i < USERS_TO_CREATE; i ++){
- firstName = generateRandomString();
- lastName = generateRandomString();
- const id = new mongoose.Types.ObjectId();
- const user = new User({
- _id: id,
- name: firstName + " " + lastName,
- image_url: '',
- username: firstName,
- password: '$2b$10$yku6sQsQY5lB1wgG4eBEKu..shUi7zo4mgcwrB7pnq14yk9Sy3ili',
- token: '',
- });
- user.save()
- .then(result => {
- console.log(i + "-" + user.name);
- userIDs.push(user._id);
- for(let dayDiff = 2; dayDiff >= 0; dayDiff--){
- var created_at = new Date();
- created_at.setDate(new Date(Date.now()).getDate() - dayDiff);
- var hourDiff = Math.floor(Math.random() * (10 - (-10) + 1)) + (-10);
- created_at.setHours(created_at.getHours() + hourDiff);
- const post_id_1 = new mongoose.Types.ObjectId();
- const post1 = new Post({
- _id: post_id_1,
- message: generateRandomMessage(),
- image_url: 'https://s3.eu-west-2.amazonaws.com/cloudcomputing-iaas-images/profile_images/unknown.png',
- created_at: created_at,
- username: user.username,
- user_id: user._id,
- user_image_url: ''
- });
- post1.save()
- .then(result => {
- hourDiff = Math.floor(Math.random() * (10 - (-10) + 1)) + (-10);
- created_at.setHours(created_at.getHours() + hourDiff);
- const post_id_2 = new mongoose.Types.ObjectId();
- const post2 = new Post({
- _id: post_id_2,
- message: generateRandomMessage(),
- image_url: '',
- created_at: created_at,
- username: user.username,
- user_id: user._id,
- user_image_url: ''
- });
- post2.save()
- .then(result => {
- counter ++ ;
- if(counter == USERS_TO_CREATE){
- createFriends(userIDs);
- }
- });
- });
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement