-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate.seed.data.js
118 lines (105 loc) · 3 KB
/
generate.seed.data.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* SEEDS THE DATABASE AND WRITES TO FILE THE SEEDED DATA
*
* Using the fancy new ES6 syntax
*
* This file is run with `npm run seed`
*/
const faker = require('Faker');
const fs = require('fs');
const path = require('path');
require('./app');
const mongoose = require('mongoose');
const User = mongoose.model('User');
/**
* HELPER FUNCTIONS
*/
function writeTofile(data) {
const fileName = 'seeded_users.json';
fs.writeFile(
path.join(__dirname, fileName),
JSON.stringify(data, null, 2),
(err) => {
if (err) {
console.log(`[ERROR] writeUsersToFile: ${err}`);
return false;
}
console.log(`[INFO] Wrote users to file /test/${fileName}`);
return true;
}
);
}
/**
* Returns a random integer between min and max. Includes min and max.
* @param {number} min Minimum random number possible.
* @param {number} max Maximum random number posisble.
* @return {number} Random number between min and max.
*/
function randInt(min, max) {
const range = (max - min) + 1;
return Math.floor(Math.random() * range) + min;
}
/**
* Returns a random element from the specified array.
* @param {Array} arr The array to return an element from.
* @return {*} A random element from arr.
*/
function choose(arr) {
return arr[randInt(0, arr.length - 1)];
}
/**
* SEED THE USERS
*/
const USER_ROLE = 'youth';
const NUM_USERS = 2;
const GENDER = User.schema.path('gender').enumValues;
const seededUsers = [];
function seedUsers() {
for (let i = 0; i < NUM_USERS; i += 1) {
const year = Math.floor(2003 - randInt(0, 10)); // 15 to 25 y.o. as of 2018
const month = randInt(1, 12);
const day = randInt(1, 30);
const user = new User();
const seedUser = {
name: faker.Name.firstName().concat(' ').concat(faker.Name.lastName()),
email: faker.Internet.email(),
phoneNumber: faker.PhoneNumber.phoneNumberFormat(0),
dob: new Date().setFullYear(year, month, day),
gender: choose(GENDER),
hasDisability: Math.random() < 0.5,
hasChild: Math.random() < 0.5,
role: USER_ROLE,
password: faker.Internet.userName()
};
user.name = seedUser.name;
user.email = seedUser.email;
user.phoneNumber = seedUser.phoneNumber;
user.dob = seedUser.dob;
user.gender = seedUser.gender;
user.hasDisability = seedUser.hasDisability;
user.hasChild = seedUser.hasChild;
user.role = seedUser.role;
user.setPassword(seedUser.password);
user.save((err) => {
if (err) {
console.log(`[ERROR] Seeding user: ${err}`);
} else {
console.log(`[SUCCESS] Seeded user: ${seedUser.firstName} ${seedUser.lastName}`);
}
});
seededUsers.push(seedUser);
}
return true;
}
seedUsers();
writeTofile(seededUsers);
/**
* DONE WITH THE DB CONNECTION TIME TO CLEAN UP
*
* Waits 6 seconds then closes connections because if not
* the connection will close before the new users have been
* saved to the database
*/
setTimeout(() => {
mongoose.connection.close();
}, 6000);