-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoManager.js
267 lines (232 loc) · 8.79 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
// ███╗ ███╗ ██████╗ ███╗ ██╗ ██████╗ ██████╗
// ████╗ ████║██╔═══██╗████╗ ██║██╔════╝ ██╔═══██╗
// ██╔████╔██║██║ ██║██╔██╗ ██║██║ ███╗██║ ██║
// ██║╚██╔╝██║██║ ██║██║╚██╗██║██║ ██║██║ ██║
// ██║ ╚═╝ ██║╚██████╔╝██║ ╚████║╚██████╔╝╚██████╔╝
// ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═════╝
// ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗██████╗
// ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝██╔══██╗
// ██╔████╔██║███████║██╔██╗ ██║███████║██║ ███╗█████╗ ██████╔╝
// ██║╚██╔╝██║██╔══██║██║╚██╗██║██╔══██║██║ ██║██╔══╝ ██╔══██╗
// ██║ ╚═╝ ██║██║ ██║██║ ╚████║██║ ██║╚██████╔╝███████╗██║ ██║
// ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═════╝ ╚═ ═════╝╚═╝ ╚═╝
/** Dependencies needed */
const mongoose = require('mongoose');
const {
MongoClient,
ObjectID
} = require('mongodb');
var config = require('./config.json');
const chalk = require('chalk');
/** Get data from the configuration files */
let connectionURI = config.mongoConnection.mongoURI;
/** Get the Schemas */
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({
from: jsonDoc.from,
to: jsonDoc.to,
funds: jsonDoc.funds
});
return tx.save()
.then(() => {
return true;
})
.catch(function (err) {
console.log('An error occured: ', chalk.bold.red(err));
});
}
/**
* @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) {
const METHOD = 'saveAsset';
var wallet = new Wallet({
id: jsonDoc[0],
balance: jsonDoc[1]
});
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 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 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 Asset Command
* @returns {Promise} whose fullfilment means the asset has been persisted
*/
static saveAst(jsonDoc) {
let mong = new MongoManager();
return mong.saveAsset(jsonDoc);
}
/**
* @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 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 All Asset's id command
* @returns {Promise} whose fullfilment means either all assets were retrieved or none existed
*/
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));
});
}
/**
* @description Removes all Wallets
* @returns {Promise} whose fullfilment means all assets were deleted
*/
static removeAllAssets() {
return Wallet.remove({})
.then(() => {
console.log(('All Wallets have been removed'));
})
.catch(function () {
console.log('An error occured: ', chalk.bold.red(error));
});
}
/**
* @description Removes all Transactions
* @returns {Promise} whose fullfilment means all Transactions were deleted
*/
static removeAllTransactions() {
return Transaction.remove({})
.then(() => {
console.log(('All Transactions have been removed'));
})
.catch(function () {
console.log('An error occured: ', chalk.bold.red(error));
})
}
}
module.exports = MongoManager;