gopocketbase

PocketBase event OnAfterBootstrap


I am new to pocketbase. I want to write go function initializeDb that would run immediately after app isbootstrap i.e. before ever command (not just before Serve). I started like this:

    app.OnBootstrap().Bind(&hook.Handler[*core.BootstrapEvent]{
        Id: "ensureCollections",
        Func: func(e *core.BootstrapEvent) error {
            return db.ensureCollections()
        },
    })

Unfortunately this is executed before app is bootstraped as the bootstrap even trigger run the db initialization hook as a very last (https://github.com/pocketbase/pocketbase/blob/7175992ce4edead4d8e7c6e00aa5c2ef540fe20b/core/base.go#L382). What do I miss? How do I do that?


Solution

  • The problem was I didn't carefuly read documentation nor the pocketbase codebase. Developer need to explicitly call e.Next() to proceed with chain of handlers. Following works as expected:

        app.OnBootstrap().Bind(&hook.Handler[*core.BootstrapEvent]{
            Id: "ensureCollections",
            Func: func(e *core.BootstrapEvent) error {
                if err := e.Next(); err != nil {
                    return err
                }
                return db.ensureCollections()
            },
        })