📜 ⬆️ ⬇️

NodeJS framework with Laravel syntax (and no noodles in code)

Running through Habra was surprised to notice that this framework is mentioned only in a few digests, although in my opinion deserves more attention. Below is a lot of code and very few comments on it. And yes, friends! This is my first article on Habré, please do not strongly be and do not scold.


Many of us got used to Laravel in a relatively short time, his elegant style, Artisan for the console, Eloquent ORM, Middlewares, Providers, etc. So thought the creator of the Adonis JS framework and completely repeated everything that we love laravel for, but only on NodeJS - well, isn't that great? Let's take a closer look:


Application structure


├── app │ ├── Commands │ ├── Http │ ├── Listeners │ ├── Model ├── bootstrap ├── config ├── database │ ├── migrations │ └── seeds ├── providers ├── public ├── resources │ └── views ├── storage 

Supported Databases



Migrations and console utility Ace (Artisan)


 #   ./ace make:migration users --create=users # database/migrations/1464437815620_users.js 

Migration code

 'use strict' const Schema = use('Schema') class UsersSchema extends Schema { up () { this.create('users', (table) => { table.increments() table.string('email').unique() table.timestamps() }) } down () { this.drop('users') } } module.exports = UsersSchema 

Some Ace commands


 #  ./ace migration:run #  ./ace migration:status #    ./ace migration:rollback #     ./ace migration:refresh 

Some interesting observations


AdonisJS installation is fast enough and reduced to just a few commands. After installing it, you can immediately roll out the migration; by default, the framework will create a SQLite file in the database folder.


Routing


 const Route = use('Route') const User = use('App/Model/User') Route.get('/', function * (request, response) { const users = yield User.all() response.json(users) }) Route.get('users', 'UsersController.index') 

Database


Working with the database in AdonisJS is as beautiful as in Laravel 5, there is Gettters | Setters, Database Hooks allow you to listen to model events and, for example, at the time of Save, perform some actions.


Query builder


  const john = yield Database .table('users') .where('username', 'john') .limit(1) 

Lucid (Eloquent)


 yield User.all() yield User.query().where('status', 'active').fetch() 

Relationship


 class User extends Lucid { profile () { return this.hasOne('App/Model/Profile') } posts () { return this.hasMany('App/Model/Post') } } 

And why AdonisJS if there is Laravel?


Despite the fact that php is actively developing, js largely overtook it. Just look at the front end and its frameworks. One VueJS is worth. I will run through the points:


Same syntax


Similar language constructions both at the front and at the back


Giant npm world


You need a tricky functional, look for it on npm, it is not a bit less than composer.


Asynchrony out of the box


In the controller, you can return a response at the very beginning. And all the laborious and blocking operations to perform after that, in the background:


High performance


I did some of my little tests, and NodeJS won them all.


Websocket


It is very convenient to work with ws using the Eloquent-like model.


 class SiteController { * save (request, response) { response.ok({success:true}) const post = new SiteRequest() post.fill( request.all() ) yield post.save() // SQL Update console.log(request.all()) } } 

But what about js single-threaded?


This code is my custom solution taken from the NodeJS dock , which allows parallelizing processes:


 const cluster = require('cluster'); const numCPUs = require('os').cpus().length const http = require('./bootstrap/http') if (process.env.NODE_ENV === 'production') { if (cluster.isMaster) { console.log(`Master ${process.pid} is running`); // Fork workers. for (let i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', (worker, code, signal) => { console.log(`worker ${worker.process.pid} died`); }); } else { // Workers can share any TCP connection // In this case it is an HTTP server runServer() console.log(`Worker ${process.pid} started`); } }else{ runServer() } function runServer() { const http = require('./bootstrap/http') http(function () { use('Event').fire('Http.start') }) } 

Also have



With this, I want to complete my review and share some links:



')

Source: https://habr.com/ru/post/324220/


All Articles