-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: main
Are you sure you want to change the base?
Conversation
…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.
There was a problem hiding this 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!
const database = client.db('dataBaseWeek4'); | ||
const accounts = database.collection('accounts'); |
There was a problem hiding this comment.
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.
const database = client.db('dataBaseWeek4'); | ||
const accounts = database.collection('accounts'); |
There was a problem hiding this comment.
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.
// 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'); | ||
} |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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?
No description provided.