CRUD Application with Deno

Tuhin Das
3 min readMay 23, 2020

Creating CRUD application from scratch using Deno, REST API and MongoDB

This story will cover creating a CRUD application using Deno and MongoDB from scratch. Also, if you want to know about setting up Deno, please read the below story.

About the application
The application will have REST services for each of following operations :

  • Create user
  • Read users
  • Update user
  • Delete user

For each operation it will query into the database which is created in MongoDB.

Prerequisites

  • Deno should be installed and running.
  • Local or Remote MongoDB database with a collection like users.
{
"_id": {
"$oid": "5e68a5cdb25ebb404cfeef00"
},
"email": "...........",
"password": "...............",
"username": "........"
}

Want to read this story later? Save it in Journal.

Create Modules

Let’s start by creating modules for the application one by one.

db.services.ts will have all the methods to perform CRUD operations with database.

  • Import MongoClient module.
  • Connect to database using database URL and fetch the collection (users).
  • Create methods for all the CRUD operations similar to below method (getAllUsers).

rest.services.ts This module will have all the REST API services for CRUD operations.

  • Import Router module.
  • Import all the methods of db.services.ts.
  • Define all the REST API paths.
  • Create REST services for each CRUD operation and call the required database service method in it.

app.server.ts This module will create a server on localhost:127.0.0.1

  • Import Application module.
  • Import REST module.
  • Initialize Application and add REST routes in Application.
  • Define port(5000) on which you want to run your application server instance.

Start the application

Development part of the application has been completed. Now, it’s time to start the application.

  • Open the terminal/command prompt in the app.server.ts location.
  • Run the following command :
deno run --unstable --allow-read --allow-write --allow-plugin 
--allow-net app.server.ts
  • You should see the following message on your console which means your application is running.

Before moving to the next step, you must be wondering what are all those arguments used with run command. Let’s understand them first.

--unstable --allow-read --allow-write --allow-plugin 
--allow-net

unstable Deno is very new and a lot of libraries are still in their development phase and are unstable. ‘unstable’ flag is used to allow such modules/plugins which are declared unstable currently by Deno.

allow-read and allow-write are used to take permission for reading and writing files, in this case the database collection in MongoDB.

allow-plugin is used to take permission for using plugins, in this case the MongoDB plugin to connect with database.

allow-net is used to take permission for network related operations like server hosting.

Test the application

Finally, it’s time to test the application. Firstly, Make sure your local or remote MongoDB instance is up and running. Now, open the following URL in any web browser and where you should be able to see all users from the collection.

http://localhost:5000/app/users

You can try to test the remaining REST APIs using Postman or some other tool of your preference. You can also download the project from github deno-crud-app.

Facing any issue or stuck somewhere? Do let me know in the response section.

📝 Save this story in Journal.

👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.

--

--

Tuhin Das

Front End Engineer (L5) @ Amazon | Solving Customer Problems In E-Commerce Domain