Skip to main content

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:
  1. Single Table Inheritance (STI): Import and Export types inherit from base classes
  2. State Machine: Imports progress through states (pending → mapping → processing → completed)
  3. Schema Definition: ImportSchema classes define expected fields and validation
  4. Row Processors: Transform CSV rows into database records
  5. Event-Driven Processing: Background jobs handle heavy lifting asynchronously
  6. Registry Pattern: Types registered in Spree.import_types and Spree.export_types

Exports

Exports generate CSV files from filtered database records.

Built-in Export Types

Export Model

The base Spree::Export class provides:

Creating a Custom Exporter

Step 1: Create the Export Class
app/models/spree/exports/subscriptions.rb
Step 2: Add to_csv Method to Your Model
app/models/spree/subscription.rb
Step 3: Register the Export Type
config/initializers/spree.rb
Step 4: Add Translations
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 via search_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 Class
app/models/spree/imports/subscriptions.rb
Step 2: Define the Schema
app/models/spree/import_schemas/subscriptions.rb
Step 3: Create the Row Processor
app/services/spree/imports/row_processors/subscription.rb
Step 4: Register the Import Type
config/initializers/spree.rb
Step 5: Add Translations
config/locales/en.yml

Products Import Schema

The built-in products import supports these fields: Required Fields:
  • slug - Product URL slug
  • sku - Variant SKU
  • name - Product name
  • price - Variant price
Optional Fields:
  • status - Product status (active/draft/archived)
  • description - Product description
  • meta_title, meta_description, meta_keywords - SEO metadata
  • tags - Product tags
  • compare_at_price - Original price for sale display
  • currency - Price currency
  • width, height, depth, dimensions_unit - Dimensions
  • weight, weight_unit - Weight
  • available_on, discontinue_on - Availability dates
  • track_inventory - Enable inventory tracking
  • inventory_count, inventory_backorderable - Stock settings
  • tax_category, shipping_category - Category assignments
  • image1_src, image2_src, image3_src - Image URLs
  • option1_name, option1_value through option3_name, option3_value - Variant options
  • category1, category2, category3 - Taxon assignments (format: “Taxonomy -> Taxon -> Child Taxon”)

Handling Multi-Variant Products

The products import handles variants intelligently:
  1. Master variant rows (no option values): Create/update the product and its master variant
  2. 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 format metafield.{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 from Spree::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 separate ProcessGroupJob:

ProcessGroupJob

Processes one group of rows and reports completion:

GenerateJob (Exports)

Generates CSV files for exports:

Events

Import and export lifecycle events such as import.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:
Records are filtered by current_ability ensuring users only export data they have access to.