seamlessict / darey

0 stars 0 forks source link

Creating the Backend - Models and mongodb Database #16

Closed seamlessict closed 1 year ago

seamlessict commented 1 year ago

Models are used to define the database schema of NON-SQL databases. but before we create a model we need to install mongoose using the npm packages npm install mongoose
the next step is to create a directory and a file that we can use in creating our model using the following command: mkdir models && cd models && touch todo.js and writing the model schema.

We modified the api.js route file we created earlier to sync with the model we just created.

goto mongodb website and create an account after which you click on deploy database and create a new database and create credential all of which is used to create a connection string that will be used by your application for connecting to the database.

Create a ,.env file using the touch command and update your index.js file. Note that the connection string is kept in this file and the advantage of keeping it here is to have global access to the connection string and if it needs to be changed it will only be effected in one file.

run your application by typing node index.js to check if the database is connected to successfully. Now we can check the request sent by our application using a software called postman

Image Image Image

Testing the GET request on a browser just for fun you can see that it returns the database content in JSON format

Image