A basestack where lot of features will be handelled outof the box. So even if you don't know adonis and quickly want resource endpoints, you can use this template.
This is the boilerplate for creating an API server in AdonisJs, it comes pre-configured with.
- Bodyparser
- Authentication
- CORS
- Lucid ORM
- Migrations and seeds
- Pagination
- Resource
Install Adonis
npm i -g @adonisjs/cli
Use the adonis command to install the blueprint
git clone https://github.com/hypermine-bc/adonis-rest-template.git
clone the repo and then run npm install
.
adonis serve --dev
Run the following command to run startup migrations.
adonis migration:run
We are lazy developers Right! 😜
So lets make the controller as lean as possible 😉
Pay attention to these variables, that are defined
allowedWiths updatable, searchable
'use strict'
const BaseController = use('App/Controllers/Http/BaseController')
const UserValidations = use('App/Validators/AuthRegister')
const User = use('App/Models/User');
class UserController extends BaseController {
constructor() {
super();
this.model = User
this.allowedWiths = [
'posts',
'tokens'
]
this.updatable = [
'username',
'email',
'password'
]
this.searchable = [
'username',
'email',
'id'
]
this.getValidators = function () {
return new UserValidations()
// return validations.rules
}
}
}
module.exports = UserController
Yes! you got it right search is out of the box for every resource that extends baseController.
Ex 1 : Search for user contains vik
{{HYPERMINE_API_HOST}}/api/v1/users/?query=vik
Ex 2 : Search for user contains Vik with Posts and token
{{HYPERMINE_API_HOST}}/api/v1/users/?query=vik&attributes=posts,tokens
Look how easy it was right 😉
Lets say you want to get User with Posts and tokens
You can do request API like
{{HYPERMINE_API_HOST}}/api/v1/users/?attributes=posts,tokens
Since we are very lazy and generally fitrers are complicated so. This template add's it out-of-the-box.
Lets say you want to get users where username=vikram
{{HYPERMINE_API_HOST}}/api/v1/users/?filters=[{"username": "vikram"}]
Or perhaps you want to get vikram whoes id = 1
{{HYPERMINE_API_HOST}}/api/v1/users/?filters=[{"username":"vikram"},{"id": 1}]
Lets get one step further, lets say you want to get
Get user starts with vi
and id=1 with posts and token
{{HYPERMINE_API_HOST}}/api/v1/users/?attributes=posts,tokens&filters=[{"username": [{"startsWith": "vi"}]},{"id": 1}]
Some more ;)
{{HYPERMINE_API_HOST}}/api/v1/users/?attributes=posts,tokens&filters=[{"username": [{"endsWith": "ide"}]}]
Finally we have logging Out-Of-The_box
As explained in Adonis framework the access log you can fine in your tmp/adonis.log file.
Ex: Loggin an update method
{"userid":2,"controller":"UserController","method":"update()","data":{},"time":"Mon Aug 19 2019 01:14:09 GMT+0530 (IST)","level":"info","message":"Resource-id : 5"}