Application scaffolding in AdonisJS is a technique that allows the developer to define and create a basic application that can create, retrieve, update and delete objects. Basically CRUD (create, Read, Update, Delete) data operations are common in most of the applications.
- Adonis ^4.0
- Postgres ^9.0
Install adonis-scaffold by running the below command.
npm install adonis-scaffold --save
Also add providers for the newly installed dependencies.
const providers = [
'adonis-scaffold/providers/ScaffoldProvider'
];
LOGO_URL=https://adonisjs.com/images/header-logo.svg
LOGO_PATH="/images/logo.svg"
COLOR_PRIMARY=#220052
COLOR_SECONDARY=#FB2F51
TYPE=api
Example from Controller extending ScaffoldController:
"use strict";
const ScaffoldController = use("ScaffoldController");
const model = use("App/Models/User");
class UserController extends ScaffoldController {
constructor() {
super();
this.resource = {
model
};
}
}
module.exports = UserController;
Example from Model extending ScaffoldModel:
The model need two functions required static get hidden()
and static get visible()
.
"use strict";
const ScaffoldModel = use("ScaffoldModel");
class User extends ScaffoldModel {
static get hidden() {
return ["id", "password", "created_at", "updated_at"];
}
static get visible() {
return ["username", "email", "password"];
}
}
module.exports = User;
For relationship the function static get with()
is required.
Example:
static get with(){
return ['tokens']
}
Example:
async index(request) {
this.data = await this.resource.model.first();
return super.index(request);
}
accessible_attributes:
{
name: "name",
type: "character"
}
Thanks to the community of AdonisJs.