-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoManager.js
329 lines (284 loc) · 11 KB
/
mongoManager.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// ███╗ ███╗ ██████╗ ███╗ ██╗ ██████╗ ██████╗
// ████╗ ████║██╔═══██╗████╗ ██║██╔════╝ ██╔═══██╗
// ██╔████╔██║██║ ██║██╔██╗ ██║██║ ███╗██║ ██║
// ██║╚██╔╝██║██║ ██║██║╚██╗██║██║ ██║██║ ██║
// ██║ ╚═╝ ██║╚██████╔╝██║ ╚████║╚██████╔╝╚██████╔╝
// ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═════╝
// ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗██████╗
// ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝██╔══██╗
// ██╔████╔██║███████║██╔██╗ ██║███████║██║ ███╗█████╗ ██████╔╝
// ██║╚██╔╝██║██╔══██║██║╚██╗██║██╔══██║██║ ██║██╔══╝ ██╔══██╗
// ██║ ╚═╝ ██║██║ ██║██║ ╚████║██║ ██║╚██████╔╝███████╗██║ ██║
// ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═════╝ ╚═ ═════╝╚═╝ ╚═╝
/** Dependencies needed */
const mongoose = require('mongoose');
const {
MongoClient,
ObjectID
} = require('mongodb');
var config = require('config').get('mongo-connection');
/** Get data from the configuration files */
let connectionURI = config.get('mongoURI');
/** Get the Schemas */
var {
Participant
} = require('./Schemas/participant');
var {
Transaction
} = require('./Schemas/transaction');
var {
Wallet
} = require('./Schemas/wallet');
//Start the connection to Mongoose
mongoose.Promise = global.Promise;
mongoose.connect(connectionURI, {
useMongoClient: true
});
class MongoManager {
/**@description Persists a transaction into MongoDB
* @param {JSON} a JSON Document with transaction information
* @return {Promise} whose fulfillment means the transaction has been saved
*/
saveTransaction(jsonDoc) {
const METHOD = 'saveTransaction';
var tx = new Transaction({
amount: jsonDoc.amount,
from: {
id: jsonDoc.from.id,
balance: jsonDoc.from.balance,
owner: jsonDoc.from.owner
},
to: {
id: jsonDoc.to.id,
balance: jsonDoc.to.balance,
owner: jsonDoc.to.owner
}
});
return tx.save()
.then(() => {
return true;
})
.catch(function () {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Persists a participant into MongoDB
* @param {JSON} a JSON Document with participant information
* @return {Promise} whose fulfillment means the participant has been saved
*/
saveParticipant(jsonDoc) {
const METHOD = 'saveParticipant';
var ptc = new Participant({
id: jsonDoc.id
});
return ptc.save()
.then(() => {
return true;
})
.catch(function () {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Persists an asset into MongoDB
* @param {JSON} a JSON Document with asset information
* @param {String} an md5 address of the owner
* @return {Promise} whose fulfillment means the asset has been saved
*/
saveAsset(jsonDoc, idOwner) {
const METHOD = 'saveAsset';
var wallet = new Wallet({
id: jsonDoc.id,
balance: jsonDoc.balance,
ownerID: idOwner
});
return wallet.save()
.then(() => {
return true;
})
.catch(function () {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Queries all transactions persisted on MongoDB
* @return {JSON} a file with all transactions persisted on mongoDB
*/
getAllTransactions() {
const METHOD = 'getAllTransactions';
return Transaction.find({})
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Queries all participants persisted on MongoDB
* @return {JSON} a file with all participants persisted on mongoDB
*/
getAllParticipants() {
return Participant.find({})
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Queries all Assets persisted on MongoDB
* @return {JSON} a file with all assets persisted on mongoDB
*/
getAllAssets() {
return Wallet.find({})
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Queries one asset persisted on MongoDB
* @param {String} Id of the wallet on MongoDB
* @return {JSON} a file with one asset persisted on mongoDB
*/
getOneAsset(identifier) {
return Wallet.findOne({
id: identifier
})
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Queries one participant persisted on MongoDB
* @param {String} Id of the participant on MongoDB
* @return {JSON} a file with one participant persisted on mongoDB
*/
getOneParticipant(identifier) {
return Participant.findOne({
id: identifier
})
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Queries all assets on MongoDB
* @return {JSON} a file with all asset's id persisted on mongoDB
*/
getAllAssetsID() {
return Wallet.find({}, 'id -_id')
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Executes the Save Transaction Command
* @returns {Promise} whose fullfilment means the transaction has been persisted
*/
static saveTx(jsonDoc) {
let mong = new MongoManager();
return mong.saveTransaction(jsonDoc);
}
/**@description Executes the Save Participant Command
* @returns {Promise} whose fullfilment means the participant has been persisted
*/
static savePnt(jsonDoc) {
let mong = new MongoManager();
return mong.saveParticipant(jsonDoc);
}
/**@description Executes the Save Asset Command
* @returns {Promise} whose fullfilment means the asset has been persisted
*/
static saveAst(jsonDoc, idOwner) {
let mong = new MongoManager();
return mong.saveAsset(jsonDoc, idOwner);
}
/**@description Executes the Get All Transaction command
* @returns {Promise} whose fullfilment means all transactions have been retrieved
*/
static getAllTx() {
let mong = new MongoManager();
return mong.getAllTransactions()
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Executes the Get All Participants command
* @returns {Promise} whose fullfilment means all participants have been retrieved
*/
static getAllPnt() {
let mong = new MongoManager();
return mong.getAllParticipants()
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Executes the Get All Assets command
* @returns {Promise} whose fullfilment means all assets have been retrieved
*/
static getAllAst() {
let mong = new MongoManager();
return mong.getAllAssets()
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Executes the Get One Asset command
* @param {String} that identifies the asset on the Database
* @returns {Promise} whose fullfilment means either the asset was retrieved or none existed
*/
static getOneAst(identifier) {
let mong = new MongoManager();
return mong.getOneAsset(identifier)
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Executes the Get One Participant command
* @param {String} that identifies the participant on the Database
* @returns {Promise} whose fullfilment means either the participant was retrieved or none existed
*/
static getOnePnt(identifier) {
let mong = new MongoManager();
return mong.getOneParticipant(identifier)
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**@description Executes the All Asset's id command
* @returns {Promise} whose fullfilment means either all assets were retrieved or none existed
* @deprecated Should be taken out by release 1.2 if not used
*/
static getAllAstID() {
let mong = new MongoManager();
return mong.getAllAssetsID()
.then((result) => {
return result;
})
.catch(function (error) {
console.log('An error occured: ', chalk.bold.red(error));
});
}
}
module.exports = MongoManager;