Using existing services in a custom medusa service

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In the previous article, we had an introduction to creating our very first custom medusa service.
I wanted to expand on that and show you how we can use this custom service for other services. This is not limited to the medusa ones. You can even load your custom services this way.
For this article, we'll use the translation service as our base. Inside we'll look at injecting the product service so we can translate products.
The main file will stay the same. As we already extend the TransactionBaseService we can use the constructor and assign services via this constructor.
Let's look at how we can import the product service.
class TranslateService extends TransactionBaseService {
productService;
constructor({ productService }) {
super();
this.productService = productService;
}
}
This will give us access to the product service.
Let's create a translateProduct function. This function will take a product ID. With this ID it can fetch the product and return the title.
(In a real-world example, we would get a translation from another table)
translateProduct = async (productId, locale) => {
const product = await this.productService.retrieve(productId);
// Here we would add our translation
return product.title;
};
Let's modify our endpoint so we can test out if it works.
router.get('/store/title', (req, res) => {
const translateService = req.scope.resolve('translateService');
translateService
.translateProduct('prod_01GCKNHT1KPGBK1JR2MK7JC6KY', 'en')
.then((title) => {
res.json({ title });
});
});
You should see the title returned if you re-run your server and query this route.
Note: make sure to change the ID to a product you have in your database

Let's say we want to create yet another custom service we call TestService.
Inside this service, we want to use our custom translate service. We can easily use the same approach to use.
import { TransactionBaseService } from '@medusajs/medusa';
class TestService extends TransactionBaseService {
translateService;
constructor({ translateService }) {
super();
this.translateService = translateService;
}
test(msg, locale) {
return this.translateService.translate(msg, locale);
}
}
export default TestService;
So nothing special here. We port the existing translate function into this test function.
We can then use it as such:
router.get('/store/title', (req, res) => {
const testService = req.scope.resolve('testService');
const title = testService.test('title', 'nl');
res.json({ title });
});
This is merely an example of how you can use your custom services inside another custom service.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter