docs: fixes and improvements to Sanity guide (#10414)
This commit is contained in:
@@ -680,6 +680,7 @@ import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
|
||||
import { ProductDTO } from "@medusajs/framework/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
promiseAll,
|
||||
} from "@medusajs/framework/utils"
|
||||
import SanityModuleService from "../../../modules/sanity/service"
|
||||
import { SANITY_MODULE } from "../../../modules/sanity"
|
||||
@@ -694,15 +695,15 @@ export const syncStep = createStep(
|
||||
const sanityModule: SanityModuleService = container.resolve(SANITY_MODULE)
|
||||
const query = container.resolve(ContainerRegistrationKeys.QUERY)
|
||||
|
||||
const total = 0
|
||||
let total = 0;
|
||||
const upsertMap: {
|
||||
before: any
|
||||
after: any
|
||||
}[] = []
|
||||
|
||||
const batchSize = 200
|
||||
const hasMore = true
|
||||
const offset = 0
|
||||
const batchSize = 200;
|
||||
let hasMore = true;
|
||||
let offset = 0;
|
||||
let filters = input.product_ids ? {
|
||||
id: input.product_ids
|
||||
} : {}
|
||||
@@ -760,54 +761,39 @@ Notice that you pass `sanity_product.*` in the `fields` array. Medusa will retri
|
||||
}
|
||||
```
|
||||
|
||||
Next, you want to sync the retrieved products. So, replace the `TODO` with the following:
|
||||
Next, you want to sync the retrieved products. So, replace the `TODO` in the `while` loop with the following:
|
||||
|
||||
```ts title="src/workflows/sanity-sync-products/steps/sync.ts"
|
||||
// other imports...
|
||||
import {
|
||||
while (hasMore) {
|
||||
// ...
|
||||
promiseAll,
|
||||
} from "@medusajs/framework/utils"
|
||||
try {
|
||||
await promiseAll(
|
||||
products.map(async (prod) => {
|
||||
const after = await sanityModule.upsertSyncDocument(
|
||||
"product",
|
||||
prod as ProductDTO
|
||||
);
|
||||
|
||||
export const syncStep = createStep(
|
||||
{ name: "sync-step", async: true },
|
||||
async (input: SyncStepInput, { container }) => {
|
||||
// ...
|
||||
upsertMap.push({
|
||||
// @ts-ignore
|
||||
before: prod.sanity_product,
|
||||
after
|
||||
})
|
||||
|
||||
while (hasMore) {
|
||||
// ...
|
||||
try {
|
||||
await promiseAll(
|
||||
products.map(async (prod) => {
|
||||
const after = await sanityModule.upsertSyncDocument(
|
||||
"product",
|
||||
prod as ProductDTO
|
||||
);
|
||||
|
||||
upsertMap.push({
|
||||
// @ts-ignore
|
||||
before: prod.sanity_product,
|
||||
after
|
||||
})
|
||||
|
||||
return after
|
||||
}),
|
||||
)
|
||||
} catch (e) {
|
||||
return StepResponse.permanentFailure(
|
||||
`An error occurred while syncing documents: ${e}`,
|
||||
upsertMap
|
||||
)
|
||||
}
|
||||
|
||||
offset += batchSize
|
||||
hasMore = offset < count
|
||||
total += products.length
|
||||
}
|
||||
|
||||
return new StepResponse({ total }, upsertMap)
|
||||
return after
|
||||
}),
|
||||
)
|
||||
} catch (e) {
|
||||
return StepResponse.permanentFailure(
|
||||
`An error occurred while syncing documents: ${e}`,
|
||||
upsertMap
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
offset += batchSize
|
||||
hasMore = offset < count
|
||||
total += products.length
|
||||
}
|
||||
```
|
||||
|
||||
In the `while` loop, you loop over the array of products to sync them to Sanity. You use the `promiseAll` Medusa utility that loops over an array of promises and ensures that all transactions within these promises are rolled back in case an error occurs.
|
||||
@@ -816,6 +802,12 @@ For each product, you upsert it into Sanity, then push its document before and a
|
||||
|
||||
You also wrap the `promiseAll` function within a try-catch block. In the catch block, you invoke and return `StepResponse.permanentFailure` which indicates that the step has failed but still invokes the rollback mechanism that you'll implement in a bit. The first parameter of `permanentFailure` is the error message, and the second is the data to use in the rollback mechanism.
|
||||
|
||||
Finally, after the `while` loop and at the end of the step, add the following return statement:
|
||||
|
||||
```ts title="src/workflows/sanity-sync-products/steps/sync.ts"
|
||||
return new StepResponse({ total }, upsertMap);
|
||||
```
|
||||
|
||||
If no errors occur, the step returns an instance of `StepResponse`, which must be returned by any step. It accepts as a first parameter the data to return to the workflow that executed this step.
|
||||
|
||||
#### Add Compensation Function
|
||||
|
||||
@@ -3245,7 +3245,7 @@ export const generatedEditDates = {
|
||||
"references/types/HttpTypes/interfaces/types.HttpTypes.AdminBatchProductVariantRequest/page.mdx": "2024-11-25T17:49:26.851Z",
|
||||
"references/types/WorkflowTypes/ProductWorkflow/interfaces/types.WorkflowTypes.ProductWorkflow.ExportProductsDTO/page.mdx": "2024-11-12T09:36:24.232Z",
|
||||
"app/contribution-guidelines/admin-translations/page.mdx": "2024-11-14T08:54:15.369Z",
|
||||
"app/integrations/guides/sanity/page.mdx": "2024-11-27T10:10:12.100Z",
|
||||
"app/integrations/guides/sanity/page.mdx": "2024-12-03T14:14:11.347Z",
|
||||
"references/api_key/types/api_key.FindConfigOrder/page.mdx": "2024-11-25T17:49:28.715Z",
|
||||
"references/auth/types/auth.FindConfigOrder/page.mdx": "2024-11-25T17:49:28.887Z",
|
||||
"references/cart/types/cart.FindConfigOrder/page.mdx": "2024-11-25T17:49:29.455Z",
|
||||
|
||||
Reference in New Issue
Block a user