Overview
Spree provides a comprehensive bulk data import and export system for managing large datasets. The system supports CSV file processing with configurable field mapping, asynchronous processing via background jobs, and real-time progress tracking in the admin interface.Import/Export System Diagram
Architecture
The import/export system uses several design patterns:- Single Table Inheritance (STI): Import and Export types inherit from base classes
- State Machine: Imports progress through states (pending → mapping → processing → completed)
- Schema Definition: ImportSchema classes define expected fields and validation
- Row Processors: Transform CSV rows into database records
- Event-Driven Processing: Background jobs handle heavy lifting asynchronously
- Registry Pattern: Types registered in
Spree.import_typesandSpree.export_types
Exports
Exports generate CSV files from filtered database records.Built-in Export Types
Export Model
The baseSpree::Export class provides:
Creating a Custom Exporter
Step 1: Create the Export Classapp/models/spree/exports/subscriptions.rb
to_csv Method to Your Model
app/models/spree/subscription.rb
config/initializers/spree.rb
config/locales/en.yml
Multi-line Exports
For exports where each record produces multiple rows (like products with variants):app/models/spree/exports/orders_with_items.rb
app/models/spree/order.rb
Export Filtering
Exports support Ransack filtering viasearch_params:
Imports
Imports process CSV files to create or update database records.Built-in Import Types
Import Workflow
Import Components
Import Model
Import Schema
Defines expected CSV fields:Import Mapping
Maps CSV columns to schema fields:Import Row
Represents a single CSV row:Row Processor
Transforms row data into database records:Creating a Custom Importer
Step 1: Create the Import Classapp/models/spree/imports/subscriptions.rb
app/models/spree/import_schemas/subscriptions.rb
app/services/spree/imports/row_processors/subscription.rb
config/initializers/spree.rb
config/locales/en.yml
Products Import Schema
The built-in products import supports these fields: Required Fields:slug- Product URL slugsku- Variant SKUname- Product nameprice- Variant price
status- Product status (active/draft/archived)description- Product descriptionmeta_title,meta_description,meta_keywords- SEO metadatatags- Product tagscompare_at_price- Original price for sale displaycurrency- Price currencywidth,height,depth,dimensions_unit- Dimensionsweight,weight_unit- Weightavailable_on,discontinue_on- Availability datestrack_inventory- Enable inventory trackinginventory_count,inventory_backorderable- Stock settingstax_category,shipping_category- Category assignmentsimage1_src,image2_src,image3_src- Image URLsoption1_name,option1_valuethroughoption3_name,option3_value- Variant optionscategory1,category2,category3- Taxon assignments (format: “Taxonomy -> Taxon -> Child Taxon”)
Handling Multi-Variant Products
The products import handles variants intelligently:- Master variant rows (no option values): Create/update the product and its master variant
- Non-master variant rows (with option values): Create additional variants for an existing product
Metafield Support
Both imports and exports support metafields dynamically: Export: Metafield definitions are automatically added as CSV columns using the formatmetafield.{namespace}.{key}.
Import: Map CSV columns to metafield definitions. The system automatically detects columns matching the metafield pattern and updates the corresponding metafield values.
Background Jobs
CreateRowsJob
Parses CSV and creates ImportRow records: All import jobs inherit fromSpree::Imports::BaseJob, which sets queue_as Spree.queues.imports once and is shared across the pipeline.
ProcessRowsJob
Fans the pending rows out into groups, each processed by a separateProcessGroupJob:
ProcessGroupJob
Processes one group of rows and reports completion:GenerateJob (Exports)
Generates CSV files for exports:Events
Import and export lifecycle events such asimport.completed and export.created can be delivered as webhooks to react when an asynchronous import or export finishes.
Import Events
Export Events
Import Row Events
Configuration
Queue Configuration
config/initializers/spree.rb
Preferences
Imports support configurable delimiter:Key Files Reference
Permissions
Access is controlled via CanCanCan:current_ability ensuring users only export data they have access to.
Related Documentation
- Admin imports/exports endpoints — REST routes and required scopes to trigger imports and exports programmatically.
- Admin SDK resources — typed TypeScript client for driving these back-office operations.

