generated from michaelmrose/mern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.js
83 lines (71 loc) · 2.08 KB
/
seed.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
require('dotenv').config();
require('./config/database');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const User = require('./models/user'); // Assuming your model file is named user.js
const SALT_ROUNDS = 6;
function randomBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
async function seedDatabase() {
try {
// Drop the existing User collection
await mongoose.connection.dropCollection('users');
// Initialize empty arrays to hold 30 days worth of data for each user
const journalEntries = [];
const lifeEvents = [];
const notes = [];
// Generate 30 days worth of data
for (let i = 0; i < 30; i++) {
const date = new Date();
date.setDate(date.getDate() - i);
journalEntries.push({
creationDate: date,
morningPainLevel: randomBetween(1, 10),
morningStiffnessLevel: randomBetween(1, 10),
morningFatigueLevel: randomBetween(1, 10),
dayPainLevel: randomBetween(1, 10),
dayStiffnessLevel: randomBetween(1, 10),
dayFatigueLevel: randomBetween(1, 10),
qualityOfSleep: "OK",
hoursOfSleep: 8,
});
lifeEvents.push({
date: date,
event: `Event for day ${i + 1}`,
});
notes.push({
creationDate: date,
contents: `Note for day ${i + 1}`,
});
}
// Create sample users
const users = [
{
name: "John Doe",
email: "[email protected]",
password: "a",
journalEntries,
lifeEvents,
notes,
},
{
name: "Jane Doe",
email: "[email protected]",
password: "b",
journalEntries,
lifeEvents,
notes,
},
];
// Seed the users including their embedded journal entries, life events, and notes
await User.create(users);
console.log('Database seeding complete with 30 days of data for each user!');
} catch (error) {
console.log('An error occurred:', error);
} finally {
// Disconnect the database
mongoose.connection.close();
}
}
seedDatabase();