What is Axe API?
Axe API is the fastest way to create Rest API by defining only database models and relationships between them. It is built on Knex.js, and it's an awesome active records pattern. On the other hand, you have another familiar thing, Express.
Axe API provides you the ability to separate your common tasks to build an API from your business logic. Axe API expects model definitions to analyze your routing structure. After you created your models and their relations between them, Axe API can handle all well-known API requests. Creating an API with 5 tables takes almost 15 minutes.
Shortly, Axe API performs three basic functions;
- Analyzes your models and their relationships to create routes.
- Handles all HTTP requests.
- Separate your business logic from API best practices.
Creating a new project
You can create a new axe-api project in your local development environment;
After that, your API is ready to run!
You can access the API via the URL;
Creating a database table
To execute the migrations, first, you need to install the Knex CLI in your development environment;
Now you can create a new migration file;
In the “migrations” folder, you can see the migration file. Let’s change its content with the following codes;
To implement the changes, you should execute the following commands;
Your database table is ready.
Creating a model
Models are the heart of the Axe API. But they are not simple ORM models. They are more than that.
Let’s create a new one in the “app/models” directory.
As you can see, this is a very simple file. You hardly believe us but the API is ready to use. To check it, please navigate your browser the following URL;
http://localhost:3000/api/users
You will see the following result;
If you can see this result in your browser, it means that you created a simple API by defining a simple model file!
Creating a user
To create a user, you should define that which fields can be sent by clients. To do that, you should add the following getter to your model file;
After that, the API is ready to handle POST requests. You can use the following command to add a new record;
Paginating users
You can use the following request to paginate all users;
http://localhost:3000/api/users
Fetching a user
You can use the following request to fetch a record by using the primary key
http://localhost:3000/api/users/1
Updating a user
You can use the following curl request to update the record;
Deleting a user
You can use the following curl request to delete a record;
Summary
Axe API is not a passive framework that expects you to define everything. Unlike other frameworks or libraries, Axe API tries to understand what you will build under best practices’ guidance. Whenever you change something in the code, it just doesn’t mean you tell something to the programming language, it also means that Axe API should take an action about it.
In this tutorial, I just tried to show you how easy to create a simple CRUD API. Of course, this is a very simple example that doesn’t cover all real-life scenarios. But I guarantee that Axe API is a powerful tool for API-based projects.
You can find the whole project in the GitHub repository;
Learn More
If you want to deep dive to Axe API, you can use the following documents;