Adonis.js

Adonis.js Documentation

Basic Example

todos_controller.ts
import { HttpContext } from '@adonisjs/core/http'
import cache from '#services/cache.service'
import Todo from '#models/todo'

export default class TodosController {
    /**
     * Return list of all todos
     */
    async index({ response }: HttpContext) {
        let cachedData;
        const fileName = 'todos'
        const key = 'index'
        
        try {
            cachedData = await cache.get({ fileName, key })
        } catch (_) {}
        
        if (!cachedData) {
            try {
                cachedData = await Todo.all()
                await cache.set({ fileName, key, payload: cachedData })
            } catch (e) {
                return response.badRequest({ error, message: 'Something went wrong while getting records' })
            }
        }
        return response.ok({ data: cachedData })
    }

    /**
     * Handle form submission to create a new todo
     */
    async store({ request, response }: HttpContext) {
        const { name } = request.only(['name'])
        
        try {
            await Todo.create({
                name,
                completed: false,
            })
            await cache.flushByRegex('todos') // Invalidates todos so that we get fresh results on next index() call
            return response.ok({ data: 'Successfully inserted a new record' })
        } catch (error) {
            return response.badRequest({ error, message: 'Something went wrong while inserting a record' })
        }
    }

    /**
     * Display a single todo by id.
     */
    async show({ request, response }: HttpContext) {
        const id = request.param('id')
        
        let cachedData;
        const fileName = 'todos-id'
        const key = id
        
        try {
            cachedData = await cache.get({ fileName, key })
        } catch (_) {}
        
        if (!cachedData) {
            try {
                cachedData = await Todo.find(id)
                await cache.set({ fileName, key, payload: cachedData })
            } catch (error) {
                return response.badRequest({ error, message: 'Something went wrong while getting a record' })
            }   
        }
        return response.ok({ data: cachedData })
    }

    /**
     * Handle the form submission to update a specific todo by id
     */
    async update({ request }: HttpContext) {
        const id = request.param('id')
        const { name } = request.only(['name'])
        
        try {
            const todo = await Todo.findOrFail(id)
            todo.name = name
            
            await todo.save()
            await cache.flushByRegex('todos', id) // Invalidates single todo by id so that we get fresh results on next show() call
            return response.ok({ data: 'Successfully updated a record' })
        } catch (error) {
            return response.badRequest({ error, message: 'Something went wrong while updating a record' })
        }
    }
}
cache.service.ts
import { FileSysCache } from 'file-sys-cache'

const cache = new FileSysCache({
    basePath: './.file-sys-cache'
})

export default cache