Adonis 3.0 dev release

Adonis 3.0 is going to be the most stable release so far. In past few months, we have gathered plenty of feedback, solved a handful of issues and progressively made Adonis the most practical framework in the community.

The newer version is going to have breaking changes and this dev release is a sneak peek of same. If you are planning to start a new project, consider using the dev release as it is going to be the future.

Setting up dev project #

You can make use of adonis-cli to setup the dev project, but make sure to pass the --dev flag.

adonis new yardstick --dev

What’s new? #

Lots of new features have been added to the framework that I will be talking about, also trello board can be used as a reference for same https://trello.com/b/yzpqCgdl/adonis-for-humans.

Model Hooks #

Model hooks have been introduced to keep your code DRY by attaching hooks to your models, which are called on specific events.

Available Hooks
beforeCreate
afterCreate
beforeUpdate
afterUpdate
beforeDelete
afterDelete
beforeRestore
afterRestore

Hooks are saved inside the app/Model/Hooks directory, and you can register them inside your model static boot method.

Password hash example using a hook. #

In order to create a hook, we will make use of ace make:hook method.

./ace make:hook User --method=encryptPassword

app/Model/Hooks/User.js

'use strict'

const Hash = use('Hash')

const User = exports = module.exports = {}

User.encryptPassword = function * (next) {
  this.password = yield Hash.make(this.password)
  yield next
}

app/Model/User

class User extends Lucid {

  static boot () {
     super.boot()
     this.addHook('beforeCreate', 'User.encryptPassword')
  }

}

Trust me, you would have never encrypted a password that easily.

Multi-tenant apps #

Lucid now supports a unique connection per model. Which means your models can make use of different database connections on runtime.

class User extends Lucid {

   static get connection () {
       return 'mysql'
   }

}

Assuming your application makes use of SQLite by default and you want to use MySQL to manage your users. Define a static getter returning the connection to be used for a given model, which is mysql in this case.

In-built Repl #

ace now has an in-built REPL to play with your code, without having to setup controllers, routes, and views. Also, it has support for ES6 generators out of the box.

Repl

Route Renderer #

Every application needs logic-less views like an about page, a contact page etc. These web pages do not require any logic, so routing them through controllers does not make much sense.

New route renderer will let you render views using the new render method.

Route.on('/contact').render('contact')
Route.on('/signup').render('signup')

render method will pass the request object to your views, so you can make use of all request methods within your views. For example - you can get email address passed as a query string to your signup view.

// url /signup?email=abc@example.org
{{ request.input('email') }}

Breaking Changes #

For the better future of Adonis, we have to come up with breaking changes. These changes are well thought and will likely not going to get change quickly.

Model query builder #

In order to get access to the model query builder, you need to make use of query method.

Earlier

User.where('username', '')

Now

User.query().where('username', '')

Little verbose, but it solves the problem described in Github issue #20

.nunjucks views #

According to issue #133, having .html the extension makes it harder to apply proper syntax highlighting to your views.

We decided to drop .html and make use of .nunjucks, that has extensive support for the majority of editors.

  1. Atom
  2. Sublime

If in case you are not able to find the nunjucks support for your favorite editor, just search for twig highlighting. Which is similar to nunjucks.

Brief #

Also, there are few other components added to the dev release.

  1. Events provider and listeners.
  2. Support for data seeders.
  3. Model/Database factories.
  4. Improvements to ace.

Final Release #

We will be releasing 3.0 soon with extensive documentation, up-to-date CHANGELOG and upgrade guides.

The final release will have the shiny Authentication provider.

The New E-book #

Chris has written quite a few popular e-books on Laravel. Me ( Harminder Virk ) and Chris are working together to release an e-book focusing on practical learning and building useful products with Adonis.

Do subscribe to the mailing list to receive the preview chapters.

 
83
Kudos
 
83
Kudos

Now read this

Changing Route.url behavior

The Route.url method returns a fully qualified URL to a registered route. For example: // register route Route.get('user/:username', 'UserController.show') // following returns - user/virk Route.url('user/:username', { username: 'virk'... Continue →