Skip to content

Schema

Course: PayTM project - chapter 9

Update the db.js file to add one new schemas and export the respective models

The Accounts table will store the INR balances of a user.

The schema should look something like this -

{
userId: ObjectId (or string),
balance: float/number
}
In the real world, you shouldn’t store `floats` for balances in the database.
You usually store an integer which represents the INR value with
decimal places (for eg, if someone has 33.33 rs in their account,
you store 3333 in the database).
There is a certain precision that you need to support (which for india is
2/4 decimal places) and this allows you to get rid of precision
errors by storing integers in your DB

You should reference the users table in the schema (Hint - https://medium.com/@mendes.develop/joining-tables-in-mongodb-with-mongoose-489d72c84b60)

Solution
const accountSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId, // Reference to User model
ref: 'User',
required: true
},
balance: {
type: Number,
required: true
}
});
const Account = mongoose.model('Account', accountSchema);
module.exports = {
Account
}

By the end of it, `db.js` should look lie this
$1b