Build your own marketplace from scratch using Medusa
models
folder, that will extend the core entity with new properties
services
folder that will initially only include our basic changes (LIFE_TIME
,this.loggedInUser_
…)
Product
with a Store
, therefore we use the same reasoning as the User
table we extended earlier
Store
implies an update of the previously extended Store
model
up
and down
functions like this :
Product
table
Product
tied to a Store
, we’ll start by extending the ProductService
and overriding the create
function to force the logged-in user’s store_id
into the product before inserting it. However, you will see type problems with the input type expected on the method, therefore we will need to expand the type to add our new store_id
property
User
who is logged in AND has a store_id
must be able to create a product for its store.
On the other hand, when a User
wants to retrieve products, there are currently no constraints preventing him from seeing ONLY its store’s products, let’s implement this feature.
store_id
to retrieve only his products. However, the selector of this function does not know the store_id
property at all, so we will expand the selector to make it aware of that new property
ProductService.retrieve
, ProductService.update
and more, depending on your needsproduct.handle
might create an issue in the long term when creating a product for a store, as it’s supposed to be unique. You can for example either add a prefix or suffix containing the store_id
to avoid any problems :
/store/products
relations and allowed fields.
First you’ll need to create a new loader in the /src/loaders/
directory :
/store/products
API route, the goal here is to be able to add a query parameter /store/products?store_id=<STORE_ID>
, and the default Medusa validator for that API is not aware of the new property we wants to add.
Next step, is to create a file in the /src/api/
folder, named specifically index.ts
.