Loaders | Medusa Development Course

By Viktor Holik

Featured image

Welcome to our Medusa Development Course! In this series, you'll learn how to get your development environment ready and take your first steps in building a Medusa-powered e-commerce application. We'll walk you through everything step by step, helping you master Medusa’s modular architecture and build the key features you need.

This course was made by Viktor Holik, our Software Engineer and Medusa Expert.

In this guide, we’ll explore how loaders work within Medusa core and then create a custom loader. This example will walk you through setting up a simple logger that delays for a few seconds before logging a message to the console.

Loaders

You can find the transcription of the guide below.

Medusa loaders are scripts that run when the Medusa backend starts, and in Medusa core they are used to initialize critical components, like database connections, plugins, other resources, the dependency container, and they allow you to perform custom actions during the start-up process.

In the Medusa core repository, if you navigate to the loaders folder and open the pgconnection.ts file, you'll see how this loader initializes the database connections, and register them within the dependency container.

Similarly, by checking the models.ts loader, you can observe how Mendusa identifies core and backend model files under your local machine, and register them in the dependency container.

Since we have already created the loader called extendStoreProductFields, that imports from Medusa core API routes and adds new values to the endpoint relations, let's now create a new loader that logs a message.

We should create an exported default function, which will take the Medusa container as the first argument. Now let's reach you the logger from the dependency container. And then use setTimeout function to create a 5-second delay.

Now let's use logger to log a message in the console.

And now after the backend starts, we will check the 5 seconds, and you should see the message being logged.

In short

You now have a solid understanding of Medusa loaders and how they support essential startup tasks. By creating a custom loader, we’ve shown how you can easily add your own logic to Medusa’s initialization phase, whether for logging, connecting resources, or performing other setup steps.

In the next episode, we'll create and customize subscribers in Medusa to respond to key events, like sending an email when an order is placed

We hope you found Viktor's tutorial insightful and helpful.

Learn how to create a customizable commerce solutions

Check out our Medusa Development Course on YouTube

Other blog posts

Services | Medusa Development Course

Welcome to our Medusa Development Course! Improve Medusa’s SupplierService by making a few updates, like adding transaction support, handling configurations, and adjusting the service’s behavior.

API Routes | Medusa Development Course

Welcome to our Medusa Development Course! Explore the essential aspect of building and customizing API routes within Medusa.

Tell us about your project

Got a project in mind? Let's make it happen!

By clicking “Send Message” you grant us, i.e., Rigby, consent for email marketing of our services as part of the communication regarding your project. You may withdraw your consent, for example via hello@rigbyjs.com.
More information
placeholder

Grzegorz Tomaka

Co-CEO & Co-founder

LinkedIn icon
placeholder

Jakub Zbaski

Co-CEO & Co-founder

LinkedIn icon