importBaseSchemafrom'@ioc:Adonis/Lucid/Schema'exportdefaultclassextendsBaseSchema{protectedtableName='cursos'publicasyncup(){this.schema.createTable(this.tableName,(table)=>{table.increments('id')table.string('nome',50).notNullable()table.integer('duracao')table.string('modalidade',1).notNullable()/** * Uses timestamptz for PostgreSQL and DATETIME2 for MSSQL */table.timestamp('created_at',{useTz: true})table.timestamp('updated_at',{useTz: true})})}publicasyncdown(){this.schema.dropTable(this.tableName)}}
import{DateTime}from'luxon'import{BaseModel,column}from'@ioc:Adonis/Lucid/Orm'exportdefaultclassCursoextendsBaseModel{
@column({isPrimary: true})publicid: number
@column()publicnome: string
@column()publicduracao: number
@column()publicmodalidade: string
@column.dateTime({autoCreate: true})publiccreatedAt: DateTime
@column.dateTime({autoCreate: true,autoUpdate: true})publicupdatedAt: DateTime}
Rodar as Migration
node ace migration:run
Voltar as Migration
node ace migration:rollback
ou
node ace migration:refresh
Voltar as Migration ao início
node ace migration:reset
Criar uma seeder
node ace make:seeder [Nome]
Código de uma seeder
importBaseSeederfrom'@ioc:Adonis/Lucid/Seeder'importFuncionariofrom'App/Models/Funcionario'exportdefaultclassextendsBaseSeeder{publicasyncrun(){awaitFuncionario.createMany([{concessionariaId: 1,matricula: '12345',cpf: '001.002.003-04',salario: 2500,nome: 'Hugo',email: 'hugo@gmail.com',idade: 20,telefone: 61991862235,endereco: 'QNO 7 Conjunto F',},])// Write your database queries inside the run method}}
Rodar uma seeder
node ace db:seed
Criando um Controller.
node ace make:controller [Nome]
Codigo de uma rota com Controller
// import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'importAlunofrom'App/Models/Aluno'exportdefaultclassAlunosController{index(){returnAluno.all()}store({ request }){constdados=request.only(['nome','cpf','matricula','email','telefone','cep','logradouro','complemento','numero','bairro',])returnAluno.create(dados)}show({ request }){constid=request.param('id')returnAluno.findOrFail(id)}asyncdestroy({ request }){constid=request.param('id')constaluno=awaitAluno.findOrFail(id)returnaluno.delete()}asyncupdate({ request }){constid=request.param('id')constaluno=awaitAluno.findOrFail(id)constdados=request.only(['nome','cpf','matricula','email','telefone','cep','logradouro','complemento','numero','bairro',])aluno.merge(dados).save()returnaluno}}
Validação
node ace make:validator [Nome]
Exemplo de Validação
import{schema,CustomMessages,rules}from'@ioc:Adonis/Core/Validator'importtype{HttpContextContract}from'@ioc:Adonis/Core/HttpContext'exportdefaultclassAlunoValidator{constructor(protectedctx: HttpContextContract){}publicschema=schema.create({nome: schema.string([rules.alpha({allow: ['space']}),rules.maxLength(100)]),cpf: schema.string.nullableAndOptional([rules.unique({table: 'alunos',column: 'id'}),rules.regex(/^\d{3}\.\d{3}\.\d{3}\-\d{2}$/),]),matricula: schema.string([rules.unique({column: 'matricula',table: 'alunos',}),rules.alphaNum(),rules.maxLength(20),]),email: schema.string.nullableAndOptional([rules.email(),rules.unique({table: 'alunos',column: 'email'}),rules.maxLength(100),]),telefone: schema.string.nullableAndOptional([rules.regex(/^\(?[1-9]{2}\)? ?(?:[2-8]|9[1-9])[0-9]{3}\-?[0-9]{4}$/),rules.mobile({locale: ['pt-BR']}),rules.unique({table: 'alunos',column: 'telefone'}),]),cep: schema.string.nullableAndOptional([rules.regex(/[0-9]{5}-[\d]{3}/)]),logadouro: schema.string.nullableAndOptional([rules.alpha({allow: ['space']}),rules.maxLength(100),]),complemento: schema.string.nullableAndOptional([rules.maxLength(100),rules.alpha({allow: ['space']}),]),numero: schema.string.nullableAndOptional([rules.alphaNum({allow: ['dash','space']}),rules.maxLength(120),]),bairro: schema.string.nullableAndOptional([rules.alpha({allow: ['space']}),rules.maxLength(120),]),})publicmessages: CustomMessages={'maxLength': 'o número máximo de caractéres do campo {{ field }} é de {{ options.maxLength }}','minLength': 'o número máximo de caractéres do campo {{ field }} é de {{ options.minLength }}','required': 'o campo {{ field }} é obrigatório','unique': 'o campo {{ field }} é único','cpf.regex': 'o cpf tem a formatação 666.666.666-13','cep.regex': 'o cep tem a formatação 12345-67','telefone.regex': 'o telefone tem a formatação (00)9999-9999',}}
Autenticação
instalar
npm i @adonisjs/auth
configurar
node ace configure @adonisjs/auth
# Lucid# API token# User# Yes# Database# Yes
npm i phc-argon2