Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/datatable2 #3

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LOG_LEVEL=info
APP_KEY=TG5wX4cU9QL_Dch8q0Nbv37a3GssIPJc
NODE_ENV=development
SESSION_DRIVER=cookie
DB_CONNECTION=postgres
DB_HOST=127.0.0.1
DB_PORT=5432
DB_USER=postgres
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ yarn-error.log
.DS_Store

# railway
railway-prod-connection.json
railway-prod-connection.json

# db
*.sqlite
7 changes: 6 additions & 1 deletion adonisrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default defineConfig({
() => import('@adonisjs/auth/auth_provider'),
() => import('@adonisjs/mail/mail_provider'),
() => import('#providers/turbo/turbo_provider'),
() => import('#providers/data_table/datatable_builder_provider'),
],

/*
Expand All @@ -52,7 +53,11 @@ export default defineConfig({
| List of modules to import before starting the application.
|
*/
preloads: [() => import('#start/routes'), () => import('#start/kernel')],
preloads: [
() => import('#start/routes'),
() => import('#start/kernel'),
() => import('#start/view'),
],

/*
|--------------------------------------------------------------------------
Expand Down
54 changes: 54 additions & 0 deletions app/controllers/employees_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Employee from '#models/employee'
import { employeeUpdateValidator } from '#validators/employee'
import type { HttpContext } from '@adonisjs/core/http'

const tableConfig = {
baseUrl: '/employees',
pagination: { perPage: 10 },
select: ['*'],
// mark filterable fields
// TODO - refactor to string[]
filterable: {
city: 'city',
name: 'name',
salary: 'salary',
},
// mark sortable fields
sortable: {
salary: 'salary',
name: 'name',
},
// mark searchable fields
searchable: ['name', 'city', 'position'],
}

export default class EmployeesController {
async index(ctx: HttpContext) {
const { turboFrame } = ctx

const employees = await Employee.query().datatable(ctx.request.all(), tableConfig)

return turboFrame.render('pages/employees/index', { employees })
}

async update({ params, request, turboStream }: HttpContext) {
const columns = await request.validateUsing(employeeUpdateValidator)

const employee = await Employee.findOrFail(params.id)

const saved = await employee.merge(columns).save()

return turboStream
.update('pages/employees/_table_row', { employee: saved }, `table-row-${params.id}`)
.render()
}

async delete({ params, turboStream }: HttpContext) {
const employee = await Employee.findOrFail(params.id)
console.log(employee)

await employee.delete()

return turboStream.remove(`table-row-${params.id}`).render()
}
}
25 changes: 25 additions & 0 deletions app/models/employee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { DateTime } from 'luxon'
import { BaseModel, column } from '@adonisjs/lucid/orm'

export default class Employee extends BaseModel {
@column({ isPrimary: true })
declare id: number

@column()
declare name: string

@column()
declare city: string

@column()
declare position: string

@column()
declare salary: number

@column.dateTime({ autoCreate: true })
declare createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime
}
10 changes: 10 additions & 0 deletions app/validators/employee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import vine from '@vinejs/vine'

export const employeeUpdateValidator = vine.compile(
vine.object({
name: vine.string().trim().minLength(1).maxLength(256),
city: vine.string().trim().minLength(1).maxLength(256),
position: vine.string().trim().minLength(1).maxLength(256),
salary: vine.number().withoutDecimals().min(1),
})
)
8 changes: 7 additions & 1 deletion config/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import env from '#start/env'
import { defineConfig } from '@adonisjs/lucid'

const dbConfig = defineConfig({
connection: 'postgres',
connection: env.get('DB_CONNECTION') || 'postgres',
connections: {
sqlite: {
client: 'better-sqlite3',
connection: {
filename: './db.sqlite',
},
},
postgres: {
client: 'pg',
connection: {
Expand Down
13 changes: 13 additions & 0 deletions database/factories/employee_factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import factory from '@adonisjs/lucid/factories'
import Employee from '#models/employee'

export const EmployeeFactory = factory
.define(Employee, async ({ faker }) => {
return {
name: faker.person.fullName(),
city: faker.location.city(),
position: faker.person.jobTitle(),
salary: faker.number.int({ min: 2000, max: 15000 }),
}
})
.build()
23 changes: 23 additions & 0 deletions database/migrations/1720127077572_create_employee_table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BaseSchema } from '@adonisjs/lucid/schema'

export default class extends BaseSchema {
protected tableName = 'employees'

async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments('id')

table.string('name')
table.string('city')
table.string('position')
table.integer('salary').checkPositive()

table.timestamp('created_at')
table.timestamp('updated_at')
})
}

async down() {
this.schema.dropTable(this.tableName)
}
}
8 changes: 8 additions & 0 deletions database/seeders/employee_seeder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { EmployeeFactory } from '#database/factories/employee_factory'
import { BaseSeeder } from '@adonisjs/lucid/seeders'

export default class extends BaseSeeder {
async run() {
await EmployeeFactory.createMany(900)
}
}
Loading