#Use of repository in custom service
7 messages · Page 1 of 1 (latest)
Hello, have you tried following this guide: https://docs.medusajs.com/learn/fundamentals/modules/db-operations
Yes, I have tried this by injecting repository as shown in doc but that gave me AwilixResolutionError. I have tried many solutions to fix that but none of them worked. and after that I have injected the models in service file like this:
class SubscriptionBoxSupplyModuleService extends MedusaService({
SubscriptionBoxSupply,
Subscription,
SubscriptionBox,
SubscriptionCategoryProductOptions,
}) {
private readonly logger: Logger;
private readonly manager: EntityManager;
static LIFE_TIME = Lifetime.SCOPED;
constructor(container: InjectedDependencies) {
super(container);
this.logger = container.logger;
this.manager = container.manager;
}
}
and used their inbuilt CRUD methods using super but now I'm getting this error:
Cannot add alias "subscription" for "subscription_box_supply". It is already defined for Service "subscription".
Error: Cannot add alias "subscription" for "subscription_box_supply". It is already defined for Service "subscription".
type InjectedDependencies = {
manager: EntityManager;
logger: Logger;
event_bus: AbstractEventBusModuleService;
baseRepository: DAL.RepositoryService;
subscriptionRepository: DAL.RepositoryService<typeof Subscription>;
};
class DraftOrderModuleService extends MedusaService({
DraftOrder,
,
}) {
static resolutionKey = 'draftOrderService';
protected readonly manager: EntityManager;
protected readonly logger: Logger;
protected readonly eventBusService: AbstractEventBusModuleService;
protected readonly subscriptionRepository_: DAL.RepositoryService<typeof Subscription>;
static readonly Events = {
CREATED: 'draft_order.created',
UPDATED: 'draft_order.updated',
CANCELLED: 'draft_order.cancelled',
SKIPPED: 'draft_order.skipped',
};
constructor(container: InjectedDependencies) {
super(container);
this.logger = container.logger;
this.manager = container.manager;
this.subscriptionRepository_ = container.subscriptionRepository;
this.eventBusService = container.event_bus;
}
I have also tried using this way as this is my draft order service and I need to use subscription repository which is in another module but this gives me below error :
Resolution path: subscriptionRepository
AwilixResolutionError: Could not resolve 'subscriptionRepository'.
}
Resolution path: subscriptionRepository
at resolve (C:\Project\HerogoV2\Herogo\node_modules\awilix\src\container.ts:497:15)
I'm sorry it's a bit confusing. Can you share the entire service code that you have now? Is the issue in the subscription or draft order one?
Basically I have two different modules draft order & subscription. now I want to use subscription repository in draft order module service. I have injected the repository and implemented the methods but when i run my project it gives me
Error starting server
error: Could not resolve 'subscriptionRepository'.
Resolution path: subscriptionRepository
AwilixResolutionError: Could not resolve 'subscriptionRepository'.
Resolution path: subscriptionRepository
at resolve (C:\Project\HerogoV2\Herogo\node_modules\awilix\src\container.ts:497:15)
at Object.subscriptionRepository [as get] (C:\Project\HerogoV2\Herogo\node_modules\awilix\src\container.ts:269:52)
at new DraftOrderModuleService (C:\Project\HerogoV2\Herogo\src\modules\draft_order\service.ts:106:46)
You can't inject a repository from one module to another, as explain in the Module Isolation documentation: https://docs.medusajs.com/learn/fundamentals/modules/isolation
What you should do instead is build a workflow that uses the services of each module to each your feature