Skip to content
Search
Examples

Schema

Description

withSchema() on a reader declares the column types instead of inferring them. Formats that carry no types (CSV, JSON, XML, arrays) then never guess from a sample, and NOT NULL columns stay NOT NULL.

Documentation

The schema describes every column a frame carries, and gates what a row may hold.

Code

<?php

declare(strict_types=1);

use function Flow\ETL\DSL\{bool_schema, data_frame, from_array, int_schema, schema, str_schema, to_output};
use Flow\ETL\IO\Loader\StreamLoader\Output;
use Flow\ETL\Values\Schema\Metadata;

require __DIR__ . '/vendor/autoload.php';

$schema = schema(
    int_schema('id', nullable: false),
    str_schema('name', nullable: true),
    bool_schema('active', nullable: false, metadata: Metadata::empty()->add('key', 'value')),
);

data_frame()
    ->read(
        from_array([
            ['id' => 1, 'name' => 'Product 1', 'active' => true],
            ['id' => 2, 'name' => 'Product 2', 'active' => false],
            ['id' => 3, 'name' => 'Product 3', 'active' => true],
        ])->withSchema($schema)
    )
    ->collect()
    ->write(to_output(truncate: false, output: Output::schema))
    ->run();
Contributors

Built in the open.

Join us on GitHub
scroll back to top