This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Object Oriented Express API – DEV Community


I have been toying round with the thought of getting a scalable specific api service, which is able to deal with a few of the primary crud operations for a mannequin with out writing the precise code for the controller. I’ve three years of expertise and I used to be unsure when you may one thing like that however then one thing occurred.

I began to work on Ruby on Rails final 12 months and my perspective has been modified quite a bit since then, engaged on completely different tech stacks, with completely different individuals actually opens you up. Somebody had written a generic handler for all of the fashions and all the essential operations of CRUD had been dealt with with out writing a bit of code for the brand new controller. It was then I realised the ability of Object Oriented Programming.

There are a numerous language particular perks of writing code in Ruby, however Object Orientation is a basic idea which will be very helpful, so I made a decision to make use of it in Node and Categorical and make my first worthwhile contribution to the Open Supply Neighborhood. I select Node and Categorical as a result of I’ve labored on it earlier than and wished to see the distinction within the high quality of code, In different phrases, I wished to see how my abilities have improved through the years.

I began the mission on a Saturday afternoon, I used to be nearly performed on Sunday. In the course of the mission, I came upon some essential issues:

  • In Categorical 5, which is at present a beta model, you do not have to throw the error explicitly, It throws the error itself and name the following() with the error, so no extra if this then that within the handlers.
  • You should utilize an entire javascript class as a route handler. (AMAZING!!)

It needed to dig a bit to seek out these two options. Now with the assistance of those two options my handlers had been trying one thing like this.

var specific = require('specific');
var router = specific.Router();

const Ebook = require('../fashions/Ebook');
const books = require('../caches/ebook.cache');
const Base = require('./base.controller');

class BookController extends Base {
  constructor(mannequin) {
    tremendous()
    this.mannequin = mannequin;

    // All of the routes declared right here!
    router.get('/', books.cache, this.index);
    router.get('/:id',books.cache, this.get);
    router.submit('/', this.create);
  }
}

new BookController(Ebook);

module.exports = router;
Enter fullscreen mode

Exit fullscreen mode

You should utilize this in your routes as this:

var booksRouter = require("../controllers/ebook.controller");

module.exports = operate(app) {
  app.use("/books", booksRouter);
};
Enter fullscreen mode

Exit fullscreen mode

Is not this neat and clear? As you may see I’ve imported the Mannequin, and a cache class I’ve carried out for this. When you look intently you may understand how is that this working. Thats the most effective half. I’ve carried out a base controller which will be extented to each controller they usually can use the strategies outlined within the base controller to implement the essential crud.

The bottom controller seems to be one thing like this:

class Base {
  constructor() {}

  index = async (req, res, subsequent) => {
    const sources = await this.mannequin.discover({})
    return res.standing(200).json(this.apiSend(sources))
  }

  get = async (req, res, subsequent) => {
    const useful resource = await this.mannequin.findById(req.params.id)
      .orFail(new NotFoundError());
    return res.standing(200).json(this.apiSend(useful resource))
  }

  create = async (req, res) => {
    const { ...physique } = req.physique
    const useful resource = await this.mannequin.create(physique)
    return res.standing(200).json(this.apiSend(useful resource))
  }

  apiSend = (json) => {
    return {
      information: json
    }
  }
}

module.exports = Base;
Enter fullscreen mode

Exit fullscreen mode

As You’ll be able to see there’s nothing particular to any mannequin or controller. You probably have a create methodology methodology which wants a little bit of manipulations of submit adjustments, You’ll be able to implement that in your new controller utilizing a service module particular to the controller or the mannequin.

A essential level to note is there isn’t any attempt catch blocks in any of the handlers, it is the magic of specific 5.

When you guys have an interest you may test it out here

This mission nonetheless wants plenty of enhancements, however I’m completely happy that it began this fashion! I will maintain posting additional updates.

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

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

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?