Skip to main content
Skip to main content
You're viewing the documentation for v1, which isn't the latest Medusa version.Latest documentation

ProductService

constructor​

Parameters​

__namedParametersInjectedDependenciesRequired

Properties​

manager_EntityManagerRequired
transactionManager_undefined | EntityManagerRequired
__container__anyRequired
productOptionRepository_Repository<ProductOption>Required
productRepository_Repository<Product> & objectRequired
productVariantRepository_Repository<ProductVariant>Required
productTypeRepository_Repository<ProductType> & objectRequired
productTagRepository_Repository<ProductTag> & objectRequired
imageRepository_Repository<Image> & objectRequired
productCategoryRepository_TreeRepository<ProductCategory> & objectRequired
productVariantService_ProductVariantServiceRequired
searchService_SearchServiceRequired
salesChannelService_SalesChannelServiceRequired
eventBus_EventBusServiceRequired
featureFlagRouter_FlagRouterRequired
remoteQuery_RemoteQueryFunctionRequired
IndexName"products"Required
EventsobjectRequired
Events.UPDATEDstringRequired

Default: "product.updated"

Events.CREATEDstringRequired

Default: "product.created"

Events.DELETEDstringRequired

Default: "product.deleted"

__configModule__Record<string, unknown>
__moduleDeclaration__Record<string, unknown>

Accessors​

activeManager_​

Returns​

EntityManagerEntityManagerRequired

Methods​

withTransaction​

Parameters​

transactionManagerEntityManager

Returns​

thisthisRequired

shouldRetryTransaction_​

Parameters​

errRecord<string, unknown> | objectRequired

Returns​

booleanbooleanRequired

atomicPhase_​

Wraps some work within a transactional block. If the service already has a transaction manager attached this will be reused, otherwise a new transaction manager is created.

Type Parameters​

TResultobjectRequired
TErrorobjectRequired

Parameters​

work(transactionManager: EntityManager) => Promise<TResult>Required
the transactional work to be done
isolationOrErrorHandlerIsolationLevel | (error: TError) => Promise<void | TResult>
the isolation level to be used for the work.
maybeErrorHandlerOrDontFail(error: TError) => Promise<void | TResult>
Potential error handler

Returns​

PromisePromise<TResult>Required
the result of the transactional work

list​

Lists products based on the provided parameters.

Parameters​

selectorProductSelectorRequired
an object that defines rules to filter products by
configFindProductConfigRequired
object that defines the scope for what should be returned

Returns​

PromisePromise<Product[]>Required
the result of the find operation

listAndCount​

Lists products based on the provided parameters and includes the count of products that match the query.

Parameters​

selectorProductSelectorRequired
an object that defines rules to filter products by
configFindProductConfigRequired
object that defines the scope for what should be returned

Returns​

PromisePromise<[Product[], number]>Required
an array containing the products as the first element and the total count of products that matches the query as the second element.

count​

Return the total number of documents in database

Parameters​

selectorSelector<Product>Required
the selector to choose products by

Default: {}

Returns​

PromisePromise<number>Required
the result of the count operation

retrieve​

Gets a product by id. Throws in case of DB Error and if product was not found.

Parameters​

productIdstringRequired
id of the product to get.
configFindProductConfigRequired
object that defines what should be included in the query response

Returns​

PromisePromise<Product>Required
the result of the find one operation.

retrieveByHandle​

Gets a product by handle. Throws in case of DB Error and if product was not found.

Parameters​

productHandlestringRequired
handle of the product to get.
configFindProductConfigRequired
details about what to get from the product

Default: {}

Returns​

PromisePromise<Product>Required
the result of the find one operation.

retrieveByExternalId​

Gets a product by external id. Throws in case of DB Error and if product was not found.

Parameters​

externalIdstringRequired
handle of the product to get.
configFindProductConfigRequired
details about what to get from the product

Default: {}

Returns​

PromisePromise<Product>Required
the result of the find one operation.

retrieve_​

Gets a product by selector. Throws in case of DB Error and if product was not found.

Parameters​

selectorSelector<Product>Required
selector object
configFindProductConfigRequired
object that defines what should be included in the query response

Returns​

PromisePromise<Product>Required
the result of the find one operation.

retrieveVariants​

Gets all variants belonging to a product.

Parameters​

productIdstringRequired
the id of the product to get variants from.
configFindProductConfigRequired
The config to select and configure relations etc...

Returns​

PromisePromise<ProductVariant[]>Required
an array of variants

filterProductsBySalesChannel​

Parameters​

productIdsstring[]Required
salesChannelIdstringRequired
configFindProductConfigRequired

Returns​

PromisePromise<Product[]>Required

listTypes​

Returns​

PromisePromise<ProductType[]>Required

listTagsByUsage​

Parameters​

takenumberRequired

Default: 10

Returns​

PromisePromise<ProductTag[]>Required

isProductInSalesChannels​

Check if the product is assigned to at least one of the provided sales channels.

Parameters​

idstringRequired
product id
salesChannelIdsstring[]Required
an array of sales channel ids

Returns​

PromisePromise<boolean>Required
Check if the product is assigned to at least one of the provided sales channels.

create​

Creates a product.

Parameters​

productObjectCreateProductInputRequired
the product to create

Returns​

PromisePromise<Product>Required
resolves to the creation result.

update​

Updates a product. Product variant updates should use dedicated methods, e.g. addVariant, etc. The function will throw errors if metadata or product variant updates are attempted.

Parameters​

productIdstringRequired
the id of the product. Must be a string that can be casted to an ObjectId
updateUpdateProductInputRequired
an object with the update values.

Returns​

PromisePromise<Product>Required
resolves to the update result.

delete​

Deletes a product from a given product id. The product's associated variants will also be deleted.

Parameters​

productIdstringRequired
the id of the product to delete. Must be castable as an ObjectId

Returns​

PromisePromise<void>Required
empty promise

addOption​

Adds an option to a product. Options can, for example, be "Size", "Color", etc. Will update all the products variants with a dummy value for the newly created option. The same option cannot be added more than once.

Parameters​

productIdstringRequired
the product to apply the new option to
optionTitlestringRequired
the display title of the option, e.g. "Size"

Returns​

PromisePromise<Product>Required
the result of the model update operation

reorderVariants​

Parameters​

productIdstringRequired
variantOrderstring[]Required

Returns​

PromisePromise<Product>Required

updateOption​

Updates a product's option. Throws if the call tries to update an option not associated with the product. Throws if the updated title already exists.

Parameters​

productIdstringRequired
the product whose option we are updating
optionIdstringRequired
the id of the option we are updating
dataProductOptionInputRequired
the data to update the option with

Returns​

PromisePromise<Product>Required
the updated product

retrieveOptionByTitle​

Retrieve product's option by title.

Parameters​

titlestringRequired
title of the option
productIdstringRequired
id of a product

Returns​

PromisePromise<null | ProductOption>Required
product option

deleteOption​

Delete an option from a product.

Parameters​

productIdstringRequired
the product to delete an option from
optionIdstringRequired
the option to delete

Returns​

PromisePromise<void | Product>Required
the updated product

updateShippingProfile​

Assign a product to a profile, if a profile id null is provided then detach the product from the profile

Parameters​

productIdsstring | string[]Required
ID or IDs of the products to update
profileIdnull | stringRequired
Shipping profile ID to update the shipping options with

Returns​

PromisePromise<Product[]>Required
updated products

prepareListQuery_​

Temporary method to be used in place we need custom query strategy to prevent typeorm bug

Parameters​

configFindProductConfigRequired

Returns​

qstringRequired
relationskeyof Product[]Required
queryFindWithoutRelationsOptionsRequired
qstringRequired
relationskeyof Product[]Required
queryFindWithoutRelationsOptionsRequired

decorateProductsWithSalesChannels​

Temporary method to join sales channels of a product using RemoteQuery while MedusaV2 FF is on.

Parameters​

productsProduct[]Required

Returns​

PromisePromise<Product[]>Required
Temporary method to join sales channels of a product using RemoteQuery while MedusaV2 FF is on.

getSalesChannelModuleChannels​

Temporary method to fetch sales channels of a product using RemoteQuery while MedusaV2 FF is on.

Parameters​

productIdsstring[]Required

Returns​

PromisePromise<Record<string, SalesChannel[]>>Required
Temporary method to fetch sales channels of a product using RemoteQuery while MedusaV2 FF is on.
Was this section helpful?