-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulatedb.js
150 lines (130 loc) · 3.82 KB
/
populatedb.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
#! /usr/bin/env node
var userArgs = process.argv.slice(2);
var async = require('async')
var Table = require('./models/table')
var Dish = require('./models/dish')
var TableReservation = require('./models/tableReservation')
var mongoose = require('mongoose');
var mongoDB = userArgs[0];
mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true});
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
var tables = []
var dishes = []
var tableReservations = []
function tableCreate(name, cb) {
tabledetail = { name:name }
var table = new Table(tabledetail);
table.save(function (err) {
if (err) {
cb(err, null)
return
}
console.log('New Table: ' + table);
tables.push(table)
cb(null, table)
} );
}
function dishCreate(name, price, cb) {
dishdetail = { name:name, price:price }
var dish = new Dish(dishdetail);
dish.save(function (err) {
if (err) {
cb(err, null)
return
}
console.log('New Dish: ' + dish);
dishes.push(dish)
cb(null, dish)
} );
}
function tableReservationCreate(table, d_begin, d_end, cb) {
tableReservationdetail = { table:table }
if (d_begin != false) tableReservationdetail.date_of_begin = d_begin
if (d_end != false) tableReservationdetail.date_of_end = d_end
var tableReservation = new TableReservation(tableReservationdetail);
tableReservation.save(function (err) {
if (err) {
console.log('ERROR CREATING TableReservation: ' + tableReservation);
cb(err, null)
return
}
console.log('New TableReservation: ' + tableReservation);
tableReservations.push(tableReservation)
cb(null, tableReservation)
} );
}
function createTables(cb) {
async.series([
function(callback) {
tableCreate('Столик1', callback);
},
function(callback) {
tableCreate('Столик2', callback);
},
function(callback) {
tableCreate('Столик3', callback);
},
function(callback) {
tableCreate('Столик4', callback);
},
function(callback) {
tableCreate('Столик5', callback);
}
],
// optional callback
cb);
}
function createDishes(cb) {
async.series([
function(callback) {
dishCreate('Оливье', '150 рублей', callback);
},
function(callback) {
dishCreate('Спагетти', '170 рублей', callback);
},
function(callback) {
dishCreate('Суп', '120 рублей', callback);
},
function(callback) {
dishCreate('Шашлык', '300 рублей', callback);
},
function(callback) {
dishCreate('Стакан воды', '20 рублей', callback);
}
],
// optional callback
cb);
}
function createTableReservations(cb) {
async.series([
function (callback) {
tableReservationCreate(tables[0], new Date(2022, 0, 20, 15), new Date(2022, 0, 20, 18), callback);
},
function (callback) {
tableReservationCreate(tables[1], new Date(2022, 0, 22, 12), new Date(2022, 0, 22, 16), callback);
},
function (callback) {
tableReservationCreate(tables[0], new Date(2022, 0, 20, 12), new Date(2022, 0, 20, 14), callback);
}
],
// optional callback
cb);
}
async.series([
createTables,
createDishes,
createTableReservations
],
// Optional callback
function(err, results) {
if (err) {
console.log('FINAL ERR: '+err);
}
else {
console.log('TABLEReservations: '+tableReservations);
}
// All done, disconnect from database
mongoose.connection.close();
});