Serverless architecture is a pattern of running and building applications and services without having to manage infrastructure. It involves the applications and services running on the server, but all the server management is done by a cloud provider.
This post will discuss building a fullstack user management application using MongoDB, MongoDB Realm, and React.js. At the end of this tutorial, we will learn how to create a database on MongoDB, serverless functions as our endpoints using MongoDB Realm and consume the endpoints in a React.js application.
MongoDB Realm is a development platform designed for building mobile, web, desktop, and IoT applications. It offers services like data synchronization, serverless functions, triggers, user authentication, e.t.c. We can build and maintain application on MongoDB Realm using any of the following:
- Realm UI: a browser-based option to create and maintain application
- Realm CLI: a CLI-based option to define and deploy applications
- GitHub Deploy: use configuration files on Github to deploy applications from a Github repository
- Admin API: an HTTP-based request to manage your applications.
In this post, we will be using Realm UI to build our applications.
You can code along by cloning this repository (main branch) here. If you prefer to view the complete code, checkout to the dev branch of this same repository.
In this tutorial, we will focus on implementations only. The project UI has already been set up with TailwindCSS.
You can check out the Vue.js version here.
Prerequisites
The following steps in this post require JavaScript and React.js experience. Experience with TypeScript isn’t a requirement, but it’s nice to have.
We also need a MongoDB account to host database the and create serverless functions. Signup is completely free.
Let’s code
Running the Project
To get started, we need to navigate to the project location, open our terminal and install project dependency as shown below:
npm install
With that done, we can start a development server using the command below:
npm start
Setting up MongoDB
To get started, we need to log in or sign up into our MongoDB account and follow the option that applies to us:
For a New Account (Sign Up)
First, we need to answer a few questions to help MongoDB help set up our account. Then click on Finish.
Select Shared as the type of database.
Click on Create to setup a cluster. This might take sometime to setup.
Next, we need to create a user to access the database externally by inputting the Username, Password and then click on Create User. We also need to add our IP address to safely connect to the database by clicking on the Add My Current IP Address button. Then click on Finish and Close to save changes.
On saving the changes, we should see a Database Deployments screen, as shown below:
For an Existing Account (Log In)
Click the project dropdown menu and click on the New Project button.
Enter the reactRealm as the project name, click on Next and then click Create Project
Click on Build a Database
Select Shared as the type of database.
Click on Create to setup a cluster. This might take sometime to setup.
Next, we need to create a user to access the database externally by inputting the Username, Password and then clicking on Create User. We also need to add our IP address to safely connect to the database by clicking on the Add My Current IP Address button. Then click on Finish and Close to save changes.
On saving the changes, we should see a Database Deployments screen, as shown below:
Loading Sample Data
Next, we need to populate our database with users' sample data. To do this, click on the Browse Collections button
Click on Add My Own Data, input reactRealmDB and reactRealmCol as the database and collection name, and click on Create.
Next, we need to insert these sample data:
{
"name": "daniel mark"
"location": "new york"
"title": "software engineer"
}
{
"name": "clara patrick"
"location": "lisbon"
"title": "data engineer"
}
To do this, click on the Insert Document button, fill in the details above and click on Insert to save.
Creating and configuring MongoDB Realm application
With our database populated, we need to create serverless functions to perform Create, Read, Update and Delete (CRUD) on our database. To do this, select the Realm tab, click on Build your own App. Then click on Create Realm Application to setup our application.
MongoDB Realm also ships with templates that we can use to build our application quickly. For this tutorial, we will be building from scratch.
Next, we need to setup permission and rules for our functions. To do this, close the popup guide click on Rules, select the reactRealmCol and click on Configure Collection.
MongoDB Realm’s Save and Deploy
With that done, MongoDB Realm will show us a widget illustrating the concept of Save and Deploy.
When writing a serverless function, clicking on Save creates a development draft that we can test and play around with. At the same time, Deploy makes our changes public to be consumed by another application(React.js in our case).
Click on Next and then Got it to continue.
Next, we need to allow Read and Write permissions for our function and then Save.
Next, navigate to the Authentication tab, click on Allow users to log in anonymously, toggle it on and Save Draft.
MongoDB Realm also ships with several authentication options that we can explore. For this tutorial, we will be using the anonymous option.
Creating serverless functions on MongoDB Realm
Get All Users Serverless Function
With the configuration done, we can now create a serverless function that returns list of users. To do this, navigate to the Functions tab, click on Create New Function, and input getAllUsers as the function name
Next, select the Function Editor tab and modify the function to the following:
exports = function(arg){
let collection = context.services.get("mongodb-atlas").db("reactRealmDB").collection("reactRealmCol");
return collection.find({});
};
The snippet above does the following:
- Create a collection variable to access the
reactRealmDBdatabase andreactRealmColcollection - Return the list of documents in the collection.
Next, we can test our function by clicking on Run button to see list of users.
Finally, we need to copy any returned user’s _id and save it somewhere; we need it for the next function. Then click on Save Draft to create a deployment draft for our function.
Get A User Serverless Function
To do this, click on the Functions tab, click on Create New Function, and input getSingleUser as the function name
Next, select the Function Editor tab and modify the function to the following:
exports = function(arg){
let collection = context.services.get("mongodb-atlas").db("reactRealmDB").collection("reactRealmCol");
return collection.findOne({_id: BSON.ObjectId(arg)});
};
The snippet above does the following:
- Create a collection variable to access the
reactRealmDBdatabase andreactRealmColcollection - Return a single user by finding it by its
_id. Because MongoDB saves documents in BSON, we need to parse theargas BSON using theBSON.ObjectId.
To test our function, Navigate to the Console tab, replace the Hello world! in the exports function with the user’s _id we copied earlier and then click on Run.
Finally, we need to save our function by clicking on the Save Draft button.
Edit A User Serverless Function
To do this, we need to follow the same steps as above.
First, click on the Functions tab, click on Create New Function, and input editUser as the function name.
Next, select the Function Editor tab and modify the function to the following:
exports = function(id, name, location, title){
let collection = context.services.get(
































