-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
222 lines (173 loc) · 6.28 KB
/
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
var winston = require('winston');
var async = require('async');
var config = require('./config');
var Schema = mongoose.Schema;
//////
var User = new Schema({
// user can log in using this one too
shortId: {type: String, required:true},
email: {type: String, required: true},
password: {type: String, required: true},
validated: {type:Boolean, required: true},
// optional
facebookID: {type: String, required:false},
validationSig: {type: String, required: false},
resetSig: {type: String, required: false},
created: { type: Date, default: Date.now, required:false },
modified: { type: Date, default: Date.now, required:false },
comment: {type: String, required:false}
});
User.statics.findByEmail = function(e,cb){
this.find({ email: e}, cb);
}
User.statics.findByShortId = function(id,cb){
this.find({ shortId: id }, cb);
}
User.statics.findByFacebookID = function(id,cb){
this.find({facebookID: id}, cb);
}
var Organizer = new Schema({
organizer: {type: String, required:false},
organizer_inn: {type: String, required:false},
organizer_ogrn: {type: String, required:false},
organizer_ogrnip: {type: String, required:false},
organizer_address: {type: String, required:false},
});
////////////////
var Ticket = new Schema({
serial_number: {type: String, required:true},
// 0 - init
// 1 - sold
// 2 - cancelled
state: {type: Number, required:true},
created: { type: Date, default: Date.now, required: true},
price_kop: {type: Number, required:false},
is_paper_ticket: {type: Boolean, required:false},
issuer: {type: String, required:false},
// WARNING: inn is required!
issuer_inn: {type: String, required:false},
issuer_ogrn: {type: String, required:false},
issuer_ogrnip: {type: String, required:false},
issuer_address: {type: String, required:false},
event_title: {type: String, required:false},
event_place_title: {type: String, required:false},
//event_date: { type: Date, required:false },
event_date: { type: String, required:false },
event_place_address: {type: String, required:false},
row: {type: String, required:false},
seat: {type: String, required:false},
ticket_category: {type: Number, required:false},
//organizer: {type: Schema.ObjectId, required: true},
organizer: {type: String, required:false},
organizer_inn: {type: String, required:false},
organizer_ogrn: {type: String, required:false},
organizer_ogrnip: {type: String, required:false},
organizer_address: {type: String, required:false},
seller: {type: String, required:false},
seller_inn: {type: String, required:false},
seller_ogrn: {type: String, required:false},
seller_ogrnip: {type: String, required:false},
seller_address: {type: String, required:false},
buyer_name: {type: String, required:false},
//buying_date: { type: Date, required:false },
//cancelled_date: { type: Date, required:false},
buying_date: { type: String, required:false },
cancelled_date: { type: String, required:false},
tx_hash: {type:String, required: false},
contract_address: {type:String, required: false}
});
Ticket.plugin(mongoosePaginate);
var Batch = new Schema({
organizer_inn: {type: String, required:false},
//organizer: {type: Schema.ObjectId, required:true},
tickets: [{
ticketId: {type:Schema.ObjectId, required:true},
}],
});
//Batch.plugin(mongoosePaginate);
var Task = new Schema({
fileName: {type: String, required:true},
fileNameReal: {type: String, required:true},
organizer_inn: {type: String, required:true},
// 0 - created
// 1 - processing
// 2 - ready
status: {type: Number, required:true},
batch_id: {type: Schema.ObjectId, required:false},
// each item is a line index
error_indexes: [{
index: {type:Number, required:false},
}],
// each item is a serial_number
collisions: [{
serial_number: {type:String, required:false},
}]
});
/// \brief Call this one and keep returned object
function connectToDbCallback(uri,user,pass,cb){
var options = {
db: { native_parser: true },
server: { poolSize: 5 },
//replset: { rs_name: 'myReplicaSetName' },
user: user, // can be empty
pass: pass, // can be empty
};
options.server.socketOptions = { keepAlive: 1 };
//options.replset.socketOptions = { keepAlive: 1 };
mongoose.connect(uri,options);
var db = mongoose.connection;
db.on('error', function (err) {
cb(err);
});
db.once('open', function callback () {
cb(null,db);
});
db.on('disconnected', function () {
winston.info('DB disconnected');
});
}
// Simple wrapper for convinience...
function connectToDb(uri,user,pass){
// We are under tests - so setup behaviour
if(uri.indexOf('test')!==-1){
}
return connectToDbCallback(uri,user,pass,function(err,cb){
if(!err){
winston.info("Connected to DB!");
}else{
winston.error('DB connection error:', err.message);
}
});
}
function blockLogging(){
winston.remove(winston.transports.Console);
}
/// \brief Call this one when connection is no more needed
function disconnectDb(){
mongoose.disconnect();
}
function removeDb(cb){
mongoose.connection.once('connected', () => {
mongoose.connection.db.dropDatabase();
cb();
});
}
// Exports:
var UserModel = mongoose.model('User', User);
var TicketModel = mongoose.model('Ticket', Ticket);
var OrganizerModel = mongoose.model('Organizer', Organizer);
var BatchModel = mongoose.model('Batch', Batch);
var TaskModel = mongoose.model('Task', Task);
module.exports.UserModel = UserModel;
module.exports.TicketModel = TicketModel;
module.exports.OrganizerModel = OrganizerModel;
module.exports.BatchModel = BatchModel;
module.exports.TaskModel = TaskModel;
//
module.exports.blockLogging = blockLogging;
module.exports.connectToDb = connectToDb;
module.exports.connectToDbCallback = connectToDbCallback;
module.exports.disconnectDb = disconnectDb;
module.exports.removeDb = removeDb;