#Help - Extend medusa model

1 messages · Page 1 of 1 (latest)

tight compass
#

Hey team,
I'm facing an issue to extend medusa order model. I want to add some fields in order table without creating new one. I have created model & link for that but facing an error.

order link:
export default defineLink(
{
linkable: CustomOrderModule.linkable.custom_order,
field: 'order_id',
},
OrderModule.linkable.order, // The core Order model
{
readOnly: true, // Use readOnly if you only want to retrieve the linked order
},
);

order model:
const CustomOrder = model.define('custom_order', {
id: model.id().primaryKey(),
orderId: model.text(), // This will be linked to the core Order via a module link
deliveryDate: model.dateTime().nullable(),
warehouseStatus: model
.enum([WarehouseStatus.OPEN, WarehouseStatus.PICKING, WarehouseStatus.SHIPPING, WarehouseStatus.DELIVERED])
.default(WarehouseStatus.OPEN)
.nullable(),
subscriptionBox: model.belongsTo(() => SubscriptionBox),
subscription: model.hasOne(() => Subscription),
});

An error:

lilac hull
#

Hey @tight compass, from what i see, you have a mismatch between your CustomOrder model definition (orderId) and your link definition, targetting that model (order_id).

Also, you don't need to include orderId in CustomOrder model. They way modules work, is by defining links between related models of different modules. So your link would look like:

defineLink(
{
linkable: CustomOrderModule.linkable.custom_order,
field: 'id',
},
OrderModule.linkable.order, // The core Order model
{
readOnly: true, // Use readOnly if you only want to retrieve the linked order
},
);

tight compass
#

Hey @lilac hull , can you advice me how can I use medusa inbuilt services like orderService, custmerService, storeService in my custom service file for medusa V2?

lilac hull
#

Hey @tight compass could you be a little bit more specific? what are you trying to achieve? What purpose does this custom module serve?

tight compass
#

I have some of my custom modules which have their own service files. In those service files I want to use the methods of medusa services like retrieve Orders, update product variant etc.

#

@lilac hull

#

in medusa v1 it's easy to access those methods by importing medusa services in custom service but in medusa v2 it seems difficult

lilac hull
#

Hey @tight compass Medusa v2 introduced the concept of modules, to increase isolation and reduce problems that arise from the lack of it.

To implement cross module functionality you should implement workflows (https://docs.medusajs.com/learn/fundamentals/workflows)

To query for related data between modules, use query (https://docs.medusajs.com/learn/fundamentals/module-links/query#content) and to link related records, use link (https://docs.medusajs.com/learn/fundamentals/module-links/link)

I recommend you go through the fundamentals section of the docs, to get acuqinted with the new paradigm introduced by v2: https://docs.medusajs.com/learn

Explore and learn how to use Medusa.

Explore and learn how to use Medusa.

Explore and learn how to use Medusa.

Explore and learn how to use Medusa.

tight compass
#

Do I also need to create workflow to acess my custom service methods in another service? @lilac hull

lilac hull
tight compass
#

Thanks @lilac hull

lilac hull
#

no worries, happy coding!

tight compass
#

@lilac hull I need your help for service, How can we extend medusa inbuilt service in medusa v2? Ex: In Medusa v1 we used to extend the medusa service like this synctax :
class ExtendedDiscountService extends DiscountService . I have tried to found in medusa doc but didn't find relatable.

lilac hull
#

In Medusa You don't extend core services but rather, create custom modules that You can then link to core modules to create custom workflows

tight compass
#

okay and how to use fileservice in medusa v2? I have below methods in medusa v1 but now in medusa I can't see those method in fileservice.

here is the code od medusa v1 @lilac hull :
const url = await this.fileService.getPresignedDownloadUrl({ fileKey });

const { writeStream, promise, fileKey } = await this.fileService.getUploadStreamDescriptor({
name: ${email}-product-catalog-${Date.now()},
ext: 'csv',
});

lilac hull