-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeder.ts
134 lines (113 loc) · 4.43 KB
/
seeder.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import bcrypt from "bcryptjs"
import dotenv from "dotenv"
import mongoose, { Types } from "mongoose"
import { RolesEnum } from "./enums"
import { Channel, Message, User } from "./models"
import connectDB from "./utils/db"
dotenv.config()
// Function to generate a random date within the past year
const randomDate = () => {
const start = new Date()
const end = new Date(start.getTime() - 365 * 24 * 60 * 60 * 1000)
return new Date(
start.getTime() + Math.random() * (end.getTime() - start.getTime()),
)
}
const chooseRandomIndex = (arr: any[]) => Math.floor(Math.random() * arr.length)
// Connect to the database and drop the existing data
async function main() {
try {
await connectDB(process.env.MONGO_URL as string)
// await mongoose.connection.db.dropDatabase()
await User.deleteMany({})
await Channel.deleteMany({})
await Message.deleteMany({})
let salt = await bcrypt.genSalt(5)
const hashedPassword = await bcrypt.hash("[email protected]", salt)
// Generate 10 dummy users
const dummyUsers = Array.from({ length: 10 }, (_, i) => ({
_id: new Types.ObjectId(),
name: `User ${i + 1}`,
email: `user${i + 1}@example.com`,
phoneNo: `123-456-78${i.toString().padStart(2, "0")}`,
password: hashedPassword,
profileImage: "https://source.unsplash.com/random",
status: "active",
otp: undefined,
createdAt: randomDate(),
updatedAt: randomDate(),
}))
dummyUsers.push({
_id: new Types.ObjectId("60f1b9e3b3f1f3b3b3f1f3b3"),
name: "Hello Hi",
email: "[email protected]",
phoneNo: "1234567890",
password: hashedPassword,
profileImage: "https://source.unsplash.com/random",
status: "active",
otp: undefined,
createdAt: randomDate(),
updatedAt: randomDate(),
})
// Generate 4 dummy channels with random members
const dummyChannels = Array.from({ length: 4 }, () => ({
_id: new Types.ObjectId(),
members: generateMembers(),
isGroup: true,
createdAt: randomDate(),
updatedAt: randomDate(),
}))
// Generate 50 dummy chat messages
const dummyChatMessages = Array.from({ length: 50 }, (_, i) => {
//select receiverId randomly from user or group
const randomChannel = dummyChannels[chooseRandomIndex(dummyChannels)]
if (!randomChannel)
return
const randomUser = randomChannel.members[chooseRandomIndex(randomChannel.members)]
if (!randomUser)
return
return {
_id: new Types.ObjectId(),
channelId: randomChannel._id,
bucket: Math.floor(Math.random() * 10),
senderId: randomUser.userId.toString(),
message: `Message ${i + 1}`,
createdAt: randomDate(),
updatedAt: randomDate(),
}
})
//Generate dummy random number of members with one admin and other members
function generateMembers() {
// a value between 2 to length of dummyUsers
const randomNumberOfMembers = Math.floor(
Math.random() * (dummyUsers.length - 1) + 2,
)
let selectedIndex = new Set<number>()
while (selectedIndex.size < randomNumberOfMembers) {
selectedIndex.add(chooseRandomIndex(dummyUsers))
}
const members = Array.from(selectedIndex).map((index, i) => ({
userId: dummyUsers[index]._id,
role: i === 0 ? RolesEnum.ADMIN : RolesEnum.MEMBER,
}))
// console.log(members)
return members
}
// Insert the dummy data
const users = await User.insertMany(dummyUsers)
await Message.insertMany(dummyChatMessages)
await Channel.insertMany(dummyChannels)
for (const user of users) {
if (user.email === "[email protected]") {
console.log(user.generateSocketToken());
}
}
console.log("Dummy data has been successfully created!")
mongoose.connection.close()
} catch (err) {
console.error(err)
mongoose.connection.close()
process.exit(1)
}
}
main()