Parses a module expression to extract the module import path and the method to call on the default exported class.
moduleExpression('#controllers/users_controller').parse()
// ['#controllers/users_controller', 'handle']
With method
moduleExpression('#controllers/users_controller.index').parse()
// ['#controllers/users_controller', 'index']
Converts the module expression to a callable function. Invoking this method run internally import the module, create a new instance of the default export class using the container and invokes the method using the container.
You can create a callable function using the container instance as shown below
const fn = moduleExpression('#controllers/users_controller.index')
.toCallable(container)
// Call the function and pass context to it
await fn(ctx)
Another option is to not pass the container at the time of creating the callable function, but instead pass a resolver instance at the time of calling the function
const fn = moduleExpression('#controllers/users_controller.index')
.toCallable()
// Call the function and pass context to it
const resolver = container.createResolver()
await fn(resolver, ctx)
Optionalcontainer: TConverts the module expression to an object with handle method. Invoking the handle method run internally imports the module, create a new instance of the default export class using the container and invokes the method using the container.
You can create a handle method object using the container instance as shown below
const handler = moduleExpression('#controllers/users_controller.index')
.toHandleMethod(container)
// Call the function and pass context to it
await handler.handle(ctx)
Another option is to not pass the container at the time of creating the handle method object, but instead pass a resolver instance at the time of calling the function
const handler = moduleExpression('#controllers/users_controller.index')
.toHandleMethod()
// Call the function and pass context to it
const resolver = container.createResolver()
await handler.handle(resolver, ctx)
Optionalcontainer: T
The moduleExpression module works around a very specific pattern we use with AdonisJS, ie to bind modules as string.
For example: With the router of AdonisJS, we can bind a controller to a route as follows.
Behind the scenes, we have to run following operations in order to call a method on the users_controller class.
#controllers/users_controllermoduleindexmethod on the controller class using the container.Router is just one example, we do this with event listeners, redis pub/sub and so on.
So, instead of writing all this parsing logic, we encapsulate it inside the "moduleExpression" module.