forked from joanaz/HireDot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.js
191 lines (175 loc) · 6.15 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
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 usersSeeds = require('./seeds/students.js');
var Company = Promise.promisifyAll(mongoose.model('Company'));
var companiesSeeds = require('./seeds/companies.js');
var Award = Promise.promisifyAll(mongoose.model('Award'));
var awardsSeeds = require('./seeds/awards.js');
var Project = Promise.promisifyAll(mongoose.model('Project'));
var projectsSeeds = require('./seeds/projects.js');
var Hackathon = Promise.promisifyAll(mongoose.model('Hackathon'));
var hackathonsSeeds = require('./seeds/hackathons.js');
var Technology = Promise.promisifyAll(mongoose.model('Technology'));
var TechnologiesSeeds = require('./seeds/technologies.js');
var seedCompanies = function() {
return Company.createAsync(companiesSeeds);
};
var seedAwards = function() {
return Award.createAsync(awardsSeeds);
};
var seedHackathons = function() {
return Hackathon.createAsync(hackathonsSeeds);
};
var seedTechnologies = function() {
return Technology.createAsync(TechnologiesSeeds)
}
var seedProjects = function() {
return Award.findAsync({})
.then(function(awards) {
projectsSeeds.forEach(function(project) {
if (project.awards) {
project.awards = project.awards.map(function(projectAward) {
awards.forEach(function(award) {
if (award.name === projectAward) {
projectAward = award._id
}
})
return projectAward
})
}
})
return Technology.findAsync({})
})
.then(function(technologies) {
projectsSeeds.forEach(function(project) {
project.technologies = project.technologies.map(function(projectTech) {
technologies.forEach(function(technology) {
if (technology.name === projectTech) {
projectTech = technology._id
}
})
return projectTech
})
})
return Project.createAsync(projectsSeeds)
})
}
var seedUsers = function() {
return Project.findAsync({})
.then(function(projects) {
usersSeeds.forEach(function(user) {
if (user.projects) {
user.projects = user.projects.map(function(userproject) {
projects.forEach(function(project) {
if (project.title === userproject) {
userproject = project._id
}
})
return userproject
})
// console.log(user.projects)
}
})
// console.log(usersSeeds)
return User.createAsync(usersSeeds)
})
};
connectToDb.then(function() {
// Project.removeAsync()
// .then(function() {
// return seedProjects()
// })
// 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);
// }
// })
// seedUsers();
// seedAwards()
// .then(function() {
// return seedTechnologies();
// })
// .then(function() {
// return seedProjects();
// })
// .then(function() {
// return seedHackathons();
// })
// ProjectAward.findAsync({}).then(function(awards) {
// if (awards.length === 0) {
// return seedAwards();
// Technology.removeAsync()
// .then(function() {
// return seedTechnologies()
// })
// Award.findAsync({}).then(function(awards) {
// if (awards.length === 0) {
// return seedAwards();
// } else {
// return
// }
// })
// .then(function() {
// Company.findAsync({}).then(function(companies) {
// if (companies.length === 0) {
// return seedCompanies()
// } else return
// })
// })
// .then(function() {
// return
User.removeAsync()
// })
.then(function() {
return seedAwards();
})
.then(function() {
return seedTechnologies();
})
.then(function() {
return seedProjects();
})
.then(function() {
return seedHackathons();
})
.then(function() {
return seedUsers();
})
// User.findAsync({}).then(function(users) {
// if (users.length === 0) {
// return seedUsers();
// } else {
// return
// // console.log(chalk.magenta('Seems to already be user data, exiting!'));
// // process.kill(0);ç
// }
// })
// })
.then(function() {
console.log(chalk.green('Seed successful!'));
process.kill(0);
})
.catch(function(err) {
console.error(err);
process.kill(1);
});
});