-
Notifications
You must be signed in to change notification settings - Fork 0
/
createDb.js
50 lines (39 loc) · 1.2 KB
/
createDb.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
/**
* Created by we on 30-Dec-15.
*/
//D:\alexey\MongoDB\Server\3.2\bin\mongod.exe --dbpath "D:\alexey\MongoDB\db" -v
var mongooose = require('./common/mongoose'),
async = require('async');
mongooose.set('debug', true);
async.series([open, dropDatabase, requireModels, createUsers], function (err, results) {
if (err) {
process.exit(255);
return;
}
console.log('results: ', results);
mongooose.disconnect();
process.exit(0);
});
function open(callback) {
mongooose.connection.on('open', callback);
}
function dropDatabase(callback) {
mongooose.connection.db.dropDatabase(callback);
}
function requireModels(callback) {
require('./models/user');
async.each(Object.keys(mongooose.models), function (modelName, callback) {
mongooose.models[modelName].ensureIndexes(callback);
}, callback)
}
function createUsers(callback) {
var users = [
{username: 'Vasiliy', password: 'pi[i[ipi[i'},
{username: 'Peter', password: 'mimimi'},
{username: 'admin', password: 'kikiki'}
];
async.each(users, function (userData, callback) {
var user = mongooose.models.User(userData);
user.save(callback)
}, callback);
}