diff --git a/Week4/homework/ex1-aggregation/index.js b/Week4/homework/ex1-aggregation/index.js new file mode 100644 index 000000000..5adf824d3 --- /dev/null +++ b/Week4/homework/ex1-aggregation/index.js @@ -0,0 +1,100 @@ +import { MongoClient } from "mongodb"; +import "dotenv/config"; +const clientMongo = new MongoClient(process.env.MONGODB_URL); +import fs from "fs"; +import csv from "csv-parser"; +let data; + +async function processCSV(fileName) { + return new Promise((resolve, reject) => { + const data = []; + fs.createReadStream(fileName) + .pipe(csv()) + .on("data", (row) => data.push(row)) + .on("end", () => resolve(data)) + .on("error", (err) => reject(err)); + }); +} + +async function getTotalPopulationByYear(collection, country) { + const result = await collection + .aggregate([ + { $match: { Country: country } }, + { + $group: { + _id: { $toInt: "$Year" }, + countPopulation: { + $sum: { + $add: [{ $toInt: "$M" }, { $toInt: "$F" }], + }, + }, + }, + }, + { $sort: { _id: 1 } }, + ]) + .toArray(); + return result; +} + +async function getContinentsPopulation(collection, year, age) { + const result = await collection + .aggregate([ + { + $match: { + Year: year.toString(), + Age: age.toString(), + // Filter only continents by checking if Country is uppercase + $expr: { + $eq: ["$Country", { $toUpper: "$Country" }], + }, + }, + }, + { + $addFields: { + TotalPopulation: { $add: [{ $toInt: "$M" }, { $toInt: "$F" }] }, + }, + }, + { + $project: { + _id: 1, + Country: 1, + Year: 1, + Age: 1, + M: 1, + F: 1, + TotalPopulation: 1, + }, + }, + ]) + .toArray(); + return result; +} + +async function main() { + try { + await clientMongo.connect(); + const db = clientMongo.db("databaseWeek4"); + const collection = db.collection("population_pyramid"); + data = await processCSV( + "./Week4/homework/ex1-aggregation/population_pyramid_1950-2022.csv" + ); + await collection.insertMany(data); + data = await getTotalPopulationByYear(collection, "Netherlands"); + console.log( + "\nPopulation by Year (Netherlands):\n", + JSON.stringify(data, null, 2) + ); + data = await getContinentsPopulation(collection, "2020", "100+"); + console.log( + "\nPopulation by Continent (2020, 100+):\n", + JSON.stringify(data, null, 2) + ); + } catch (error) { + console.error("Error connecting to MongoDB:", error); + } finally { + await clientMongo.close(); + console.log("Connection to MongoDB closed"); + } +} + +main(); diff --git a/Week4/homework/ex2-transactions/data.json b/Week4/homework/ex2-transactions/data.json new file mode 100644 index 000000000..7c813b96d --- /dev/null +++ b/Week4/homework/ex2-transactions/data.json @@ -0,0 +1,41 @@ +{ + "accounts": [ + { "account_number": 101, "balance": 5000 }, + { "account_number": 102, "balance": 12000 }, + { "account_number": 103, "balance": 7500 }, + { "account_number": 104, "balance": 3000 }, + { "account_number": 105, "balance": 9500 } + ], + "account_changes": [ + { + "account_number": 101, + "amount": 1000, + "changed_date": "2025-09-01", + "remark": "Salary deposit" + }, + { + "account_number": 102, + "amount": -500, + "changed_date": "2025-09-02", + "remark": "Grocery shopping" + }, + { + "account_number": 103, + "amount": 2000, + "changed_date": "2025-09-03", + "remark": "Freelance payment" + }, + { + "account_number": 104, + "amount": -1000, + "changed_date": "2025-09-04", + "remark": "Rent payment" + }, + { + "account_number": 105, + "amount": 1500, + "changed_date": "2025-09-05", + "remark": "Gift received" + } + ] +} diff --git a/Week4/homework/ex2-transactions/index.js b/Week4/homework/ex2-transactions/index.js new file mode 100644 index 000000000..61b11d0df --- /dev/null +++ b/Week4/homework/ex2-transactions/index.js @@ -0,0 +1,23 @@ +import { setup } from "./setup.js"; +import { transfer } from "./transfer.js"; +let clientMongo, account; +async function main() { + try { + ({ clientMongo, account } = await setup()); + const transactionDetails = { + donator_account_number: 101, + receiver_account_number: 102, + amount: 1000, + changed_date: new Date().toISOString().slice(0, 10), // 'YYYY-MM-DD' + remark: `Transfer from 101 to 102`, + }; + await transfer(clientMongo, account, transactionDetails); + } catch (error) { + console.error("Error:", error); + } finally { + await clientMongo.close(); + console.log("Connection to MongoDB closed"); + } +} + +main(); diff --git a/Week4/homework/ex2-transactions/setup.js b/Week4/homework/ex2-transactions/setup.js new file mode 100644 index 000000000..e35972546 --- /dev/null +++ b/Week4/homework/ex2-transactions/setup.js @@ -0,0 +1,24 @@ +import { MongoClient } from "mongodb"; +import "dotenv/config"; +import fs from "fs"; + +export async function setup() { + const { accounts, account_changes } = JSON.parse( + fs.readFileSync("./Week4/homework/ex2-transactions/data.json") + ); + const clientMongo = new MongoClient(process.env.MONGODB_URL); + await clientMongo.connect(); + const db = clientMongo.db("databaseWeek4"); + const account = await db.createCollection("accounts"); + await account.deleteMany({}); + for (const acc of accounts) { + acc.account_changes = account_changes + .filter((a) => a.account_number === acc.account_number) + .map((a) => { + delete a.account_number; + return a; + }); + await account.insertOne(acc); + } + return { clientMongo, account }; +} diff --git a/Week4/homework/ex2-transactions/transaction.js b/Week4/homework/ex2-transactions/transaction.js new file mode 100644 index 000000000..93f4e8f91 --- /dev/null +++ b/Week4/homework/ex2-transactions/transaction.js @@ -0,0 +1,48 @@ +import { Client } from "pg"; +const config = { + host: "localhost", + user: "hyfuser", + password: "hyfpassword", + database: "transactions_week3", + port: 5432, +}; +const client = new Client(config); + +async function seedDatabase(client) { + try { + await client.connect(); + console.log("Connected to PostgreSQL database!"); + + const donator_account_number = 101; + const receiver_account_number = 102; + const amount = 1000; + const changed_date = new Date().toISOString().slice(0, 10); // 'YYYY-MM-DD' + const remark = `Transfer from 101 to 102`; + + await client.query("BEGIN"); + await client.query( + "UPDATE ACCOUNT SET balance = balance - $1 WHERE account_number = $2", + [amount, donator_account_number] + ); + await client.query( + "UPDATE ACCOUNT SET balance = balance + $1 WHERE account_number = $2", + [amount, receiver_account_number] + ); + await client.query( + "INSERT INTO account_changes(account_number, amount, changed_date, remark) VALUES($1, $2, $3, $4)", + [donator_account_number, -amount, changed_date, remark] + ); + await client.query( + "INSERT INTO account_changes(account_number, amount, changed_date, remark) VALUES($1, $2, $3, $4)", + [receiver_account_number, amount, changed_date, remark] + ); + await client.query("COMMIT"); + console.log("Transaction completed!"); + } catch (error) { + console.error("Error seeding database:", error); + } finally { + await client.end(); + } +} + +seedDatabase(client); diff --git a/Week4/homework/ex2-transactions/transfer.js b/Week4/homework/ex2-transactions/transfer.js new file mode 100644 index 000000000..9c28bd2a7 --- /dev/null +++ b/Week4/homework/ex2-transactions/transfer.js @@ -0,0 +1,42 @@ +export async function transfer(clientMongo, account, transactionDetails) { + const { + donator_account_number, + receiver_account_number, + amount, + changed_date, + remark, + } = transactionDetails; + const session = clientMongo.startSession(); + const transactionOptions = { + readPreference: "primary", + readConcern: { level: "local" }, + writeConcern: { w: "majority" }, + }; + try { + await session.withTransaction(async () => { + await account.updateOne( + { account_number: donator_account_number }, + { + $inc: { balance: -amount }, + $push: { account_changes: { amount: -amount, changed_date, remark } }, + }, + { session } + ); + await account.updateOne( + { account_number: receiver_account_number }, + { + $inc: { balance: amount }, + $push: { account_changes: { amount: amount, changed_date, remark } }, + }, + { session } + ); + }, transactionOptions); + console.log("Transaction committed."); + } catch (error) { + console.log("Transaction aborted.", error); + throw new Error(error); + } finally { + await session.endSession(); + console.log("Transaction completed!"); + } +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..da3a745b9 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,185 @@ +{ + "name": "databases-Cohort53", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "csv-parser": "^3.2.0", + "dotenv": "^17.2.2", + "mongodb": "^6.19.0" + } + }, + "node_modules/@mongodb-js/saslprep": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.3.0.tgz", + "integrity": "sha512-zlayKCsIjYb7/IdfqxorK5+xUMyi4vOKcFy10wKJYc63NSdKI8mNME+uJqfatkPmOSMMUiojrL58IePKBm3gvQ==", + "license": "MIT", + "dependencies": { + "sparse-bitfield": "^3.0.3" + } + }, + "node_modules/@types/webidl-conversions": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz", + "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==", + "license": "MIT" + }, + "node_modules/@types/whatwg-url": { + "version": "11.0.5", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.5.tgz", + "integrity": "sha512-coYR071JRaHa+xoEvvYqvnIHaVqaYrLPbsufM9BF63HkwI5Lgmy2QR8Q5K/lYDYo5AK82wOvSOS0UsLTpTG7uQ==", + "license": "MIT", + "dependencies": { + "@types/webidl-conversions": "*" + } + }, + "node_modules/bson": { + "version": "6.10.4", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.10.4.tgz", + "integrity": "sha512-WIsKqkSC0ABoBJuT1LEX+2HEvNmNKKgnTAyd0fL8qzK4SH2i9NXg+t08YtdZp/V9IZ33cxe3iV4yM0qg8lMQng==", + "license": "Apache-2.0", + "engines": { + "node": ">=16.20.1" + } + }, + "node_modules/csv-parser": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/csv-parser/-/csv-parser-3.2.0.tgz", + "integrity": "sha512-fgKbp+AJbn1h2dcAHKIdKNSSjfp43BZZykXsCjzALjKy80VXQNHPFJ6T9Afwdzoj24aMkq8GwDS7KGcDPpejrA==", + "license": "MIT", + "bin": { + "csv-parser": "bin/csv-parser" + }, + "engines": { + "node": ">= 10" + } + }, + "node_modules/dotenv": { + "version": "17.2.2", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.2.2.tgz", + "integrity": "sha512-Sf2LSQP+bOlhKWWyhFsn0UsfdK/kCWRv1iuA2gXAwt3dyNabr6QSj00I2V10pidqz69soatm9ZwZvpQMTIOd5Q==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, + "node_modules/memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "license": "MIT" + }, + "node_modules/mongodb": { + "version": "6.19.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.19.0.tgz", + "integrity": "sha512-H3GtYujOJdeKIMLKBT9PwlDhGrQfplABNF1G904w6r5ZXKWyv77aB0X9B+rhmaAwjtllHzaEkvi9mkGVZxs2Bw==", + "license": "Apache-2.0", + "dependencies": { + "@mongodb-js/saslprep": "^1.1.9", + "bson": "^6.10.4", + "mongodb-connection-string-url": "^3.0.0" + }, + "engines": { + "node": ">=16.20.1" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.188.0", + "@mongodb-js/zstd": "^1.1.0 || ^2.0.0", + "gcp-metadata": "^5.2.0", + "kerberos": "^2.0.1", + "mongodb-client-encryption": ">=6.0.0 <7", + "snappy": "^7.3.2", + "socks": "^2.7.1" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { + "optional": true + }, + "gcp-metadata": { + "optional": true + }, + "kerberos": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "snappy": { + "optional": true + }, + "socks": { + "optional": true + } + } + }, + "node_modules/mongodb-connection-string-url": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.2.tgz", + "integrity": "sha512-rMO7CGo/9BFwyZABcKAWL8UJwH/Kc2x0g72uhDWzG48URRax5TCIcJ7Rc3RZqffZzO/Gwff/jyKwCU9TN8gehA==", + "license": "Apache-2.0", + "dependencies": { + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^14.1.0 || ^13.0.0" + } + }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "license": "MIT", + "dependencies": { + "memory-pager": "^1.0.2" + } + }, + "node_modules/tr46": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-5.1.1.tgz", + "integrity": "sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==", + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + } + }, + "node_modules/whatwg-url": { + "version": "14.2.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-14.2.0.tgz", + "integrity": "sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==", + "license": "MIT", + "dependencies": { + "tr46": "^5.1.0", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=18" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 000000000..5822ae68d --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "csv-parser": "^3.2.0", + "dotenv": "^17.2.2", + "mongodb": "^6.19.0" + }, + "type": "module" +}