-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseed.js
91 lines (80 loc) · 2.49 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
/*
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 User2 = mongoose.model('User');
var Lobby = mongoose.model('Lobby');
var seedUsers = function () {
var users = [
{
email: 'testing@fsa.com',
password: 'password',
username: 'TESTING'
},
{
email: 'obama@gmail.com',
password: 'potus',
username: 'ELPRESIDENTE'
},
{
email: 'victor@gmail.com',
password: 'victor',
username: 'VICTORISPOOPY'
},
{
email: 'daniel@gmail.com',
password: 'daniel',
username: 'DANIELISAWESOME'
}
];
return User.createAsync(users);
};
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() {
return User.find({}).exec();
}).then(function(fourUsers){
fourUsers = fourUsers.map(function(user){
return user._id;
})
return Lobby.create({
users: fourUsers,
public: true,
title: 'Test game from seed.js',
status: 'inProgress'
})
}).then(function(lobby) {
return User2.findById(lobby.users[0]).exec()
.then(function(user){
return [lobby._id, user]
})
}).then(function (lobbyAndUserIds) {
console.log(chalk.green('Seed successful!'));
console.log(chalk.green('Seed LobbyId: ', lobbyAndUserIds[0]));
console.log(chalk.green('Sedd user is ', lobbyAndUserIds[1]));
process.kill(0);
}).catch(function (err) {
console.error(err);
process.kill(1);
});
});