Creating a CRUD API in Deno with Oak and MongoDB

Subhra Paladhi
JavaScript in Plain English
4 min readMay 16, 2020

--

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.

  • Oak: 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.
  • deno_mongo: 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.

./app.ts

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

./helpers/dbconnect.ts

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

We have 4 controllers for this project.

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

1. ./Controllers/addFriend.ts

Route- POST:localhost:3000/addFriend

Sample input: Request body, content-type:json

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

Sample output: The mongoID is returned.

{"$oid": "5ec0a99c003430240011550b"}

2. ./Controllers/getFriend.ts

Route- GET: localhost:3000/getFriend/5ec0a99c0034302411550b

Sample output: The friend data is returned

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

3. ./Controllers/updateFriend.ts

Route- PATCH: localhost:3000/updateFriend/5ec0a99c0034302411550b

Sample input: Request body, content-type:json

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

Sample output: The following 3 fields are returned.

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

4. ./Controllers/deleteFriend.ts

Route- DELETE: localhost:3000/deleteFriend/5ec0a99c0034302411550b

Sample output: “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.ts

- — 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:

GitHub link for this project

  1. Friend list with Deno

Videos

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

Docs

  1. Deno official website
  2. oak
  3. deno_mongo

Blogs

  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 plainenglish.io — show some love by giving our publications a follow and subscribing to our YouTube channel!

--

--