-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateDb.js
More file actions
85 lines (74 loc) · 1.85 KB
/
createDb.js
File metadata and controls
85 lines (74 loc) · 1.85 KB
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
var mongoose = require('server/libs/mongoose');
// mongoose.set('debug', true);
var async = require('async');
async.series([
open,
dropDatabase,
requireModels,
createUsers
], function(err, results) {
console.log(arguments);
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('server/models/user');
async.each(Object.keys(mongoose.models), function(modelName, callback) {
mongoose.models[modelName].ensureIndexes(callback);
}, callback);
}
function createUsers(callback) {
var users = [
{username: "Seva", password: "123"},
{username: "Petya", password: "234"},
{username: "Vasya", password: "312"}
];
async.each(users, function(userData, callback) {
var user = new mongoose.models.User(userData);
user.save(callback);
}, callback);
}
// var user = new User ({
// username: "Tester2",
// password: "secret"
// });
//
// user.save(function(err, user, affected) {
// if(err) throw err;
// console.log(arguments);
//
// User.findOne({username: "Tester"}, function(err, tester) {
// console.log(tester);
// });
// });
// var mongoose = require('mongoose');
// mongoose.connect('mongodb://localhost/test');
//
// var schema = mongoose.Schema({
// name: String,
// surname: String
// });
//
// schema.methods.meow = function() {
// console.log(this.get('name'));
// console.log(this.get('surname'));
// };
//
// var Cat = mongoose.model('Cat', schema);
//
// var kitty = new Cat({
// name: 'Zildjian',
// surname: 'Cool'
// });
//
//
// kitty.save(function(err, kitty, affected){
// kitty.meow();
// });