There was Rails, then Laravel and now Adonis js
Tags: javascript, adonis, rails, laravel
Published: 2021-03-20 23:25:32

---
title: There was Rails, then Laravel and now Adonis js
published: true
description: 
tags: javascript, adonis, rails, laravel
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r7pwf06szmde50ryv93.png
series: AdonisLaravel
---
Follow me!: <a href="https://twitter.com/EricTheCoder_" class="twitter-follow-button" data-show-count="false">Follow @EricTheCoder_</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<hr>

<h2>Adonis = MVC Framework for Node</h2>
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
```php
Route::get('/', [PostController::class, 'index']);
```

Adonis route
```js
Route.get('/', 'PostsController.index')
```

Laravel migrations
```php
public function up() {
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->timestamps();
        $table->string('title');
        $table->text('content');
    });
}
```

Adonis migrations
```js
protected tableName = 'posts'
public async up () {
    this.schema.createTable(this.tableName, (table) => {
      table.increments('id')
      table.timestamps(true)
      table.string('title')
      table.text('content')
    })
  }
```

Laravel controller:
```php
class BlogController extends Controller
{   
    public function index()
    {
        $posts = Post::all();
        return view('posts.index', ['posts', $posts]);
    }
```

Adonis controller
```js
export default class PostsController {
  public async index (ctx: HttpContextContract) {
    const posts = await Post.all()
    return ctx.view.render('posts/index', {posts})
  }
```

Laravel view (blade)
```jinja
@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
```

Adonis view (edge)
```jinja
@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 
```

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:
```js
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

}
```

<h2>Conclusion</h2>
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: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>