Service container event



Does adonis support firing an event each time it building desired class?

for example in laravel I’ll have it like this:

class MyCustomServiceProvider extends ServiceProvider {

    public function register() {

    public function boot($someDataInject) {
        $this->app->resolving(MyCustomFactory::class, function (MyCustomFactory $factory) use ($someDataInject) {
            return $this->doSomethingFromDb($factory, $someDataInject);

what’s important to have attention here is this:
$this->app->resolving(MyCustomFactory::class, callback);

You may find information about it here:

I’m trying to move some of my production system in php to Adonis.js, and got stuck on this issue.



Just to clarify, are you looking for an event, or a callback? As I don’t see an actual event being fired in your demo code. But it looks like resolving method is taking a callback as the second argument.

I think it’d be better if you stated your actual need, as not everything is directly translatable from one language to another. And maybe ther is another, better, solution to your problem.


Hi Moltar,

As you may see in the example I’ve provided the app has a resolving method which accept 2 arguments.
The first one is the desired resolved class by the container and the second is the function that called when the object has been resolved.

I’ve already got a working php code and I need to move this part to nodejs.
The reason for all this event is as you can see when the object has been resolved I need to fetch some information from the db.

Anyone know a way to figure it out?



You could use a service provider to build a service object, at which point you have full control of object construction and you could add any custom logic you want.

Would that work?


I’ve already done it with adonis service-provider
the thing is that I don’t have that resolve method

class myServiceProvider extends ServiceProvider{
    boot(){'App/Services/MyFactoryClass', async (app) =>{
            const someModel = app.use('App/Models/someModel');
                    csm = await someModel.query()
                        .where('enabled', 1)
        returning myFactoryClass;


So the problem here is that if I’ll update some rows in the DB to be enabled = 0 for example, the ServiceProvider won’t be updated since the app’s instance is already running with the pulled data from before.
This is why I’ve used resolve() in laravel service-providers.

Any way to make it in Adonis?


Are you constructing a singleton service?


No, not constructing a singleton


Maybe I am not understanding you correctly…

But here is a demo of a service provider that resolves on every invocation:

Run it, and then see the log output. It resolves on every page request.

2018-03-11T15:59:27.886Z - info: serving app on
2018-03-11T15:59:30.473Z - info: Resolving
2018-03-11T15:59:30.474Z - info: Constructing
2018-03-11T15:59:32.096Z - info: Resolving
2018-03-11T15:59:32.096Z - info: Constructing
2018-03-11T15:59:33.599Z - info: Resolving
2018-03-11T15:59:33.599Z - info: Constructing


Thanks for the example!
It is working as expected in your example, so it looks like the fetching data from db caused it.
because it wait for the deferred promise to be resolved (async-await)

is there anyway to solve it?


Added an example to the repo with async wait in the constructor.

Maybe if you create a slim example of what is not working for you, then I could help you fix it?


Hi Moltar,

I’ve looked at your example, the different from my code was that in my controller I had:

const Foo = use('Foo')

class myController{
         async query({request, response}){
                     let myFoo = await Foo;
                     //do the rest of the stuff..

So if I move the Foo=use('Foo') into the query() method, everything work as expected.

Thank you so much!


Yeah, I hope that makes sense to you now.

When you use outside of the request cycle, you are getting one instance at the global scope level. When you use inside the request cycle, you are getting a new instance on each request.