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!