-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreateDB.js
45 lines (36 loc) · 909 Bytes
/
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
var mongoose = require('libs/mongoose');
var async = require('async');
async.series([
open,
dropDatabase,
requireModels,
createUsers
],function(err){
if(err) console.log(err);
mongoose.disconnect();
process.exit(err ? 255 : 0);
});
function open (callback){
mongoose.connection.on('open',callback);
};
function dropDatabase(callback){
var db = mongoose.connection.db;
db.dropDatabase(callback);
};
function requireModels(callback){
require ('models/user').User;
async.each(Object.keys(mongoose.models), function(modelName,callback){
mongoose.models[modelName].ensureIndexes(callback);
},callback);
};
function createUsers(callback){
var users= [
{username:'Tom',password:"Tom"},
{username:'Jon',password:"Jon"},
{username:'admin',password:"admin"}
];
async.each(users,function(UserData,callback){
var user = new mongoose.models.User(UserData);
user.save(callback);
},callback);
};