feregate.blogg.se

Todolist app using mongodb
Todolist app using mongodb








todolist app using mongodb

You can save the file and it is ready to go. You can download the component from the GitHub. package models import "go./mongo-driver/bson/primitive" type ToDoList struct export default App Ĭreate a new file To-Do-List.js inside the src folder. Once both the packages installed successfully, create a models directory and models.go file inside it and paste the below code. To install it run the below command in the terminal or command window. mux is one of the most popular packages for the router in the Golang. Second, install the gorilla/mux package for the router. The instances created from the model constructor are documents which will be persisted by Mongo.

todolist app using mongodb

That's because when a model is compiled, the result is a constructor function that is used to create instances of the model. Notice that the Todo model is capitalized. To install it run the below command in the terminal or command window. MongoDB will automatically generate an id for each Todo that we create. We are going to use the official MongoDB Go Driver from MongoDB. In the server, we require 2 dependencies: the first to connect with MongoDB and the second to create RESTAPIs. The server directory structure will be: go-todo - server - middleware - middleware.go - models - models.go - router - router.go - main.go Server in GolangĬreate a server directory inside the go-todo. Note: This project is developed outside the GOPATH due to which the local imports are working.Ĭreate a project directory and give it an appropriate name. If you don’t, I’ll explain all the steps and will mention the references of the related topics. We also have made changes to our UI by mapping through todos array and rendering each Todo and changing CSS classes dynamically when a Todo is done.I am assuming that you have Go installed and have a basic understanding of it. The component will reflect all these changes because we also call the forceUpdate function - if any change gets caught, the component will re-render itself.įinally, we are using the onDestroy lifecycle method to close the connection to the database when a component instance gets destroyed. on delete a specific Todo from todos array will be deleted.on update a specific Todo from todos array will be updated.on insert a Todo will be pushed to todos array.Then we added real-time functionality by watching for changes to database todoListCollection.watch(): First, we have added a connection to our database (don’t forget to change YOURMONGODBCONNECTIONSTRING to your actual connection string). Then we made the initial fetch of our Todo list by converting it to an array - let todos = await todoListCollection.find().toArray(). First, we have added a connection to our database (don’t forget to change YOUR_MONGODB_CONNECTION_STRING to your actual connection string).










Todolist app using mongodb