-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateFakeUsers.js
94 lines (70 loc) · 3.26 KB
/
generateFakeUsers.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// const mongoose = require('mongoose');
// const { faker } = require('@faker-js/faker'); // Import faker from @faker-js/faker
// const User = require('./models/User'); // Update with the correct path to your User model
// require('dotenv').config();
// // Connect to MongoDB
// const mongoURI = process.env.MONGO_URI; // Replace with your MongoDB connection string
// mongoose.connect(mongoURI, { useUnifiedTopology: true }) // Removed useNewUrlParser
// .then(() => {
// console.log('MongoDB connected successfully.');
// generateFakeUsers(100); // Generate 300 fake users
// })
// .catch((err) => console.error('MongoDB connection error:', err));
// // Function to generate fake users
// async function generateFakeUsers(numUsers) {
// try {
// const users = [];
// for (let i = 0; i < numUsers; i++) {
// const user = {
// username: faker.internet.userName(),
// quizAnswers: Array.from({ length: 20 }, () => faker.helpers.arrayElement([1, 2, 3, 4])), // Generate 15 answers, each 1, 2, 3, or 4
// gender: faker.helpers.arrayElement(['male', 'female']), // Randomly select gender
// };
// users.push(user);
// }
// // Insert fake users into the database
// await User.insertMany(users);
// console.log(`${numUsers} fake users generated successfully.`);
// } catch (error) {
// console.error('Error generating fake users:', error);
// } finally {
// mongoose.connection.close(); // Close the connection
// }
// }
const mongoose = require('mongoose');
const { faker } = require('@faker-js/faker'); // Import faker from @faker-js/faker
const User = require('./models/User'); // Update with the correct path to your User model
require('dotenv').config(); // Load environment variables from .env file
// Connect to MongoDB
const mongoURI = process.env.MONGO_URI; // Replace with your MongoDB connection string
mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true }) // You can keep both options for compatibility
.then(() => {
console.log('MongoDB connected successfully.');
generateFakeUsers(2000); // Generate 100 fake users
})
.catch((err) => console.error('MongoDB connection error:', err));
// Function to generate fake users
async function generateFakeUsers(numUsers) {
try {
const users = [];
for (let i = 0; i < numUsers; i++) {
const gender = faker.helpers.arrayElement(['male', 'female']); // Randomly select gender
const user = {
firebaseUID: faker.string.uuid(), // Generate a unique ID for firebase
username: faker.internet.userName(),
email: faker.internet.email(), // Generate a random email
dob: faker.date.past(20, new Date()).toISOString().split('T')[0], // Generate a random past date as string (YYYY-MM-DD)
gender,
quizAnswers: Array.from({ length: 20 }, () => faker.helpers.arrayElement([1, 2, 3, 4])), // Generate 20 answers, each 1, 2, 3, or 4
};
users.push(user);
}
// Insert fake users into the database
await User.insertMany(users);
console.log(`${numUsers} fake users generated successfully.`);
} catch (error) {
console.error('Error generating fake users:', error);
} finally {
mongoose.connection.close(); // Close the connection
}
}