Class: ProductService
Hierarchy
TransactionBaseService
↳
ProductService
Constructors
constructor
• new ProductService(__namedParameters
)
Parameters
Name | Type |
---|---|
__namedParameters | InjectedDependencies |
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
Name | Type |
---|---|
CREATED | string |
DELETED | string |
UPDATED | string |
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
Name | Type | Description |
---|---|---|
productId | string | the product to apply the new option to |
optionTitle | string | the 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
Name | Type | Description |
---|---|---|
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
Name | Type | Description |
---|---|---|
selector | Selector <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
Name | Type | Description |
---|---|---|
productObject | CreateProductInput | the 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
Name | Type | Description |
---|---|---|
productId | string | the 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
Name | Type | Description |
---|---|---|
productId | string | the product to delete an option from |
optionId | string | the 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
Name | Type |
---|---|
productIds | string [] |
salesChannelId | string |
config | FindProductConfig |
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
Name | Type | Description |
---|---|---|
id | string | product id |
salesChannelIds | string [] | 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
Name | Type | Description |
---|---|---|
selector | ProductSelector | an object that defines rules to filter products by |
config | FindProductConfig | object 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
Name | Type | Description |
---|---|---|
selector | ProductSelector | an object that defines rules to filter products by |
config | FindProductConfig | object 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
Name | Type | Default value |
---|---|---|
count | number | 10 |
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
Name | Type | Description |
---|---|---|
selector | Selector <Product > | FilterableProductProps | the selector to create the query from |
config | FindProductConfig | the config to use for the query |
Returns
Object
an object containing the query, relations and free-text search param.
Name | Type |
---|---|
q | string |
query | FindWithoutRelationsOptions |
relations | keyof Product [] |
Defined in
packages/medusa/src/services/product.ts:908
reorderVariants
▸ reorderVariants(productId
, variantOrder
): Promise
<Product
>
Parameters
Name | Type |
---|---|
productId | string |
variantOrder | string [] |
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
Name | Type | Description |
---|---|---|
productId | string | id of the product to get. |
config | FindProductConfig | object 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
Name | Type | Description |
---|---|---|
externalId | string | handle of the product to get. |
config | FindProductConfig | details 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
Name | Type | Description |
---|---|---|
productHandle | string | handle of the product to get. |
config | FindProductConfig | details 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
Name | Type | Description |
---|---|---|
title | string | title of the option |
productId | string | id 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
Name | Type | Description |
---|---|---|
productId | string | the id of the product to get variants from. |
config | FindProductConfig | The 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
Name | Type | Description |
---|---|---|
selector | Selector <Product > | selector object |
config | FindProductConfig | object 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
Name | Type |
---|---|
err | Record <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
Name | Type | Description |
---|---|---|
productId | string | the id of the product. Must be a string that can be casted to an ObjectId |
update | UpdateProductInput | an 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
Name | Type | Description |
---|---|---|
productId | string | the product whose option we are updating |
optionId | string | the id of the option we are updating |
data | ProductOptionInput | the 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
Name | Type | Description |
---|---|---|
productIds | string | string [] | ID or IDs of the products to update |
profileId | string | Shipping 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
Name | Type |
---|---|
transactionManager? | EntityManager |
Returns
Inherited from
TransactionBaseService.withTransaction
Defined in
packages/medusa/src/interfaces/transaction-base-service.ts:14