Current version : 0.21.0
Made in 🇫🇷 ◌ GitHubNPM

💡 Sept. Tip => use case server init

Executing some setup logic for each use case when the server starts.

During this month of September 2025, we'll highlight various and interesting aspects of libmodulor.

Some use cases that run on the server need some setup when the server starts. Examples of this include :

  • Creating some collection somewhere (e.g. an index on Algolia)
  • Authenticating to some external service (e.g. creating a token used across subsequent requests)
  • etc.

For this, you can use uc.def.lifecycle.server.init.

const CreateBookUCD: UCDef<CreateBookInput, CreateBookOPI0> = {
    // ...
    lifecycle: {
        // ...
        server: {
            // ...
            init: CreateBookServerInit,
            // ...
        },
        // ...
    },
    // ...
}

CreateBookServerInit is a simple class that implements UCInit.

@injectable()
export class CreateBookServerInit implements UCInit {
    constructor(
        // This is a hypothetical SearchManager where an implementation could be AlgoliaSearchManager
        @inject('SearchManager') private searchManager: SearchManager,
    ) {}

    public async exec(): Promise<void> {
        await this.searchManager.createIndex('books', {
            attributeForDistinct: 'title',
            distinct: true,
            searchableAttributes: ['title'],
        });
    }
}

When the server starts, it executes this logic and you're good to go.

Note that ideally these are idempotent so you can safely run them at every server startup.

Voilà c'est tout.