Skip to main content

1. Overview

This service provides a simple, powerful, and extensible ETL (Extract, Transform, Load) solution for Laravel applications, specifically designed to handle nested documents from a MongoDB database. The core philosophy is to be schema-driven. A simple YAML file defines the data structure, validation rules, API visibility, and even frontend behavior for a given data model. This creates a “single source of truth” that simplifies development and ensures consistency across your application. Key Features:
  • Schema-Driven: Define your data models once in a YAML file.
  • MongoDB-Friendly: Easily import and export complex documents with nested objects and arrays.
  • User-Friendly Templates: Uses a simple “parent row” convention in spreadsheets, making it easy for non-technical users to populate data with nested items.
  • Extensible Drivers: Supports both maatwebsite/excel (feature-rich) and openspout (high-performance) for a balance of features and speed.
  • Fluent Export API: A clean, chainable interface for building and dispatching complex export jobs (e.g., Etl::export()->from(...)->chunk(...)).
  • Integrated Validation: The schema can define Laravel validation rules, which can be used in your Form Requests.
  • API & UI Generation: The schema contains metadata to dynamically control API resources and render frontend forms.

2. Core Concepts

2.1. The Schema (.yml file)

The schema is the heart of the service. It’s a YAML file (e.g., user_schema.yml) that defines everything about a data model.

2.2. The Spreadsheet Format

To represent a nested MongoDB document in a flat spreadsheet, we use a “parent row” convention.
  • Headers: Column headers are generated from the schema’s header or field key. Nested object fields use dot notation (e.g., settings.theme).
  • Parent Row: The first row for a document contains all the main data and the data for the first item in the primary embedded array.
  • Child Rows: To add more items to the embedded array, add new rows directly below the parent row. On these child rows, leave the parent columns blank. This signals to the importer that they belong to the document above.

3. Directory Structure


4. How to Use

4.1. Importing Data

In a controller, use the Etl facade to select a driver and process the uploaded file.

4.2. Exporting Data (Fluent API)

Use the fluent export() method to build and dispatch export jobs. Simple Export:
Paginated (Chunked) Export:

4.3. Testing the Pipeline

Use the provided Artisan command to test the full export/import cycle.

5. Full Source Code

Here is the complete source code for every class in the service.

5.1. Configuration Schema

File: config/etl_schemas/user_schema.yml

5.2. Contracts (Interfaces)

File: app/Services/Etl/Contracts/ImportDriver.php
File: app/Services/Etl/Contracts/ExportDriver.php

5.3. Core Services & Builders

File: app/Services/Etl/SchemaService.php
File: app/Services/Etl/EtlManager.php
File: app/Services/Etl/ExportBuilder.php

5.4. Drivers

File: app/Services/Etl/Drivers/Imports/StandardSchemaImportDriver.php
File: app/Services/Etl/Drivers/Imports/OpenSpoutSchemaImportDriver.php
File: app/Services/Etl/Drivers/Exports/StandardSchemaExportDriver.php
File: app/Services/Etl/Drivers/Exports/OpenSpoutSchemaExportDriver.php

5.5. Service Integration & Testing

File: app/Services/Etl/Etl.php
File: app/Providers/EtlServiceProvider.php
File: config/app.php (Add this to the aliases array)
File: app/Console/Commands/Test/EtlTestPipeline.php

Addendum A: CSV Import Functionality

This section details the necessary additions to support importing data from .csv files.

1. Conceptual Changes

To handle CSV files, a new import driver, OpenSpoutCsvSchemaImportDriver, has been created. This driver uses the same schema-driven logic as the XLSX driver but is specifically configured to read and parse the CSV format using the high-performance OpenSpout library. The CSV file must adhere to the same “parent-child row” convention. A child row is indicated by leaving the parent columns empty, which translates to consecutive commas at the beginning of the line. Example users.csv format:

2. Directory Structure Updates

A new driver has been added to the Imports directory:

3. New Driver Source Code

File: app/Services/Etl/Drivers/Imports/OpenSpoutCsvSchemaImportDriver.php

4. Service Manager Update

The EtlManager must be updated to register the new driver. File: app/Services/Etl/EtlManager.php

5. Usage Example

To use the new driver, simply call it by its key, open_spout_csv_schema_import.