Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elias Week 4 database #54

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

EliasRajha
Copy link

No description provided.

…nstead of nested callbacks. This makes the code easier to read and maintain.

I added a foreign key constraint for the invited_by field in the Invitee table. This ensures that each person invited actually exists in the table, improving data integrity.
I updated how we manage the database connection to use promises, which makes it easier to handle errors that may occur.
I also improved the comments in the code to make it clearer how we set up the database and create the tables.
@yunchen4 yunchen4 self-assigned this Nov 8, 2024
Copy link

@yunchen4 yunchen4 left a comment

Choose a reason for hiding this comment

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

Hello Elias,
Looks good to me! I only give some suggestions which are more related to real working situation.
If you have any questions don't hesitate to ping me on slack. Have a good weekend!

Comment on lines +11 to +12
const database = client.db('dataBaseWeek4');
const accounts = database.collection('accounts');
Copy link

Choose a reason for hiding this comment

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

suggestion: these two strings are used across files. Better to make them constants.

Comment on lines +13 to +14
const database = client.db('dataBaseWeek4');
const accounts = database.collection('accounts');
Copy link

Choose a reason for hiding this comment

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

Constant suggestion applies here too.

Comment on lines +18 to +28
// Find the sender account
const senderAccount = await accounts.findOne({ account_number: fromAccountNumber });
if (!senderAccount || senderAccount.balance < amount) {
throw new Error('Insufficient funds');
}

// Find the receiver account
const receiverAccount = await accounts.findOne({ account_number: toAccountNumber });
if (!receiverAccount) {
throw new Error('Receiver account not found');
}
Copy link

Choose a reason for hiding this comment

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

Early returns or throws are always encouraged!

$inc: { balance: -amount },
$push: {
account_changes: {
change_number: senderAccount.account_changes.length + 1,
Copy link

Choose a reason for hiding this comment

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

suggestion: using fromAccount.account_changes.length + 1 to know the next change_number is OK.
However, in real life one account may have a lot more change records. We don't really care what those changes are, we just want to know the maximum change number. Fetching all changes with the account can be expensive (since you need to transfer more bytes in the network. When you download something big from Internet, it is always slower than dowloading smaller files), and we try to only fetch the columns we need.
One solution is to have a function to get only the maximum change number (you have learned MAX sql syntax), and when you fetch the account data you don't fetch the changes column.

try {
await client.connect();
const db = client.db(dbName);
const collection = db.collection('populationData');
Copy link

Choose a reason for hiding this comment

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

suggestion: why not make populationData constant too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants