npm init adonis-ts-app@latest [nome]
npm i @adonisjs/lucid
node ace configure @adonisjs/lucid
node ace serve --watch
node ace make:model [nome] -m
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
export default class extends BaseSchema {
protected tableName = 'cursos'
public async up () {
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 })
})
}
public async down () {
this.schema.dropTable(this.tableName)
}
}
table
.integer("concessionaria_id")
.unsigned()
.references("id")
.inTable("concessionarias")
.notNullable();
import { DateTime } from 'luxon'
import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'
export default class Curso extends BaseModel {
@column({ isPrimary: true })
public id: number
@column()
public nome: string
@column()
public duracao: number
@column()
public modalidade: string
@column.dateTime({ autoCreate: true })
public createdAt: DateTime
@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime
}
node ace migration:run
node ace migration:rollback
ou
node ace migration:refresh
node ace migration:reset
node ace make:seeder [Nome]
import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'
import Funcionario from 'App/Models/Funcionario'
export default class extends BaseSeeder {
public async run () {
await Funcionario.createMany([
{
concessionariaId:1,
matricula: '12345',
cpf:'001.002.003-04',
salario: 2500,
nome: 'Hugo',
email: '[email protected]',
idade: 20,
telefone: 61991862235,
endereco: 'QNO 7 Conjunto F',
}
])
// Write your database queries inside the run method
}
}
node ace db:seed
node ace make:controller [Nome]
// import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Aluno from "App/Models/Aluno";
export default class AlunosController {
index() {
return Aluno.query();
}
store({ request }) {
const dados = request.only([
"nome",
"cpf",
"matricula",
"email",
"telefone",
"cep",
"logradouro",
"complemento",
"numero",
"bairro",
]);
return Aluno.create(dados);
}
show({ request }) {
const id = request.param("id");
return Aluno.findOrFail(id);
}
async destroy({ request }) {
const id = request.param("id");
const aluno = await Aluno.findOrFail(id);
return aluno.delete();
}
async update({ request }) {
const id = request.param("id");
const aluno = await Aluno.findOrFail(id);
const dados = request.only([
"nome",
"cpf",
"matricula",
"email",
"telefone",
"cep",
"logradouro",
"complemento",
"numero",
"bairro",
]);
aluno.merge(dados).save();
return aluno;
}
}
node ace make:validator [Nome]
import { schema, rules, CustomMessages } from "@ioc:Adonis/Core/Validator";
import type { HttpContextContract } from "@ioc:Adonis/Core/HttpContext";
export default class AlunoValidator {
constructor(protected ctx: HttpContextContract) {}
public schema = schema.create({
nome: schema.string([rules.maxLength(100)]),
cpf: schema.number.optional([
rules.unique({ table: "alunos", column: "cpf" }),
]),
matricula: schema.string([
rules.minLength(20),
rules.maxLength(20),
rules.unique({ table: "alunos", column: "matricula" }),
]),
email: schema.string.optional([
rules.email(),
rules.maxLength(100),
rules.unique({ table: "alunos", column: "email" }),
]),
telefone: schema.string.optional([
rules.range(11, 15),
rules.unique({ table: "alunos", column: "telefone" }),
]),
cep: schema.string.optional(),
logradouro: schema.string.optional([rules.maxLength(100)]),
complemento: schema.string.optional([rules.maxLength(100)]),
numero: schema.string.optional([rules.maxLength(20)]),
bairro: schema.string.optional([rules.maxLength(100)]),
});
public messages: CustomMessages = {};
}
npm i @adonisjs/auth
node ace configure @adonisjs/auth
npm i phc-argon2