Skip to content

Conversation

@aleksandrstarshynov
Copy link

No description provided.

Copy link

@RHSebregts RHSebregts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Oleksandr,

Good effort, but I feel like it could use another round of polish. What I like to do, to make sure I stay fresh an don't forget things, is to make the PR, and then read through it again on the Github website. The different formatting makes me notice different things and refreshes my senses a bit.

Good luck!

Best wishes,

Robbert-Jan

require("dotenv").config();

async function main() {
const client = new MongoClient(process.env.MONGODB_URL);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit dangerous, what if the value doesn't exist?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I have never though about this from suth corner. I don't usually do that way as here. Production will definitely have some kind of env file. Hoeever, I develop and test locally. That's why I often just put const url = process.env.MONGODB_URL || "mongodb://localhost:27017/mydb"
But that will only solve the local problem.
I'll try to write secure code so that it works not only locally.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const url = process.env.MONGODB_URL || "mongodb://localhost:27017/mydb" would be fine!
What I like to do is to create a config file which exports a config object. That file will then handle all the checks on the env as well and has the defaults. That way in your code you could just require config and use config.dbConnectionString or something.

await client.connect();
const db = client.db("databaseWeek4");
const collection = db.collection("aggregation");
// taks 1. I have uploaded data via Atlas

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but I'd like to see what csv magic you performed to get it into the db.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No magic here.
First I have found noi-line service to convert JSON-CSV. Than, since I am using Atlas, I have used its web-interface. That open needed Collection, and manually add file, click "Import" button. Finally, I just checked visually that all data is in correct structure.

},
]);

console.log("\n📊 Task 2 Result:\n", await findPopulation.toArray());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't make a habit of using function calls in your console logs. And especially not asynchronous ones.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. I thought it was a good practice. If we need to do something just to output it to the console, then the code should be stored in that place and not in the middle of the rest of the working code. I'll change it.

const result = await collection.aggregate([
{
$match: {
Country: { $regex: /^[A-Z ]+$/ },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this regex based on the data structure, smart!

Comment on lines 5 to 6
const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the database connection reusable here and in transfer.js.

const { MongoClient } = require("mongodb");
require("dotenv").config();

const client = new MongoClient(process.env.MONGODB_URL);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to fail, please tell me why!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was lucky. Since the condition exactly matched and the env was in the right place and contained the correct credentials - everything worked. I'll redo this place

const client = new MongoClient(process.env.MONGODB_URL);

async function transferMoney(fromAcc, toAcc, amount, remark) {
const client = new MongoClient(process.env.MONGODB_URL);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we doing this for the second time in this file?

Comment on lines +18 to +19
if (!fromDoc || !toDoc) throw new Error("Account not found.");
if (fromDoc.balance < amount) throw new Error("Insufficient funds.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the grouping of the database calls in principle, but if we move these checks up we could save ourselves an unnecessary call.

Comment on lines +26 to +27
const lastFromChange = fromDoc.account_changes.at(-1)?.change_number || 0;
const lastToChange = toDoc.account_changes.at(-1)?.change_number || 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Comment on lines +22 to +23
const newFromBalance = fromDoc.balance - amount;
const newToBalance = toDoc.balance + amount;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you do this here, and I would love to see you also create the object you give to $push here. That way we can see what you're planning to do, and in the db query itself we can just focus on the db language, and not on what you are putting in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants