- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
3.1.2. Define Workflow to Create a Brand
Workflows vs Services: Why use Workflows?#
When manipulating data, use workflows instead of invoking a service's methods directly in your API route or other customizations.
Workflows eliminate data inconsistency in your application with its compensation mechanism that undoes changes if an error occurs. For example, if a workflow's step creates a brand, it also defines a compensation mechanism to remove the brand if an error occurs.
This is even more useful when you create workflows with many steps, or integrate third-party systems.
Create createBrandWorkflow#
Create the file src/workflows/create-brand/index.ts
with the following content:
For now, this workflow only defines its input. You'll create its step and use it in the workflow.
Create createBrandStep#
Create the file src/workflows/create-brand/steps/create-brand.ts
with the following content:
7import BrandModuleService from "../../../modules/brand/service"8 9export const createBrandStep = createStep(10 "create-brand-step",11 async (input: CreateBrandInput, { container }) => {12 const brandModuleService: BrandModuleService = container.resolve(13 BRAND_MODULE14 )15 16 const brand = await brandModuleService.createBrands(input)17 18 return new StepResponse(brand, brand.id)19 }20)
This defines a createBrandStep
. In the step, you resolve the Brand Module's main service and use its generated createBrands
method, which accepts one or more objects of brands to create.
The step returns the created brand in the first parameter of the StepResponse
's constructor.
Add Compensation Function to Step#
A compensation function rolls back changes made by the step if an error occurs in the workflow.
The second parameter of the StepResponse
's constructor is passed to the compensation function.
To add the compensation function, pass a third parameter to createStep
:
You resolve the Brand Module's main service and use its generated deleteBrands
method to delete the brand created by the step.
So, when an error occurs during the workflow, the brand that was created by the step is deleted to maintain data consistency.
Add Step to Workflow#
Go back to the workflow at src/workflows/create-brand/index.ts
and import the step you created:
Then, replace the TODO
with the following:
You use the createBrandStep
to create the brand and return it in the workflow's response.
Next Step: Create Brand API Route#
In the next step, you'll create an API route that allows admin users to create a brand using this workflow.