-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
42 lines (37 loc) · 1000 Bytes
/
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
const { MongoClient } = require("mongodb");
const url =
"mongodb+srv://Veronika_admin:[email protected]/?retryWrites=true&w=majority";
const client = new MongoClient(url);
const dbName = "wallet";
async function createTransaction(transactionData) {
try {
await client.connect();
const db = client.db(dbName);
const col = db.collection("transaction");
const result = await col.insertOne(transactionData);
return result;
} catch (err) {
console.error(err);
throw err;
} finally {
await client.close();
}
}
async function getAllTransactions() {
try {
await client.connect();
const db = client.db(dbName);
const col = db.collection("transaction");
const result = await col.find().toArray();
return result;
} catch (err) {
console.error(err);
throw err;
} finally {
await client.close();
}
}
module.exports = {
createTransaction,
getAllTransactions
};