Working with Actions

An Action is a self-contained class responsible for performing a single, clearly-defined task within your application. This promotes modularity, maintainability, and consistency across your AdonisJS project.

The simplest action

An Action is simply a class with a handle method.

app/actions/charge_user_action.ts
import  from '#models/user'
import {  } from '@foadonis/actions'
 
export default class  implements  {
  (: ) {
    // Your business logic
  }
}

Running your action

Actions can be ran easily within your application using actions.run().

import  from '@foadonis/actions/services/main'
import  from '#actions/charge_user_action'
 
await .(, user)

Dependency Injection

As your actions are resolved from the Container you can use Dependency Injection.

app/actions/charge_user_action.ts
import {  } from '@foadonis/actions'
import {  } from '@adonisjs/core'
import  from '#models/user'
import  from '#services/stripe_service'
 
@()
export default class  implements  {
  constructor(private : ) {}
 
  (: , : number) {
    await this..chargeCustomer(.stripeId, )
  }
}

On this page