1

I'm currently working with Medusa V2 and trying to create a custom service inside the modules folder. However, I'm unsure how to modify the SQL table using this service in the new version.

In Medusa V1, I could easily modify the table by doing something like this in the service:

import { TransactionBaseService } from "@medusajs/medusa";
import { ReservationDate } from "../models/reservation-date";

export default class ReservationService extends TransactionBaseService {
  private readonly reservationDateRepository_;

  constructor(container) {
    super(container); // Pass the container to the base class constructor
    this.manager_ = container.manager;
    this.reservationDateRepository_ = container.reservationDateRepository;
  }

  async createReservation(
    variantId: string,
    customerId: string,
    startDate: Date,
    endDate: Date,
  ) {
    return await this.atomicPhase_(async (transactionManager) => {
      const reservationRepo = transactionManager.withRepository(
        this.reservationDateRepository_,
      );

      const reservation = reservationRepo.create({
        variant_id: variantId,
        customer_id: customerId,
        start_date: startDate,
        end_date: endDate,
      });

      return await reservationRepo.save(reservation);
    });
  }
}

I was able to access the repository like this:

const reservationRepo = this.manager_.getRepository(ReservationDate);

or like this:

const reservationRepo = transactionManager.withRepository(
        this.reservationDateRepository_,
      );

This allowed me to create and save records in the SQL table using methods like create and save.

However, in Medusa V2, I'm not sure how to achieve this. Specifically, I’m confused about how to make the create, save, and other repository methods work with the new architecture.

  • How do I properly access the repository in Medusa V2?
  • How can I implement create and save operations on a custom model in the new version?

Any help or guidance on how to perform these operations in Medusa V2 would be greatly appreciated!

1 Answer 1

0

By default, when you create a service that extends MedusaService, under the hood you get functions that allow you to interact with your entity directly.

"The MedusaService function receives an object of the module's data models as a parameter, and generates methods to manage those data models" https://docs.medusajs.com/learn/customization/custom-features/module#3-create-module-service

On top of that, you can also access the manager we used to have on V1 from your V2 services.

You can perform database queries by injecting the manager inside your service's methods.

// other imports...
import { 
  InjectManager,
  MedusaContext,
} from "@medusajs/framework/utils"
import { Context } from "@medusajs/framework/types"
import { EntityManager } from "@mikro-orm/knex"

class BlogModuleService {
  // ...

  @InjectManager()
  async getCount(
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<number | undefined> {
    return await sharedContext?.manager?.count("my_custom")
  }
  
  @InjectManager()
  async getCountSql(
    @MedusaContext() sharedContext?: Context<EntityManager>
  ): Promise<number> {
    const data = await sharedContext?.manager?.execute(
      "SELECT COUNT(*) as num FROM my_custom"
    ) 
    
    return parseInt(data?.[0].num || 0)
  }
}

For more reference, check the docs : https://docs.medusajs.com/learn/fundamentals/modules/db-operations#run-queries

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.