DEV Community

Cover image for There was Rails, then Laravel and now Adonis js
Eric The Coder
Eric The Coder

Posted on • Edited on

There was Rails, then Laravel and now Adonis js

Follow me!: Follow @EricTheCoder_


Adonis = MVC Framework for Node

I am a Rails and Laravel developer. I love those framework. They are powerful, stable, fast and easy to use. When you get use to develop with a good MVC framework, it's hard to go back.

Earlier this year I had the mandate to develop a javascript frontend application. To accompany this application I said to myself why not use a backend in javascript also. The goal was to have my entire stack in javascript.

On paper it seemed easy but in reality the task was much more complicated than expected. I couldn't find a javascript framework that in my opinion was as good as Laravel and Rails. Good in the sense of development efficiency i.e. powerful, fast but very easy and pleasing to work with.

I did try 3 different frameworks and none matched what I was looking for, so I decided for this project to use a Laravel backend.

Recently, I stumbled across a framework that had been around for quite some time but for some reason that I didn't know had gone under my radar. This framework is Adonis.js

What is Adonis? In summary, this is the Node.js version of Laravel. It is a framework that contrasts with other Node.js frameworks. Like Laravel, Adonis has for mission the happiness of the developer and also as mandate to provide all the necessary tools to carry out a project from A to Z.

I haven't done a real project in production yet but I still had the chance to do some small projects for fun and I have to say I'm pleasantly surprised how natural and easy to use Adonis is.

Being an MVC framework just like Laravel, the learning curve is much shorter.

Here are some examples that compare Laravel code with Adonis.js code. See the similarities:

Laravel route

Route::get('/', [PostController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode

Adonis route

Route.get('/', 'PostsController.index')
Enter fullscreen mode Exit fullscreen mode

Laravel migrations

public function up() {
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->timestamps();
        $table->string('title');
        $table->text('content');
    });
}
Enter fullscreen mode Exit fullscreen mode

Adonis migrations

protected tableName = 'posts'
public async up () {
    this.schema.createTable(this.tableName, (table) => {
      table.increments('id')
      table.timestamps(true)
      table.string('title')
      table.text('content')
    })
  }
Enter fullscreen mode Exit fullscreen mode

Laravel controller:

class BlogController extends Controller
{   
    public function index()
    {
        $posts = Post::all();
        return view('posts.index', ['posts', $posts]);
    }
Enter fullscreen mode Exit fullscreen mode

Adonis controller

export default class PostsController {
  public async index (ctx: HttpContextContract) {
    const posts = await Post.all()
    return ctx.view.render('posts/index', {posts})
  }
Enter fullscreen mode Exit fullscreen mode

Laravel view (blade)

@extends('layouts.app')

@section('content')
<h1>Welcome to my Blog</h1>
    @foreach($posts as $post)
      <h3>{{post->id}}. <a href="{{ route('PostsController.show', $post)}}">{{ post->title }}</a></h3> <br>
      {{ post->content }} <br><br><hr>
    @endforeach
@endsection
Enter fullscreen mode Exit fullscreen mode

Adonis view (edge)

@layout('app')

@section('page')
    <h1>Welcome to my Blog</h1>
    @each(post in posts)
      <h3>{{post.id}}. <a href="{{ route('PostsController.show', {id: post.id})}}">{{ post.title }}</a></h3> <br>
      {{ post.content }} <br><br><hr>
    @endeach
@endsection 
Enter fullscreen mode Exit fullscreen mode

As you can see the learning curve is pretty low.

But not everything is the same for example. In Adonis the model is explicitly declare in the model file. In Laravel, the model is only define in the migration file.

Adonis model file:

export default class Post extends BaseModel {
  @column({ isPrimary: true })
  public id: number

  @column.dateTime({ autoCreate: true })
  public createdAt: DateTime

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  public updatedAt: DateTime

  @column()
  public title: string

  @column()
  public content: string

}
Enter fullscreen mode Exit fullscreen mode

Conclusion

That's it for today. Stay tune because I will post many more articles on Adonis.js in near future.

You can follow me on Twitter: Follow @justericchapman

Top comments (10)

Collapse
 
karmekk profile image
Bartosz Gleń

Thanks for this post, I wasn't aware of this one :)

I think Node.js lacked such a framework, to be honest I found developing with Express tedious and boring. Hope it gets more popular - again, thanks for spreading the word!

Collapse
 
ericchapman profile image
Eric The Coder

The project have close to 10k stars on github...

github.com/adonisjs/core

Collapse
 
karmekk profile image
Bartosz Gleń

Sorry I don't get if it's sarcasm or not, but Express.js has over 52k stars, not to mention frontend frameworks like Vue. So yes, I think it's not that popular yet.

Collapse
 
paztek profile image
Matthieu Balmes

I also strongly suggest NestJS as a full stack Node.JS framework. It’s built on top of Express (but you can switch to fastify).
It has adapters for TypeORM, Sequelize or Mongoose. And the dependency injection really helps with large projects.

Collapse
 
migueldevelopez profile image
MiguelDevelopez

Laravel developer here! and oh my god I hope Adonis become more popular! 😍

Collapse
 
johannchopin profile image
johannchopin

Would be nice at some point to have the link to the github page of this tool 👍

Collapse
 
ericchapman profile image
Eric The Coder
Collapse
 
robencom profile image
robencom

I'm staying tuned and following you, because as someone who ADORES Laravel, this (Adonis) might be a great way to do backend work in JS instead of PHP (if needed...).

Collapse
 
ngprnk profile image
Nischal Gautam

Getting there but nowhere near Rails in terms of DX and productivity.

Collapse
 
ericchapman profile image
Eric The Coder

If we compare to other Node.js framework. The DX is excellent. Of course compare to ultra mature product like Rails, Adonis is not quit there yet... That will come with more community adoption/contribution. But for now it’s a good start.