Creating a CRUD API in Deno with Oak and MongoDB

Subhra Paladhi
May 16 · 4 min read

Deno is a simple, modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.

On 13th May 2020, the v1.0 of Deno was officially launched. Deno has been developed by Ryan Dahl who also had a major role in the development of Node.js. Take a look at this video from a few months back in which Ryan Dahl and Kitson Kelly talk about the idea behind the creation of Deno.

Deno is a New Way to JavaScript — Ryan Dahl & Kitson Kelly

Here are some of the basic points of similarity and difference between Deno and Node.js.

  • Deno is built with Rust in the core, unlike Node.js which uses C++.
  • Moreover, Deno supports typeScript out of the box. They don’t need to be compiled to javascript files before using like in Node.js.
  • Both Deno and Node.js uses the V8 javascript runtime.
  • Deno has no file, network, or environment access unless explicitly enabled. Whereas Node.js has access to the local files and network without any extra permissions required which is a security concern.
  • Deno doesn't use node modules and npm. Packages as directly imported from the URL and cached when the program is run the first time.
DENO

Installation

Using Shell (Linux):

curl -fsSL https://deno.land/x/install/install.sh | sh

Using Homebrew (macOS):

brew install deno

Using Chocolatey (Windows):

choco install deno

In this blog, I will create a server application in Deno using the oak and deno_mongo libraries.

  • It is a middleware framework for Deno’s Http server, including a router middleware. This middleware framework is inspired by Koa and middleware router inspired by koa-router used with Node.js.
  • : It is a MongoDB database driver developed for Deno, based on Rust’s MongoDB library package.

So, let’s get started with the coding part. This mini project is a simple server app to create a list of our friends. We will be able to add, fetch, update and delete data about our friends in the MongoDB database.

Here is what the structure of our directory looks like

Directory structure

This is the app.ts file. This is the file that we will use to start the server and declare the routes.

Then create the helper folder in which we will create dbconnect.ts which will be used to connect to the MongoDB database.

Now we will move to the main part. Creating the controllers for the routes.

We have 4 controllers for this project.

  1. (POST)adding a friend to the database. (insert operation)
  2. (GET): Get the friend data for the specified id. (find operation)
  3. (PATCH): to update the email and pno of a friend for the given id. (update operation)
  4. (DELETE): to delete a friend using the id. (delete operation)

1.

localhost:3000/addFriend

: Request body, content-type:json

{
“name”: “Subhra”,
“pno”: “9003814273”,
“email”: “subhrapaladhi9@gmail.com
}

: The mongoID is returned.

{"$oid": "5ec0a99c003430240011550b"}

2.

localhost:3000/getFriend/5ec0a99c0034302411550b

: The friend data is returned

{
"_id": {
"$oid": "5ec0a99c003430240011550b"
},
"name": "Subhra",
"pno": "9003814273",
"email": "subhrapaladhi9@gmail.com"
}

localhost:3000/updateFriend/5ec0a99c0034302411550b

: Request body, content-type:json

{
"pno": "9458059340",
"email": "subhrapaladhi9@outlook.com"
}

: The following 3 fields are returned.

{
"matchedCount": 1,
"modifiedCount": 1,
"upsertedId": null
}

.

localhost:3000/deleteFriend/5ec0a99c0034302411550b

: “result” is 1 when the deletion is successful else it is 0.

{
"result": 1
}

Now let's run our project. Open terminal/command prompt in the root directory of the project and paste this command.

deno run --allow-net --allow-write --allow-read --allow-plugin --unstable app.tst

- — allow-write --allow-read --allow-plugin --unstable — These are the flags required to give permission to Deno to access the network and other resources.

When you will run the command the first time it will download the dependencies and cache them locally. A ./.deno_plugins folder will also be created. You can add it to .gitignore file before committing your code.

A minor update of Deno has just been released(20 May 2020). It mostly has some bug fixes. Run the following command to upgrade:-

deno upgrade --version 1.0.1

Hope this will help you to get started with Deno. I am attaching links to resources from where you can learn more about Deno.

References and resources:

  1. Friend list with Deno

  1. Deno video crash course
  2. 10 Things I Regret About Node.js — Ryan Dahl — JSConf EU

  1. Deno official website
  2. oak
  3. deno_mongo

  1. Create a server with Deno and Mongo(using abc framework)
  2. Creating your first REST API with Deno and Postgres

A note from the Plain English team

Did you know that we have four publications and a YouTube channel? You can find all of this from our homepage at — show some love by giving our publications a follow and

JavaScript In Plain English

New articles every day.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store