Feature Recipe: Exporting E-Commerce Data in CSV Format

The plugin showcases core Vendure concepts, including using services and worker bootstrapping for background tasks.
There is a Working Implementation that you can find in our Examples Repository.
This guide walks through building the export functionality in three steps: setting up the plugin structure, implementing the service that handles data processing and CSV generation, and creating a worker script that runs the export operation independently of the main application.
1. Plugin Architecture
The plugin follows Vendure's standard plugin structure. This step registers our custom service with Vendure's dependency injection system.
You can scaffold the basic plugin structure using the Vendure CLI:
This will create a new plugin in the src/plugins
directory. with the following structure:
2. Service Implementation
This step creates the service that handles the actual export logic. The service uses ProductVariantService to fetch variants and StockLevelService to check inventory levels before generating the CSV file.
Add the service to your plugin using the CLI:
This would now create a new service in the src/plugins/ProductInfoPlugin/services
directory and register it with the plugin above.
3. Worker Script Execution
This step creates a standalone script that runs the export operation outside the main application. The script uses Vendure's worker bootstrapping to access services without running the full web server.
And that is it, now you can run the export script from the command line:
This will output the path to the generated CSV file. You can also further extend this export logic.
Extending the Export Logic
The basic export pattern can be extended with additional filtering and data enrichment. You could modify the service to include product categories, pricing information, or supplier details in the export.
For scheduled exports, the worker script can be integrated with cron jobs or Vendure's job queue system. The service method accepts a RequestContext, making it straightforward to call from GraphQL resolvers or other parts of your application.
Different export formats can be supported by abstracting the file generation logic into separate methods, allowing the same data processing to output JSON, XML, or other formats as needed.
Further Reading
Share this article