Subscribe

Using services in custom medusa routes

✍️

How to use services in custom medusa routes to retrieve information

22 Sep, 2022 · 2 min read

Another cool part about medusa is that we can use services to interact with existing functionality.

For instance, we can access the ProductService from there. We can retrieve products but also create and update them.

Let’s see how we can use these services inside our custom routes.

Using services in Medusa routes

For this example, I’ll be using the ProductService. First, we’ll want to add a custom route endpoint.

Inside our custom route, we can start by assigning this service like this.

const productService = req.scope.resolve('productService');

Let’s say we want to list all products. We can easily do that with the following command.

router.get('/store/test', async (req, res) => {
  const productService = req.scope.resolve('productService');
  productService.list().then((products) => {
    res.json(products);
  });
});

If you now restart your server and query this endpoint, you should be able to see a list of your products. (My database only has one effect).

Custom service call in medusa route

This is, of course, not super beneficial as we already have an endpoint to retrieve precisely this information. The cool part is that we can choose to manipulate this data. Let’s say we only want items that have stock.

router.get('/store/test', async (req, res) => {
  const productService = req.scope.resolve('productService');
  productService.list({}, { relations: ['variants'] }).then((products) => {
    res.json(
      products.filter((product) =>
        product.variants.some((variant) => variant.inventory_quantity >= 0)
      )
    );
  });
});

We have quite a few changes here. Let’s take a closer look. The first one is that we pass arguments to the products list function. These arguments are as follows:

  • {}: The first option is the params to query on. In our case, nothing in particular
  • {relations:['variants']}: This can be used to add specific options to the query. We want to add the variants

Once we have this, we filter all products and only return products where at least one variant has stock.

You can play with adjusting the stock levels in the query.

And that’s it, we super powerful way of querying from service in medusa.

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

On a webshop break

3 Oct, 2022 · 2 min read

On a webshop break

Medusa interacting with our custom entity

2 Oct, 2022 · 4 min read

Medusa interacting with our custom entity

Join 2097 devs and subscribe to my newsletter