Skip to content

Commit 383f035

Browse files
Database-w4-Assignment
1 parent 1e3312b commit 383f035

File tree

16 files changed

+631
-0
lines changed

16 files changed

+631
-0
lines changed

Week4/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
node_modules/
63.2 KB
Loading
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { transferAmount } from "./transfer.js";
2+
3+
async function main() {
4+
await transferAmount(101, 102, 1000);
5+
}
6+
7+
main();

Week4/homework/Ex2-Transactions/package-lock.json

Lines changed: 179 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "ex2-transactions",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"dotenv": "^17.2.3",
15+
"mongodb": "^7.0.0"
16+
}
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { MongoClient } from "mongodb";
2+
import dotenv from "dotenv";
3+
dotenv.config();
4+
5+
const client = new MongoClient(process.env.MONGO_URI);
6+
7+
export async function setup() {
8+
try {
9+
await client.connect();
10+
const db = client.db("databaseWeek4");
11+
12+
const accounts = db.collection("accounts");
13+
const accountChanges = db.collection("account_changes");
14+
15+
// Clear previous data
16+
await accounts.deleteMany({});
17+
await accountChanges.deleteMany({});
18+
19+
// Insert sample accounts
20+
await accounts.insertMany([
21+
{ account_number: 101, balance: 5000 },
22+
{ account_number: 102, balance: 3000 }
23+
]);
24+
25+
console.log("Setup done: collections created and sample data inserted.");
26+
} catch (err) {
27+
console.error(err);
28+
} finally {
29+
await client.close();
30+
}
31+
}
32+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { MongoClient } from "mongodb";
2+
import dotenv from "dotenv";
3+
dotenv.config();
4+
5+
const client = new MongoClient(process.env.MONGO_URI);
6+
7+
export async function transferAmount(fromAcc, toAcc, amount) {
8+
const session = client.startSession();
9+
10+
try {
11+
await client.connect();
12+
const db = client.db("databaseWeek4");
13+
const accounts = db.collection("accounts");
14+
const accountChanges = db.collection("account_changes");
15+
16+
await session.withTransaction(async () => {
17+
// Deduct from sender
18+
const fromResult = await accounts.updateOne(
19+
{ account_number: fromAcc },
20+
{ $inc: { balance: -amount } },
21+
{ session }
22+
);
23+
24+
// Add to receiver
25+
const toResult = await accounts.updateOne(
26+
{ account_number: toAcc },
27+
{ $inc: { balance: amount } },
28+
{ session }
29+
);
30+
31+
// Log changes
32+
await accountChanges.insertMany([
33+
{
34+
account_number: fromAcc,
35+
amount: -amount,
36+
changed_date: new Date(),
37+
remark: `Transferred ${amount} to account ${toAcc}`
38+
},
39+
{
40+
account_number: toAcc,
41+
amount: amount,
42+
changed_date: new Date(),
43+
remark: `Received ${amount} from account ${fromAcc}`
44+
}
45+
], { session });
46+
});
47+
48+
console.log(`Transaction successful: ${amount} transferred from ${fromAcc} to ${toAcc}`);
49+
} catch (err) {
50+
console.error("Transaction failed:", err);
51+
} finally {
52+
await session.endSession();
53+
await client.close();
54+
}
55+
}
44.4 KB
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import fs from "fs";
2+
import csv from "csv-parser";
3+
import { MongoClient } from "mongodb";
4+
import dotenv from "dotenv";
5+
dotenv.config();
6+
7+
const client = new MongoClient(process.env.MONGO_URI);
8+
9+
async function run() {
10+
try {
11+
await client.connect();
12+
console.log("Connected to MongoDB");
13+
14+
const db = client.db("databaseWeek4");
15+
const collection = db.collection("population");
16+
17+
const results = [];
18+
19+
fs.createReadStream("./homework/ex1-aggregation/population_pyramid_1950-2022.csv")
20+
.pipe(csv())
21+
.on("data", (row) => {
22+
results.push({
23+
Country: row.Country,
24+
Year: Number(row.Year),
25+
Age: row.Age,
26+
M: Number(row.M),
27+
F: Number(row.F),
28+
});
29+
})
30+
.on("end", async () => {
31+
const inserted = await collection.insertMany(results);
32+
console.log("Inserted:", inserted.insertedCount, "documents");
33+
await client.close();
34+
});
35+
36+
} catch (err) {
37+
console.error(err);
38+
}
39+
}
40+
41+
run();
36.4 KB
Loading

0 commit comments

Comments
 (0)