docs: fixes and improvements to Sanity guide (#10414)

This commit is contained in:
Shahed Nasser
2024-12-04 17:31:20 +02:00
committed by GitHub
parent 340769595a
commit 665eea8e75
2 changed files with 39 additions and 47 deletions
@@ -680,6 +680,7 @@ import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
import { ProductDTO } from "@medusajs/framework/types" import { ProductDTO } from "@medusajs/framework/types"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
promiseAll,
} from "@medusajs/framework/utils" } from "@medusajs/framework/utils"
import SanityModuleService from "../../../modules/sanity/service" import SanityModuleService from "../../../modules/sanity/service"
import { SANITY_MODULE } from "../../../modules/sanity" import { SANITY_MODULE } from "../../../modules/sanity"
@@ -694,15 +695,15 @@ export const syncStep = createStep(
const sanityModule: SanityModuleService = container.resolve(SANITY_MODULE) const sanityModule: SanityModuleService = container.resolve(SANITY_MODULE)
const query = container.resolve(ContainerRegistrationKeys.QUERY) const query = container.resolve(ContainerRegistrationKeys.QUERY)
const total = 0 let total = 0;
const upsertMap: { const upsertMap: {
before: any before: any
after: any after: any
}[] = [] }[] = []
const batchSize = 200 const batchSize = 200;
const hasMore = true let hasMore = true;
const offset = 0 let offset = 0;
let filters = input.product_ids ? { let filters = input.product_ids ? {
id: 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" ```ts title="src/workflows/sanity-sync-products/steps/sync.ts"
// other imports... while (hasMore) {
import {
// ... // ...
promiseAll, try {
} from "@medusajs/framework/utils" await promiseAll(
products.map(async (prod) => {
const after = await sanityModule.upsertSyncDocument(
"product",
prod as ProductDTO
);
export const syncStep = createStep( upsertMap.push({
{ name: "sync-step", async: true }, // @ts-ignore
async (input: SyncStepInput, { container }) => { before: prod.sanity_product,
// ... after
})
while (hasMore) { return after
// ... }),
try { )
await promiseAll( } catch (e) {
products.map(async (prod) => { return StepResponse.permanentFailure(
const after = await sanityModule.upsertSyncDocument( `An error occurred while syncing documents: ${e}`,
"product", upsertMap
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)
} }
)
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. 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. 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. 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 #### Add Compensation Function
+1 -1
View File
@@ -3245,7 +3245,7 @@ export const generatedEditDates = {
"references/types/HttpTypes/interfaces/types.HttpTypes.AdminBatchProductVariantRequest/page.mdx": "2024-11-25T17:49:26.851Z", "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", "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/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/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/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", "references/cart/types/cart.FindConfigOrder/page.mdx": "2024-11-25T17:49:29.455Z",