AdonisJS — the best NodeJS framework of 2017

Artem Golovin
dirtyjs
Published in
3 min readJul 14, 2017

About six months ago I was searching for an alternative for SailsJS — an MVC Framework for node. Unfortunately, Sails sucked quite a lot in some things — it was slow on POST requests, it did not support many Postgres functions such as transactions and it lacked a few other features (for example, described here). But the worst of it is that it didn’t get a single major update in the last three years.

So we started to search for something to move our nodeJS projects to. We’ve tried Koa.js (but it was lacking too many things), Meteor.js (we don’t really need all-in-one solution, only API), Loopback (too much magic) and a few more.

Until I stumbled into AdonisJS. To be honest, even though the framework has 2.5k starts on github, it was not easy to find. But I immediately felt in love with it. I’ll try to explain why.

Pros

1. Looking on the folder structure and logic behind, it is almost identical to a super popular PHP-framework — Laravel. And Laravel has a good thought-out structure, which allows you to keep the things clear. So if you’ve ever worked with it, you will learn Adonis real quick.

2. Its ORM is made with SQL-databases in mind — such as Postgres or MySQL. It creates efficient SQL-queries and is based on active record idea. Its query builder is easy to learn and allows us to build simple queries quickly. For example:

const bicycles = yield Bicycle.query().whereIn('locationId', locationIds).with('locks').orderBy('label', 'asc').fetch()

But even when we need more complicated stuff like migrations, transactions, joins, chucks or completely “native SQL language” queries — it is all there and easy to use. As for MongoDB lovers — adonis supports it as well.

3. It has a handy validation extension. Makes easy to write rules, for example:

const rules = {
username: 'required|unique:users',
email: 'required|email|unique:users',
password: 'required|confirmed',
group: 'existsIn:Group'
}
const validation = yield Validator.validate(userData, User.rules)if (validation.fails()) throw new Errors.Validation(validation)

These checks can be both synchronous or asynchronous, can access database or whatever needed. And it can be extended very easily, for example, we’ve written a check to prove whether a record exists in our database.

4. Just a bonus! Personally, I love how Adonis uses and utilises generator functions. For example:

// Classical aka spaghetti way
Users.all((err, users) => {
users.bicycles().fetch((err, bicycles) => {
res.send({ users, bicycles })
})
})
// Promise way
let users = null
Users.all()
.then(fetchedUsers => {
users = fetchedUsers
return users.bicycles().fetch()
})
.then(bicycles => {
res.send({ users, bicycles })
})
// Generator way (aka AdonisJS way)
// Yielding pauses code execution until async function is finished
const users = yield Users.all()
const bicycles = yield users.bicycles().fetch()
res.send({ users, bicycles })

It makes the code a lot clearer! No more enclosed functions or promises, only simple code without tabulation.

Cons

  1. There is no default test setup. Writing code without tests — is a very naive way of developing :-) So we had to invest time into implementing our own test setup around Adonis. Upd: official test setup and tutorial will be introduced in the following Adonis 4.0
  2. Because there are not many contributors and users, sometimes you can still catch a bug in core libraries. Some of them are quite obvious :-)
  3. Even though the docs are good, the community is still small. There are not many answers on StackOverflow and there are not many tutorials (except the official one)

Instead of conclusion

We are using AdonisJS in production for the last six months. It is stable and fast. We love it!

If you’re looking for a modern Node.js framework with ES6, Postgres support and a good structure — check Adonis.js

P. S. My latest project — https://pingdisco.com is 100% adonisjs, it is a free uptime monitoring tool. Try creating an account there (only takes one field to fill) and see yourself how fast and smooth it is.

P. P. S. And if you need help or have issues with the framework, don’t hesitate to give me a message on Codementor. I also do tutorial sessions where I can show you the basics and a few cool hacks of Adonis.

--

--