-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathinit_db.js
70 lines (59 loc) · 1.62 KB
/
init_db.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
'use strict';
require('babel-core/register');
var mongoose = require('mongoose'),
env = require('./env'),
models = require('./src/server/models').default;
//data
var phones = require('./init_data/phones'),
computers = require('./init_data/computers'),
printers = require('./init_data/printers');
mongoose.connection.once('open', function () {
var Func = models.Func,
Log = models.Log,
Phones = models.Phones,
Computers = models.Computers,
Printers = models.Printers,
Notes = models.Notes;
//DATA FOR FILLING
var funcs = [{
func: 'function() {return 0;}'
}];
//remove models functions
var removeFuncs = function() {
Func.remove({}).then(removeLogs);
};
var removeLogs = function() {
Log.remove({}).then(removePhones);
};
var removePhones = function() {
Phones.remove({}).then(removeComputers);
};
var removeComputers = function() {
Computers.remove({}).then(removePrinters);
};
var removePrinters = function() {
Printers.remove({}).then(addPhones);
};
//add data functions
var addPhones = function() {
Phones.collection.insert(phones, addComputers);
};
var addComputers = function() {
Computers.collection.insert(computers, addPrinters);
};
var addPrinters = function() {
Printers.collection.insert(printers, addInnerFunc);
};
var addInnerFunc = function() {
Func.collection.insert(funcs, function() {
console.log('data updated, connection is closed');
mongoose.connection.close();
});
};
//function to start working with db
var startInitDb = function() {
removeFuncs();
};
startInitDb();
});
mongoose.connect(env.get('mongo:uri'), env.get('mongo:options'));