Skip to main content

Overview

@woltz/rich-domain-export provides powerful multi-format export capabilities for your rich-domain entities. Export data to CSV, JSON, JSON Lines (JSONL), and custom formats with full type safety, custom formatting, and efficient streaming for large datasets.

Multiple Formats

CSV, JSON, JSONL, and extensible for custom formats

Type-Safe Exports

Full TypeScript support with discriminated unions

Streaming Support

Memory-efficient export for large datasets

Progress Tracking

Real-time progress callbacks for long-running exports

Installation

This is a backend-only package (Node.js). For frontend exports, use API endpoints.

Quick Start

Approach 1: Repository Extension

Extend your repository with export capabilities:

Approach 2: Composition with ExportService

Use the standalone service to keep export logic separate:

Supported Formats

CSV Format

Export entities to comma-separated values format:
CSV Options:

JSON Format

Export entities to standard JSON array:
JSON Options:

JSON Lines (JSONL) Format

Export entities to newline-delimited JSON (streaming-friendly):
JSON Lines is ideal for streaming large datasets and is supported by many data processing tools. Learn more at jsonlines.org.

Common Use Cases

Export with Filters

Use Criteria to filter data before export:

Custom Formatters (CSV)

Transform field values before CSV serialization:

Custom Transformers (JSON)

Transform field values with any type (not just strings):

Common Formatters

The library provides pre-built formatters for CSV:
Available formatters:
  • isoDate - ISO 8601 date string
  • localeDate - Locale date string (e.g., “1/1/2024”)
  • localeDateTime - Locale datetime string
  • decimal2 - Number with 2 decimal places
  • currencyUSD - USD currency format ($X.XX)
  • yesNo - Boolean as “Yes” or “No”
  • trueFalse - Boolean as “True” or “False”
  • array - Array as comma-separated string
  • json - Object as JSON string
  • uppercase - Convert to uppercase
  • lowercase - Convert to lowercase
  • trim - Trim whitespace

Progress Tracking

Monitor export progress for large datasets:

Streaming for Large Datasets

For large datasets, use streaming to avoid loading everything into memory:

CSV Stream

HTTP Streaming (Fastify Example)

HTTP Streaming (Express Example)

Performance Considerations

Tips:
  • Use exportStream() for datasets > 10,000 records
  • Use JSON Lines (jsonLines: true) for streaming large JSON exports
  • Adjust batchSize option to control memory usage (default: 1000)

Custom Formats

Extend the library with custom formats using the Strategy Pattern:

Export Statistics

Get detailed statistics about the export operation:

Type Safety

The library provides full type safety with discriminated unions:

Error Handling

The library provides specific error types:

Complete Example

API Reference

ExportableRepository

ExportService

FormatRegistry

ExportOptions

ExportResult