cbytedigital/laravel-bi-data-export

Laravel包,用于轻松且定期导出数据以供BI用途。内置功能,可以排除或编辑列,并支持在Laravel文件系统磁盘上导出到CSV格式。

1.0.2 2024-07-24 10:28 UTC

This package is auto-updated.

Last update: 2024-09-24 12:16:10 UTC


README

PHP from Packagist Latest Version on Packagist Software License Total Downloads

一个Laravel包,可以用于轻松且定期导出大量数据集以供BI用途。包括内置功能,可以排除或编辑列,并在Laravel文件系统磁盘上导出到CSV格式。

该包使用数据库游标和流式传输来支持处理大量数据集,并尽可能减少对可怜的Web服务器的内存使用。

安装

使用composer安装此包

$ composer require cbytedigital/laravel-bi-data-export

可选:服务提供器将自动注册。或者,您可以在config/app.php文件中手动添加服务提供器

'providers' => [
    // ...
    CbyteDigital\BiDataExport\BiDataExportServiceProvider::class,
];

您应该使用以下命令发布配置

php artisan vendor:publish --provider="CbyteDigital\BiDataExport\BiDataExportServiceProvider"

用法

目前,仅支持导出到.CSV格式。这是导出大量数据集最常用的方法。幸运的是,您可以编写自己的导出作业实现并参考配置中的类。

导出可以直接在请求中(同步)使用,但建议使用后台工作者和队列。

要包含导出的模型,请配置模型使用BiExportable特质。

class Client extends Model
{
    use BiExportable;
}

如果需要,可以在模型中定义选定的和/或隐藏的字段如下(可选)

class Client extends Model
{
    use BiExportable;
    
    // Default behaviour
    public $biExportable = '*';

    // Or select specific columns
    public $biExportable = [
        'id',
        'username'
    ];

    // Values of hidden fields will be replaced
    public $biHidden = [
        'first_name',
        'last_name'
    ];

    // Define the placeholder value for hidden fields.
    // If not defined will resort to using the variable defined in the config.
    public $biHiddenText = 'REDACTED';
}

如果您需要导出例如一个关联表(通常没有专门的模型),您可以手动将表和所需的列/配置添加到配置文件中用于导出。

<?php

return [
    /**
     * Define models for exporting
     * ...
     */
    'models' => [
        \App\Models\Model::class
    ],

    /**
     * Define tables for exporting
     * ...
     */
    'tables' => [
        'partners' => [
            'columns' => '*'
        ]
    ],

    /**
     * Determines the export action. You can define your own implementation here.
     */
    'export_job' => CbyteDigital\BiDataExport\Jobs\ExportBiToCsv::class,

    /**
     * Determines the export location.
     */
    'export_disk' => env('BI_EXPORT_DISK', 's3'),

    /**
     * CSV export job delimiter value
     */
    'export_csv_delimiter' => env('BI_EXPORT_CSV_DELIMITER', ';'),

    /**
     * Default replacement value if not overwritten by the model or tables config.
     */
    'default_hidden_text' => env('BI_HIDDEN_TEXT', 'REDACTED'),

    /**
     * Ability to add a prefix to the filename. For example: {prefix}table.sql
     */
    'filename_prefix' => env('BI_FILENAME_PREFIX'),

    /**
     * Ability to add a suffix to the filename. For example: table{suffix}.sql
     */
    'filename_suffix' => env('BI_FILENAME_SUFFIX')
];

添加计划任务的导出命令

$schedule->command(ExportBiDataCommand::class)->dailyAt('23:00');

或手动调用

php artisan bi:export-data

测试

使用以下命令运行测试

$ composer test

支持

Postcardware

此包完全免费使用。如果它进入您的生产环境,我们将非常感激您从家乡给我们寄一张明信片!👏🏼

我们的地址是:CBYTE Software B.V.,Heuvelkamp 2a,6658DE Beneden-Leeuwen,荷兰。

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。