-
Notifications
You must be signed in to change notification settings - Fork 1
/
seed.js
193 lines (176 loc) · 5.2 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
192
193
/*
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 _ = require('lodash');
var mongoose = require('mongoose');
var Promise = require('bluebird');
var chalk = require('chalk');
var connectToDb = require('./server/db');
var User = mongoose.model('User');
var Screenplay = mongoose.model('Screenplay');
var Character = mongoose.model('Character');
var Component = mongoose.model('Component');
var Scene = mongoose.model('Scene');
var allScreenplays = require('./seed/screenplays');
var wipeCollections = function () {
var removeUsers = User.remove({});
var removeScreenplays = Screenplay.remove({});
var removeCharacters = Character.remove({});
var removeComponents = Component.remove({});
var removeScenes = Scene.remove({});
return Promise.all([
removeUsers,
removeScreenplays,
removeCharacters,
removeComponents,
removeScenes
]);
};
var seedUsers = function () {
var users = [
{
email: '[email protected]',
password: 'password'
},
{
email: '[email protected]',
password: 'potus'
},
{
email: '[email protected]',
password: 'zeke',
isAdmin: true
},
{
email: '[email protected]',
password: 'kim'
},
{
email: '[email protected]',
password: 'wookies'
}
];
return User.create(users);
};
var seedUsers = function () {
var users = [
{
email: '[email protected]',
password: 'password'
},
{
email: '[email protected]',
password: 'potus'
},
{
email: '[email protected]',
password: 'zeke',
isAdmin: true
},
{
email: '[email protected]',
password: 'kim'
},
{
email: '[email protected]',
password: 'wookies'
}
];
return User.create(users);
};
function seedScreenplaysTwo(screenplay){
var currentSP, currentScenes, user, savedComponents;
return User.findOne({email: screenplay.user})
.then(selectedUser => {
user = selectedUser;
})
.then(() => {
return Screenplay.create(screenplay.screenplay)
})
.then(createdScreenplay => {
currentSP = createdScreenplay;
if(!user.screenplay) user.screenplay = [];
user.screenplay.push(currentSP._id);
return user.save();
})
.then(() => {
return Scene.create(screenplay.scenes);
})
.then(scenes => {
// console.log(scenes);
currentScenes = scenes;
ScenesIds = currentScenes.map(ele => {
return ele._id;
});
currentSP.scenes = ScenesIds;
return currentSP.save();
})
.then(() => {
// console.log(currentScenes);
compPromiseArray = screenplay.components.map( ele => {
return Component.create(ele);
});
return Promise.all(compPromiseArray);
})
.then(createdComponents => {
savedComponents = createdComponents;
createdComponents.forEach((arrayOfComp, index) => {
currentScenes[index].components = arrayOfComp.map( comp =>{
return comp._id;
});
});
return Promise.all(currentScenes.map(scene => scene.save()));
})
.then(() => {
var charsToBeSavedPromiseArray = [];
var copies = [];
savedComponents.forEach(componentsArray => {
componentsArray.forEach(comp => {
if(comp.charName) {
var characterNameUpperCase = comp.charName.toUpperCase();
console.log(copies);
if(copies.indexOf(characterNameUpperCase) < 0){
charsToBeSavedPromiseArray.push(Character.create({name: characterNameUpperCase, screenplay: currentSP._id}));
copies.push(characterNameUpperCase);
}
}
})
})
return Promise.all(charsToBeSavedPromiseArray);
})
.then(savedChars => {
console.log('everything saved');
})
.catch(console.error.bind(console));
}
// Dialogue => Character => Scene =>
connectToDb
.then(function () {
return wipeCollections();
})
.then(function () {
return seedUsers();
})
.then(function(){
return Promise.all(allScreenplays.map(sp => {
return seedScreenplaysTwo(sp);
}));
})
.then(function () {
console.log(chalk.green('Seed successful!'));
process.kill(0);
})
.catch(function (err) {
console.error(err);
process.kill(1);
});