Now we have to create the main function that implements the logic of the database module. The timestamps option decorates each document with two additional fields: createdAt and updatedAt both of type Date.Īfter defining our schema, we will use it to instantiate our model called Todo. The schema defines a todo like a document that has a field text (always required) and a field isCompleted that will represent the status of our todos (completed or not). database.jsĬonst Todo = mongoose.model("Todo", todoSchema) We have to create the Schema for our data collection and the Model to manipulate the documents of our collection. We will rely on mongoose to do all the operations. The first brick of our solution is the module to connect and query MongoDB. It is easy to use and its functionality can be extended with add-ons.There are add-ons for various things, including messaging/queues, logging, metrics, and of course, data stores. Connecting and querying MongoDB: database.js Heroku is a hosting service that supports Node.js applications.
Git initializationĬlick the Source Control tab in the VS Code side barĬlick Initialize Repository button and follow the stepsĪdd a. Let's create a folder test where we keep our test files. "test": "clear tap -reporter=spec -watch" Otherwise click the New button in the top-right corner and select Create new app: Since you only need the app to get access to your database, the App name doesn't. If your dashboard is currently empty, you can click the Create new app button. "test": "echo \"Error: no test specified\" & exit 1" Once you see your personal app dashboard, you can create a new app. Open package.json and replace // package.json Mongodb-memory-server and tap are the two libraries to write tests.
Next we need to install the dependencies $ npm install dotenv fastify mongoose & npm install mongodb-memory-server tap -save-dev We need to initialize the solution running $ npm initĪnd when init will ask for the entry point we insert server.js. In this post we will use Fastify and MongoDB to create a simple REST API to manage todos that will be deployed on Heroku. Let's open the folder with VS Code and open a terminal. Tested with Ubuntu Desktop 18.04.5 LTS and Node.js v14.16.0 LTS. Solution initializationĭuring the setup of MongoDB we created the folder todos-api and put inside it the.
Go to the signup page and follow the steps.
If we use special characters in our password we have to be sure they are encoded. Type MONGODB_URI="" and paste between the quotes the link #, and with the username, password and database name we chose. On our computers, let's create a folder called todos-api (it is the folder where we will develop our solution) Once Atlas has finished the provisioning, we have to click on CONNECT and then Connect your application. We need to create a MongoDB account and then set up a cluster by using this guide. We will use a fully managed instance of MongoDB relying on Atlas. Create a MongoDB Atlas account and setup a cluster
Tested with Ubuntu Desktop 18.04.5 LTS and Node.js v14.16.0 LTS Fastify and MongoDB on Heroku April 18, 2021