How To Create An API 10 Times Faster?
Tags: node, rest, javascript, adonis
Published: 2020-05-10 19:41:47

I love coding as a developer who has been coding for almost 15 years. But it doesn’t mean that I like coding the same stuff all the time. I hate writing simple CRUD actions. I hate creating the same forms over and over again. That’s why I created a simple but efficient way to create an API 10 times faster. And I believe that it is gonna work.

How did I measure the speed? Actually I didn’t. I don’t know any method which can compare two (almost same) developer’s speed in different ways to create an API. But I can feel the speed. Please keep reading and I am going to show you why it is so fast.

<h2>The Problem</h2>
The problem is duplicating the same tasks over and over again. After you choose a framework in a programming language, you create a table, you create a model, you create the routes, and finally, you implement basic CRUD functions in your controller. But writing CRUD function is not the aim. We are not writing code just because of inserting or updating records on the database. We are writing codes because we want to add some business logic in it. Inserting/updating or deleting is not our target.
On the other hand, when you look at a database structure, you can see clearly its API endpoints by relationships by remembering best practices.
So I decided to create a structure that analyzes models and their relations between each other to creates and handles routes.

<h2>Solution</h2>
To create an API, frameworks help a lot to developers. So I decided to work with a framework. I chose AdonisJs which has got an ORM library built-in and many other things that are helpful because I am a NodeJs developer.
I created a simple service provider which I called as AdonisX. It is not a framework. It is just a service provider that uses a framework (AdonisJs) and injects itself into an AdonisJs application to analyzes models and relations to create and handle routes.

<h2>How It Works</h2>
Using AdonisX is very simple. AdonisX analyzes models in your application. Let’s assume that you have a model like this;

```
const XModel = use('AdonisX/Models/XModel')

class User extends XModel {
  static get table () {
    return 'users'
  }
}

module.exports = User
```

When you have this model in your application, AdonisX will analyze your models in the initialization process and created the following routes;

<ul>
<li>GET api/users</li>
<li>POST api/users</li>
<li>GET api/users/:id</li>
<li>PUT api/users/:id</li>
<li>DELETE api/users/:id</li>
</ul>

AdonisX just doesn’t create your routes. It also handles your HTTP request for this resource. You should define your models, nothing more. Your API is ready!

<h2>What About Business Logic</h2>
The key point is business logic. Let’s remember again; we don’t write code to insert or update a record on a database. We code because we have some business logic which helps us to make money. For example, as a developer, I may need to form validations, allowing only some routes, sending emails to the new users after creation, and even adding some kind of complex code. How can I implement all these business logic?
There are two different ways you can use it. AdonisX provides you some kind of definitions that you can extend for your business logic. This is the first way. For example, you can decide form validations in your model definitions;

```
const XModel = use('AdonisX/Models/XModel')

class Users extends XModel {
  static get validations () {
    return {
      email: 'required|email',
      name: 'required|max:50',
      surname: 'required|max:50',
      age: 'max:100'
    }
  }
}

module.exports = User
```

There are many options which you can use. But I think that this configuration will not be enough soon or later because you will have very complex business logic. For example, you may need to send a request to your other service to check something. In Adonix, we created a structure which is called as actions which help you to implement your business logic.
Just you should create an action file to catch a specific request for your resource. This is a simple example of it;

```
// app/Actions/UserActions.js

module.exports = {
  async onBeforeCreate ({ request, params, data }) {
    // Implement your business logic in here.
  }
}
```

It is done! You can handle all beforeCreate actions for your User model. In there, you can code whatever you want.
Also, there is an event mechanism that you can use. Check it out on the <a href="https://adonisx.github.io/">documentation!</a>

<h2>Conclusion</h2>
This article is not about telling all features of AdonisX. I tried to explain why I created it and why it helps us to create APIs as quickly as possible. But it is not just that! There is <a href="https://adonisx.github.io/">great documentation</a> to will help you to understand AdonisX and many features of it. They are waiting to be discovered by yourself.

<ul>
<li><a href="https://adonisx.github.io">https://adonisx.github.io</a></li>
<li><a href="https://github.com/adonisx/adonisx">https://github.com/adonisx/adonisx</a></li>
<li><a href="https://github.com/adonisx/adonisx-cli">https://github.com/adonisx/adonisx-cli</a></li>
</ul>