Skip to main content

Class: ProductService

Hierarchy

  • TransactionBaseService

    ProductService

Constructors

constructor

new ProductService(__namedParameters)

Parameters

NameType
__namedParametersInjectedDependencies

Overrides

TransactionBaseService.constructor

Defined in

packages/medusa/src/services/product.ts:78

Properties

__configModule__

Protected Optional Readonly __configModule__: Record<string, unknown>

Inherited from

TransactionBaseService.__configModule__

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:10


__container__

Protected Readonly __container__: any

Inherited from

TransactionBaseService.__container__

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:9


__moduleDeclaration__

Protected Optional Readonly __moduleDeclaration__: Record<string, unknown>

Inherited from

TransactionBaseService.__moduleDeclaration__

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:11


eventBus_

Protected Readonly eventBus_: EventBusService

Defined in

packages/medusa/src/services/product.ts:68


featureFlagRouter_

Protected Readonly featureFlagRouter_: FlagRouter

Defined in

packages/medusa/src/services/product.ts:69


imageRepository_

Protected Readonly imageRepository_: typeof ImageRepository

Defined in

packages/medusa/src/services/product.ts:63


manager_

Protected manager_: EntityManager

Overrides

TransactionBaseService.manager_

Defined in

packages/medusa/src/services/product.ts:55


productCategoryRepository_

Protected Readonly productCategoryRepository_: typeof ProductCategoryRepository

Defined in

packages/medusa/src/services/product.ts:65


productOptionRepository_

Protected Readonly productOptionRepository_: typeof ProductOptionRepository

Defined in

packages/medusa/src/services/product.ts:58


productRepository_

Protected Readonly productRepository_: typeof ProductRepository

Defined in

packages/medusa/src/services/product.ts:59


productTagRepository_

Protected Readonly productTagRepository_: typeof ProductTagRepository

Defined in

packages/medusa/src/services/product.ts:62


productTypeRepository_

Protected Readonly productTypeRepository_: typeof ProductTypeRepository

Defined in

packages/medusa/src/services/product.ts:61


productVariantRepository_

Protected Readonly productVariantRepository_: typeof ProductVariantRepository

Defined in

packages/medusa/src/services/product.ts:60


productVariantService_

Protected Readonly productVariantService_: ProductVariantService

Defined in

packages/medusa/src/services/product.ts:66


searchService_

Protected Readonly searchService_: SearchService

Defined in

packages/medusa/src/services/product.ts:67


transactionManager_

Protected transactionManager_: undefined | EntityManager

Overrides

TransactionBaseService.transactionManager_

Defined in

packages/medusa/src/services/product.ts:56


Events

Static Readonly Events: Object

Type declaration

NameType
CREATEDstring
DELETEDstring
UPDATEDstring

Defined in

packages/medusa/src/services/product.ts:72


IndexName

Static Readonly IndexName: "products"

Defined in

packages/medusa/src/services/product.ts:71

Methods

addOption

addOption(productId, optionTitle): Promise<Product>

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

NameTypeDescription
productIdstringthe product to apply the new option to
optionTitlestringthe display title of the option, e.g. "Size"

Returns

Promise<Product>

the result of the model update operation

Defined in

packages/medusa/src/services/product.ts:646


atomicPhase_

Protected atomicPhase_<TResult, TError>(work, isolationOrErrorHandler?, maybeErrorHandlerOrDontFail?): Promise<TResult>

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

Name
TResult
TError

Parameters

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

Returns

Promise<TResult>

the result of the transactional work

Inherited from

TransactionBaseService.atomicPhase_

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:50


count

count(selector?): Promise<number>

Return the total number of documents in database

Parameters

NameTypeDescription
selectorSelector<Product>the selector to choose products by

Returns

Promise<number>

the result of the count operation

Defined in

packages/medusa/src/services/product.ts:171


create

create(productObject): Promise<Product>

Creates a product.

Parameters

NameTypeDescription
productObjectCreateProductInputthe product to create

Returns

Promise<Product>

resolves to the creation result.

Defined in

packages/medusa/src/services/product.ts:383


delete

delete(productId): Promise<void>

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

Parameters

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

Returns

Promise<void>

empty promise

Defined in

packages/medusa/src/services/product.ts:612


deleteOption

deleteOption(productId, optionId): Promise<void | Product>

Delete an option from a product.

Parameters

NameTypeDescription
productIdstringthe product to delete an option from
optionIdstringthe option to delete

Returns

Promise<void | Product>

the updated product

Defined in

packages/medusa/src/services/product.ts:811


filterProductsBySalesChannel

filterProductsBySalesChannel(productIds, salesChannelId, config?): Promise<Product[]>

Parameters

NameType
productIdsstring[]
salesChannelIdstring
configFindProductConfig

Returns

Promise<Product[]>

Defined in

packages/medusa/src/services/product.ts:306


isProductInSalesChannels

isProductInSalesChannels(id, salesChannelIds): Promise<boolean>

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

Parameters

NameTypeDescription
idstringproduct id
salesChannelIdsstring[]an array of sales channel ids

Returns

Promise<boolean>

Defined in

packages/medusa/src/services/product.ts:361


list

list(selector, config?): Promise<Product[]>

Lists products based on the provided parameters.

Parameters

NameTypeDescription
selectorProductSelectoran object that defines rules to filter products by
configFindProductConfigobject that defines the scope for what should be returned

Returns

Promise<Product[]>

the result of the find operation

Defined in

packages/medusa/src/services/product.ts:117


listAndCount

listAndCount(selector, config?): Promise<[Product[], number]>

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

Parameters

NameTypeDescription
selectorProductSelectoran object that defines rules to filter products by
configFindProductConfigobject that defines the scope for what should be returned

Returns

Promise<[Product[], number]>

an array containing the products as the first element and the total count of products that matches the query as the second element.

Defined in

packages/medusa/src/services/product.ts:141


listTagsByUsage

listTagsByUsage(count?): Promise<ProductTag[]>

Parameters

NameTypeDefault value
countnumber10

Returns

Promise<ProductTag[]>

Defined in

packages/medusa/src/services/product.ts:346


listTypes

listTypes(): Promise<ProductType[]>

Returns

Promise<ProductType[]>

Defined in

packages/medusa/src/services/product.ts:337


prepareListQuery_

Protected prepareListQuery_(selector, config): Object

Creates a query object to be used for list queries.

Parameters

NameTypeDescription
selectorSelector<Product> | FilterableProductPropsthe selector to create the query from
configFindProductConfigthe config to use for the query

Returns

Object

an object containing the query, relations and free-text search param.

NameType
qstring
queryFindWithoutRelationsOptions
relationskeyof Product[]

Defined in

packages/medusa/src/services/product.ts:908


reorderVariants

reorderVariants(productId, variantOrder): Promise<Product>

Parameters

NameType
productIdstring
variantOrderstring[]

Returns

Promise<Product>

Defined in

packages/medusa/src/services/product.ts:689


retrieve

retrieve(productId, config?): Promise<Product>

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

Parameters

NameTypeDescription
productIdstringid of the product to get.
configFindProductConfigobject that defines what should be included in the query response

Returns

Promise<Product>

the result of the find one operation.

Defined in

packages/medusa/src/services/product.ts:186


retrieveByExternalId

retrieveByExternalId(externalId, config?): Promise<Product>

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

Parameters

NameTypeDescription
externalIdstringhandle of the product to get.
configFindProductConfigdetails about what to get from the product

Returns

Promise<Product>

the result of the find one operation.

Defined in

packages/medusa/src/services/product.ts:230


retrieveByHandle

retrieveByHandle(productHandle, config?): Promise<Product>

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

Parameters

NameTypeDescription
productHandlestringhandle of the product to get.
configFindProductConfigdetails about what to get from the product

Returns

Promise<Product>

the result of the find one operation.

Defined in

packages/medusa/src/services/product.ts:209


retrieveOptionByTitle

retrieveOptionByTitle(title, productId): Promise<undefined | ProductOption>

Retrieve product's option by title.

Parameters

NameTypeDescription
titlestringtitle of the option
productIdstringid of a product

Returns

Promise<undefined | ProductOption>

product option

Defined in

packages/medusa/src/services/product.ts:792


retrieveVariants

retrieveVariants(productId, config?): Promise<ProductVariant[]>

Gets all variants belonging to a product.

Parameters

NameTypeDescription
productIdstringthe id of the product to get variants from.
configFindProductConfigThe config to select and configure relations etc...

Returns

Promise<ProductVariant[]>

an array of variants

Defined in

packages/medusa/src/services/product.ts:288


retrieve_

retrieve_(selector, config?): Promise<Product>

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

Parameters

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

Returns

Promise<Product>

the result of the find one operation.

Defined in

packages/medusa/src/services/product.ts:252


shouldRetryTransaction_

Protected shouldRetryTransaction_(err): boolean

Parameters

NameType
errRecord<string, unknown> | { code: string }

Returns

boolean

Inherited from

TransactionBaseService.shouldRetryTransaction_

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:31


update

update(productId, update): Promise<Product>

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

NameTypeDescription
productIdstringthe id of the product. Must be a string that can be casted to an ObjectId
updateUpdateProductInputan object with the update values.

Returns

Promise<Product>

resolves to the update result.

Defined in

packages/medusa/src/services/product.ts:492


updateOption

updateOption(productId, optionId, data): Promise<Product>

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

NameTypeDescription
productIdstringthe product whose option we are updating
optionIdstringthe id of the option we are updating
dataProductOptionInputthe data to update the option with

Returns

Promise<Product>

the updated product

Defined in

packages/medusa/src/services/product.ts:735


updateShippingProfile

updateShippingProfile(productIds, profileId): Promise<Product[]>

Parameters

NameTypeDescription
productIdsstring | string[]ID or IDs of the products to update
profileIdstringShipping profile ID to update the shipping options with

Returns

Promise<Product[]>

updated shipping options

Defined in

packages/medusa/src/services/product.ts:882


withTransaction

withTransaction(transactionManager?): ProductService

Parameters

NameType
transactionManager?EntityManager

Returns

ProductService

Inherited from

TransactionBaseService.withTransaction

Defined in

packages/medusa/src/interfaces/transaction-base-service.ts:14