loading...

How To Create An API 10 Times Faster?

ismaildenzzz profile image İsmail DENİZ ・4 min read

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.

The Problem

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.

Solution

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.

How It Works

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;

  • GET api/users
  • POST api/users
  • GET api/users/:id
  • PUT api/users/:id
  • DELETE api/users/:id

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!

What About Business Logic

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 documentation!

Conclusion

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 great documentation to will help you to understand AdonisX and many features of it. They are waiting to be discovered by yourself.

Discussion

pic
Editor guide
Collapse
patarapolw profile image
Pacharapol Withayasakpunt

What about SELECT / projection? And Pagination?

Collapse
iozguradem profile image
Özgür Adem Işıklı

If you have a model called User.js, as default, fetching all records is active via the route api/users. In that route, we paginate rows as default because of performance issues.

You may find full documentation about it in here: adonisx.github.io/03-routes/#auto-...

Also, you can filter data with query parameters as described in here

Collapse
patarapolw profile image
Pacharapol Withayasakpunt

Will it be more complete than GraphQL from scratch?

So, I should try Adonis and AdonisX. Not sure how to get started.

I always started from scratch, rarely from complete system, like Adonis, Nest, Django or Angular...

Thread Thread
iozguradem profile image
Özgür Adem Işıklı

Will it be more complete than GraphQL from scratch?

We don't aim to be good than GraphQL. I think it solves different problems. We just focus to create APIs quickly. We need to test this project real-world scenarios more.

So, I should try Adonis and AdonisX. Not sure how to get started.

I can suggest that you to start with AdonisJs Documentation. They are helpful.

Collapse
dimensi0n profile image
Erwan ROUSSEL

That’s wonderful

Collapse
ismaildenzzz profile image
İsmail DENİZ Author

We are glad you like it. We strive to do our best. If you want to get to know us better and convey your requests, you can reach us via Github issues.

github.com/adonisx/adonisx/issues