Backend: Data Modelling

Written by sanju

Updated on:

Welcome back.

WhatsApp Group Join Now
Telegram Group Join Now
Instagram Group Join Now

In this video, let’s go ahead to model our data for our application.

For this particular application, I’m going to have three entities.

The first one is going to be the users, and then the next entity is going to be the account and account.

And then lastly, it’s going to be the transaction.

So how are you going to relate these modules together?

A user can create many accounts and one account can have many transactions.

All right.

So let’s see how we’re going to model this.

So back to the file that is inside the model.

Let’s go ahead and create three files for that.

The first one is going to be the user DOT JS, and then the next one is going to be the transaction.

As that, you can see how I’m naming the model because model are like a class of which we can create

instance of object out of this.

So by convention you use uppercase for model.

Then this one going to be the account.

So third JS.

So let’s start with the user.

Great.

So let’s bring in the mongoose package.

And next is let’s create the schema for that going to be as user schema.

So then this property is going to be as has created account and for this it’s going to be a boolean

type of boolean.

We are going to use this field to check if a user has created an account or a project or for you or

she want to keep track about the expenses and income.

So as soon as a user creates an account or a project, we are going to change this one to.

True.

So by default we say is.

Force.

Great.

So next step is that a user can have many accounts.

You’re going to have an account filled on this user, and for this you’re going to use referencing.

So here as an array with objects and the type here, you’re going to be a special type as mongoose dot

schema dot types, dirt object ID.

This field is not a required field, but instead, as soon as a user creates an account, you’re are

going to push the account created into the field or this account.

So here we need to reference the model and for this you’re going to reference the account model.

We will create it very soon and that is it for that.

So let’s pass in some additional configuration for this.

Let’s provide timestamp and this one to true for this one, as soon as a user is registered, you’re

going to have the date that a user registered.

So here you also have additional properties like to Jason.

And for this one, if you want to convert virtual properties on the object, we will use this one.

So here virtuous to true.

The next step is that let’s compile this schema to form a model.

So here we go.

Const uppercase user is equal to mongoose dot model and here we pass in the value here user and then

the schema as user schema.

This name is going to be the collection name inside our database and we’re going to utilize it by adding

X and then change everything to lowercase.

So by convention, this is how we passed in last step is that let’s go ahead and then export this one

as module dot export.

And it’s equal to the user model.

So we are done with the user model.

The next step is going to be the account creation.

It’s going to be the same step.

So let’s copy this one here and paste it here and let’s change this one to account schema.

So select that and Press Command D for multiple selection.

So here are going to be as account schema.

Great.

And for the accounts, Kima, let’s look at the properties that we need for the account.

When creating an account, we need to provide the name of the account and it’s a required field.

And then this property is going to be the account type for this one.

You need to specify the type of an account, whether is a savings account or investment or billing or

school or project or utilities or travel.

So let’s see how we’re going to do the one for this.

We are going to use a special property here called enumeration.

The type is string, but here I’m going to pass in the accepted value call.

Now we’re going to be as that and going to be an array of the type of account we want to create.

So here, let’s finish up by using account type.

So here I’m going to paste the account type here and better to provide the final source code for you.

So this is account type.

I want a user to create perfect and this one must be required field to true that is it.

So let’s collapse this one to have some enough room to do the rest.

Then this property is going to be as initial balance.

So it means that when creating an account, you can specify how much money you want to start with.

So here you’re going to be an optional.

So here are going to be as balance and a type.

Here is a number and by default let’s give it zero default value here zero.

And this one is not a required field.

Then this one is going to be the transaction.

Remember, an account can have many transactions, whether it’s income or expenses.

So let’s go ahead and create that one.

So let’s remove this one.

And now this is going to be the transaction.

And for the transactions, we’re going to keep the IDs of the transaction, but not the entire object.

So here we’re going to have the same type by the reference, we’re going to reference the transaction

model.

And for this one too, we are going to create it very soon.

The next step is that we want to know who created this particular account.

So here, let’s collapse this one and we can add a user or create it by this one has more meaning.

So here we are going to use the same type as you use for the transaction and account and that is mongoose

dot schema, dot types, dot object ID and you won’t reference the user.

And you won’t reference the user model.

Great.

After that, you want to add some notes about the transaction that is description about the transaction

and the type here is going to be of string and then is a required field to true.

All right.

So for this one, too, I think we can maintain this one.

And lastly, let’s change this one to account.

So multiple selection and change this one to account, and that is it.

The last step is going to be the transaction so we can copy the account and then paste it here for the

transaction.

So for the transaction, let’s see the model for that tool.

So let’s change this one to transaction schema and it goes like this transaction.

Come on.

Perfect.

So let’s look at the properties here.

We need a name and then the transaction type.

So here are going to be as transaction

type.

And when I be a string, the same thing is going to be an enumeration of values.

So here for the transaction type, you want to keep track of the income and then expenses.

Perfect.

Then this one going to be the amount for the transaction and a type is a number and going to be a required

field.

Great.

Then this, I’m going to be the category of the transaction.

So here we are going to copy this one.

And now down here is going to be the category of the transaction.

And for this one, so we are going to use the enum and for the values.

I’m going to copy and paste the values for the category of the transaction.

And here we go.

Boom.

And this one too is a required field.

The next type is going to be a color because we need that one to add some beauty to the transaction.

Let’s say that if we want to use green for income or red for expenses, the choice is yours.

That is why I want to add a field called color.

So here is going to be the color field here and then the type is going to be of string.

And for this one we’re going to be optional.

Then this property is going to be the created PI because you want to know who created this transaction.

So let’s paste that one here.

Let’s remove the transaction here.

The next I’m going to be the date recorded or date for short.

Let’s make it date.

Yeah.

So here is going to be a type of date.

And it’s going to be optional.

It’s optional.

Are we going to have a default value as debt default now as it does now?

Great.

So that is it for that and for this one too.

We need the nodes and we need the timestamp.

And then, Jason, the last step is let’s go ahead and then compile to for model.

So a multiple selection here is going to be transaction.

So in case we have left some properties behind, we can come back and then fix it.

Related Post

Connect To MongoDB

Express Server

Leave a Comment