Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

3.0 KB
Raw Blame
---
title: "Attribute events"
sidebarTitle: "Events"
description: "Events the Product Attribute workflows emit, when they fire, and their payloads."
---

The catalog workflows emit 6 events as attributes and values change. Subscribe to them to reindex storefront facets, sync an external product information system, or start your own workflows. The event names are exported as `ProductAttributeWorkflowEvents` and `ProductAttributeValueWorkflowEvents` from `@mercurjs/core/workflows`.

## Attribute events

Each event fires once per attribute, with that attribute’s id as the payload.

| Event | Emitted when | Payload |
| --- | --- | --- |
| `product-attribute.created` | `createProductAttributesWorkflow` creates attributes | `{ id }` |
| `product-attribute.updated` | `updateProductAttributesWorkflow` updates attributes, including an inline rename from the batch workflow | `{ id }` |
| `product-attribute.deleted` | `deleteProductAttributesWorkflow` deletes attributes, including inline attributes removed from their product | `{ id }` |

## Value events

Each event fires once per value, with that value’s id as the payload.

| Event | Emitted when | Payload |
| --- | --- | --- |
| `product-attribute-value.created` | `createProductAttributeValuesWorkflow` creates values, including new values added to an inline axis | `{ id }` |
| `product-attribute-value.updated` | `updateProductAttributeValuesWorkflow` updates values | `{ id }` |
| `product-attribute-value.deleted` | `deleteProductAttributeValuesWorkflow` deletes values, including values removed from an inline axis | `{ id }` |

## What doesn’t emit

Some writes create or change records without a matching event:

- **Values created with an attribute**: `createProductAttributesWorkflow` emits only `product-attribute.created`, not a value event per value
- **Upserts**: `upsertProductAttributeValuesWorkflow` emits nothing
- **Attaching and detaching**: linking values to a product, `text` and `unit` values created on attach, and option changes on a product emit no attribute events

## Subscribe to an event

A subscriber receives one event per attribute. Resolve `query` to load the record by id.

```ts title="src/subscribers/product-attribute-created.ts"
import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework"
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"

export default async function productAttributeCreated({
  event,
  container,
}: SubscriberArgs<{ id: string }>) {
  const query = container.resolve(ContainerRegistrationKeys.QUERY)
  const { data: [attribute] } = await query.graph({
    entity: "product_attribute",
    fields: ["id", "name", "type", "is_filterable", "values.name"],
    filters: { id: event.data.id },
  })

  container.resolve("logger").info(`Index facet ${attribute.name}`)
}

export const config: SubscriberConfig = {
  event: "product-attribute.created",
}
```

The subscriber loads the new attribute with its values. Swap the log line for a call to your search index.