Unlock the Power of Deno: Building a RESTful API with Oak and MongoDB

What is Deno?

Deno is a modern, secure runtime for JavaScript and TypeScript, built on top of V8 and Rust. It supports TypeScript and vanilla JS out of the box, and uses third-party packages with browser-compatible URLs to manage modules. By default, Deno is secure, meaning there is no file, network, or environment access unless explicitly enabled.

Getting Started with Oak and MongoDB

Before we dive into building our RESTful API, let’s take a quick look at Oak and MongoDB. Oak is a middleware framework for Deno’s http server, inspired by Koa. It’s the most popular choice for building web applications with Deno. MongoDB, on the other hand, is a cross-platform, document-oriented database program used by developers to handle application data.

Building a Simple REST API with CRUD Operations

Our objective is to create a REST API that allows us to perform basic CRUD operations. To get started, we’ll need:

  • Understanding of JavaScript/TypeScript
  • Deno installed on your machine
  • A text editor of your choice
  • Postman for API testing

Creating a Deno Server with Oak

First, we’ll create a file called server.ts and import our Router from Oak. We’ll then create our route middleware functions using Oak’s middleware framework.

“`javascript
import { Application, Router } from ‘https://deno.land/x/oak/mod.ts’;
const app = new Application();
const router = new Router();

router.get(‘/’, (ctx) => {
ctx.response.body = ‘Hello from Deno!’;
});

app.use(router.routes());
app.listen({ port: 8000 });
“`

Adding MongoDB to the Mix

Next, we’ll create a mongodb.ts file and add the following lines of code:

“`javascript
import { MongoClient } from ‘https://deno.land/x/[email protected]/mod.ts’;

const client = new MongoClient(‘mongodb://localhost:27017’);
const db = client.database();
const roomsCollection = db.collection(‘rooms’);
“`

Performing CRUD Operations

We’re now ready to try performing basic CRUD operations that will allow us to create, read, update, and delete data in our MongoDB database using Deno and Oak.

Reading Data

First, we’ll create a file called routes.ts and add the following lines of code:

javascript
router.get('/rooms', async (ctx) => {
const rooms = await roomsCollection.find().toArray();
ctx.response.body = rooms;
});

Creating Data

Next, we’ll set ourselves up to add data to our database:

javascript
router.post('/rooms', async (ctx) => {
const { room_number, size, price, isAvailable } = ctx.request.body;
const result = await roomsCollection.insertOne({ room_number, size, price, isAvailable });
ctx.response.status = 201;
ctx.response.body = result;
});

Getting a Single Document

We’ll retrieve a single room by its id:

javascript
router.get('/rooms/:id', async (ctx) => {
const id = ctx.params.id;
const room = await roomsCollection.findOne({ _id: new ObjectId(id) });
ctx.response.body = room;
});

Updating Existing Data

We’ll update an existing document:

javascript
router.put('/rooms/:id', async (ctx) => {
const id = ctx.params.id;
const { room_number, size, price, isAvailable } = ctx.request.body;
await roomsCollection.updateOne({ _id: new ObjectId(id) }, { $set: { room_number, size, price, isAvailable } });
ctx.response.status = 200;
});

Deleting Data

Finally, we’ll delete data from our MongoDB collection:

javascript
router.delete('/rooms/:id', async (ctx) => {
const id = ctx.params.id;
await roomsCollection.deleteOne({ _id: new ObjectId(id) });
ctx.response.status = 200;
});

That’s It!

We’ve successfully implemented a RESTful API by building a simple CRUD app using the Deno framework, Oak, and MongoDB. This is just the tip of the iceberg when it comes to what can be achieved with Deno runtime and Oak framework. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *