-
Notifications
You must be signed in to change notification settings - Fork 8
/
seed.js
151 lines (138 loc) · 4.13 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
var mongoose = require('mongoose'),
config = require('./server/config/config'),
bcrypt = require('bcrypt'),
MongoClient = require('mongodb').MongoClient,
async = require('async'),
fs = require('fs'),
Schema = mongoose.Schema,
uploadsDir = './server/uploads',
tempDir = './server/uploads/tmp',
formsDir = './server/uploads/forms',
profilesDir = './server/uploads/profiles';
var databaseURL = config.database;
var user = new Schema({
email: {type: String, unique: true, required: true, lowercase: true},
password: {type: String, required: true},
forms: [{type: Array}],
role: {type: Array, default: ['user']}
},
{timestamps: true}
);
user.pre('save', function (next) {
var user = this;
if (this.isModified('password') || this.isNew) {
bcrypt.genSalt(10, function (err, salt) {
if (err) {
return next(err);
}
bcrypt.hash(user.password, salt, (err, hash) => {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
} else {
return next();
}
});
var User = mongoose.model('User', user);
async.series([
function (callback) {
if (!fs.existsSync(uploadsDir)){
fs.mkdirSync(uploadsDir);
}else
{
console.log("Uploads directory already exist");
}
if (!fs.existsSync(formsDir)){
fs.mkdirSync(formsDir);
}else
{
console.log("Forms directory already exist");
}
if (!fs.existsSync(tempDir)){
fs.mkdirSync(tempDir);
}else
{
console.log("Temp directory already exist");
}
if (!fs.existsSync(profilesDir)){
fs.mkdirSync(profilesDir);
}else
{
console.log("Profiles directory already exist");
}
MongoClient.connect(databaseURL, function (err, db) {
if (err) throw err;
db.dropDatabase(function (err, result) {
if (err) throw err;
console.log('drop ', result);
db.close(true, function (err, result) {
if (err) throw err;
console.log('close ', result);
callback(null, 'Success, database is dropped');
})
})
})
},
function (callback) {
mongoose.Promise = global.Promise;
mongoose.connect(databaseURL);
mongoose.connection.on('connected', function () {
console.log('db connected via mongoose');
callback(null, 'SUCCESS - Connected to mongodb');
});
},
function (callback) {
// BEGIN SEED DATABASE
var users = [];
var userCount = 1;
// for (var i = 0; i < userCount; i++) {
var user = new User({
email: '[email protected]',
// Password will be hashed in the userSchema.pre middleware
password: 'testpass',
forms: [],
role: 'admin'
});
users.push(user);
// }
console.log("Populating database with %s users ", users.length);
async.eachSeries(
users,
function (user, userSavedCallBack) {
user.save(function (err) {
if (err) {
// Send JSON response to console for errors
console.dir(err);
}
console.log("Saving user # %s out of %s ", user.email, userCount);
userSavedCallBack();
});
},
function (err) {
if (err) console.dir(err);
console.log("Finished aysnc.each in seeding db");
// Execute callback function from line 130 to signal to async.series that
// all asynchronous calls are now done
callback(null, 'SUCCESS - Seed database');
}
);
}
],
// This function executes when everything above is done
function (err, results) {
console.log("\n\n--- Database seed progam completed ---");
if (err) {
console.log("Errors = ");
console.dir(errors)
} else {
console.log("Results = ");
console.log(results);
}
console.log("\n\n--- Exiting database seed progam ---");
// Exit the process to get back to terrminal console
process.exit(0);
});