-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseed.js
143 lines (126 loc) · 4.39 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
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
135
136
137
138
139
140
141
142
143
/*
This seed file is only a placeholder. It should be expanded and altered
to fit the development of your application.
It uses the same file the server uses to establish
the database connection:
--- server/db/index.js
The name of the database used is set in your environment files:
--- server/env/*
This seed file has a safety check to see if you already have users
in the database. If you are developing multiple applications with the
fsg scaffolding, keep in mind that fsg always uses the same database
name in the environment files.
*/
var mongoose = require('mongoose');
var Promise = require('bluebird');
var chalk = require('chalk');
var connectToDb = require('./server/db');
var User = Promise.promisifyAll(mongoose.model('User'));
var Event = Promise.promisifyAll(mongoose.model('Event'));
var Chance = require('chance');
var chance = new Chance();
var moment = require('moment');
moment().format();
var getRandomLocation = function() { //change this later to take a sport and return a location that makes sense for the sport input.
var locations = [
{latitude: 40.771606, longitude: -73.974819},
{latitude: 40.746617, longitude: -74.010184},
{latitude: 40.727127, longitude: -74.011334},
{latitude: 40.781389, longitude: -73.966553},
{latitude: 40.716920, longitude: -74.016867},
{latitude: 40.731041, longitude: -74.001244},
]
return locations[Math.floor(Math.random()*locations.length)];
}
var getRandomLevel = function() {
var levels = ['Beginner (1-2)', 'Novice (3-4)', 'Intermediate(5-6)', 'Advanced(7-8)', 'Pro(9-10)'];
return levels[Math.floor(Math.random()*levels.length)];
}
var getRandomSport = function() {
var sports = ["Basketball", "Climbing", "Soccer", "Baseball", "Football", "Lifting", "Skiing", "Mountain Biking", "Surfing", "Cycling", 'Tennis']
return sports[Math.floor(Math.random()*sports.length)];
}
var getRandomTags = function(n) {
var taglist = [];
var tags = [{text: 'Fun'}, {text:'Awesome'}, {text:'Cool'}, {text:'Advanced'}, {text:'Easy'}, {text:'Hardcore'}, {text:'Music'}, {text:'Weekend'}, {text:'Day Trip'}, {text:'Night Time'}, {text:'Day Time'}, {text:'After Work'}, {text:'Kids'}, {text:'Adults'}, {text:'Intermediate'}];
while(taglist.length < n) {
if (taglist.length === tags.length) {
break;
}
var randtag = tags[Math.floor(Math.random()*tags.length)]
taglist.push(randtag)
}
return taglist;
}
var genRandomEvents = function(num) {
return User.find({}).then(function(allUsers){
var events = [];
for(i=0;i<num;i++) {
console.log(i);
console.log(num);
var newEvent = {
name: chance.sentence({words: 2}),
host: chance.pick(allUsers)._id,
sport: getRandomSport(),
date: new Date(chance.date({string: true, year: 2015, month: 11, day: chance.integer({min: 11, max: 31})})),
location: getRandomLocation(),
level: getRandomLevel(),
tags: getRandomTags(),
description: chance.paragraph({sentences: 1})
}
events.push(newEvent);
}
return events;
})
}
var seedUsers = function () {
var users = [
{
userName: 'Jugal',
email: '[email protected]',
password: 'jugal'
},
{
userName: 'June',
email: '[email protected]',
password: 'june'
},
{
userName: 'Mingjie',
email: '[email protected]',
password: 'mingjie'
},
{
userName: 'Bryce',
email: '[email protected]',
password: 'bryce'
}
];
return User.createAsync(users);
};
var seedEvents = function () {
return genRandomEvents(15).then(function(events){
return Event.createAsync(events);
});
}
connectToDb.then(function () {
User.findAsync({}).then(function (users) {
if (users.length === 0) {
return seedUsers();
} else {
console.log(chalk.magenta('Seems to already be user data, exiting!'));
process.kill(0);
}
})
.then(function(){
console.log(chalk.yellow('Seeding Events...'));
return seedEvents();
})
.then(function () {
console.log(chalk.green('Seed successful!'));
process.kill(0);
}).catch(function (err) {
console.error(err);
process.kill(1);
});
});