Create Client and Product Schema - NodeJS and MongoDB

Create Client and Product Schema - NodeJS and MongoDB

Hello, people of Hive, in this guide we will see how to keep updating our NodeJS so we can interact with our MongoDB in a better way, in this case, I will use 2 common schemas that we can relate to all kinds of projects, also we are making the create function and the endpoints file.

landpost.png

We will start with the models, we have a Client.js file, where we import mongoose, the same as we did in the last guide in the server.js, and we declare a const schema, we create a new schema with the name ClientSchema inside we declare with key-valor logic same as JSON, we can see here name and other attributes, and we have the value right here we don't put a value we declare the type, so we later can fill that value with that data type.

guide1.png

We make the same for Product.js this is like creating a Table in SQL if you think that way, we use the same logic, and we export a module with a mongoose.model and with the product schema we created.

guide2.png

Now we will create the controller for the product, here we import the model we just created, and we create a new function called createProduct, this will require the attributes/variables we declared before in the model, with the syntax you see here, we also use product.save to save in the database.

guide3.png

We make the same for the Client controller, we can see in the client.save that we are using JSON as the format to send the data, we can also see that we need to export all the functions like createClient with module.exports

guide4.png

We will end creating a file called Endpoints.js where we will have the routers, right now we will create just 2 with the functions we made for creating product and client, we import both controllers, and we create router.post in this case to call both functions as we can see here:

guide5.png

In the next posts, we will keep expanding the functions and we will start interacting with the Database right now we already have created both models and controllers for the product and client, you can also change anything about the code this is kind of generic so you can easily change it if you need to use MongoDB or NodeJS.

You can check my blog here: https://peakd.com/@taradraz1

Thanks for the time!!!



0
0
0.000
4 comments
avatar

Congratulations!


You have obtained a vote from CHESS BROTHERS PROJECT

✅ Good job. Your post has been appreciated and has received support from CHESS BROTHERS ♔ 💪


♟ We invite you to use our hashtag #chessbrothers and learn more about us.

♟♟ You can also reach us on our Discord server and promote your posts there.

♟♟♟ Consider joining our curation trail so we work as a team and you get rewards automatically.

♞♟ Check out our @chessbrotherspro account to learn about the curation process carried out daily by our team.


🥇 If you want to earn profits with your HP delegation and support our project, we invite you to join the Master Investor plan. Here you can learn how to do it.


Kindly

The CHESS BROTHERS team

0
0
0.000
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support. 
 

0
0
0.000
avatar

great article, i usually doesnt like use mongo or nosql databases but it is good know how use it :D

0
0
0.000
avatar

Congratulations @taradraz1! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You distributed more than 2000 upvotes.
Your next target is to reach 3000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

LEO Power Up Day - December 15, 2023
0
0
0.000