-
Notifications
You must be signed in to change notification settings - Fork 0
/
faker.js
68 lines (63 loc) · 1.95 KB
/
faker.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
const faker = require("faker");
const { User, Blog, Comment } = require("./src/models");
generateFakeData = async (userCount, blogsPerUser, commentsPerUser) => {
if (typeof userCount !== "number" || userCount < 1)
throw new Error("userCount must be a positive integer");
if (typeof blogsPerUser !== "number" || blogsPerUser < 1)
throw new Error("blogsPerUser must be a positive integer");
if (typeof commentsPerUser !== "number" || commentsPerUser < 1)
throw new Error("commentsPerUser must be a positive integer");
const users = [];
const blogs = [];
const comments = [];
console.log("Preparing fake data.");
for (let i = 0; i < userCount; i++) {
users.push(
new User({
username:
faker.internet.userName() + parseInt(Math.random() * 1000000000),
name: {
first: faker.name.firstName(),
last: faker.name.lastName(),
},
age: 10 + parseInt(Math.random() * 50),
email: faker.internet.email(),
})
);
}
/*
users.map((user) => {
for (let i = 0; i < blogsPerUser; i++) {
blogs.push(
new Blog({
title: faker.lorem.words(),
content: faker.lorem.paragraphs(),
islive: true,
user,
})
);
}
});
users.map((user) => {
for (let i = 0; i < commentsPerUser; i++) {
let index = Math.floor(Math.random() * blogs.length);
comments.push(
new Comment({
content: faker.lorem.sentence(),
user,
blog: blogs[index]._id,
})
);
}
});
*/
console.log("fake data inserting to database...");
await User.insertMany(users);
console.log(`${users.length} fake users generated!`);
await Blog.insertMany(blogs);
console.log(`${blogs.length} fake blogs generated!`);
await Comment.insertMany(comments);
console.log(`${comments.length} fake comments generated!`);
console.log("COMPLETE!!");
};
module.exports = { generateFakeData };