leyton/clevexport

一种聪明的异步导出大量数据的机制

v1.1 2022-12-26 14:32 UTC

This package is auto-updated.

Last update: 2024-09-26 13:06:11 UTC


README

此 Laravel 包的目的是执行导出,以避免服务器崩溃或超时的大数据/记录。思路是将过程分解为可管理和易于执行的小操作。

安装

compose require leyton/clevexport

安装后,请确保发布资产

php artisan vendor:publish --provider="Leyton\ClevExportServiceProvider"

您将找到包含所需所有配置的 config/clevexport.php 文件。

return [
    // array of listeners that will be executed once the execution is done
    'listeners' => [], 
    
    // if you want to stock the user who started the export
    'with_owner' => true,
    
    // the guard    
    'guard' => 'web',
    
    // the foreign key name in the exports table
    'owner_id' => 'user_id',
    
    // The Authenticable class
    'owner_class' => \App\Models\User:class,
    
    // Number of chunks
    'chunks' => 10,
];

然后您可以运行您的迁移

php artisan migrate

用法

QueryFinder 应该提供一个实现了 IseExportable 接口的对象实例。

 $dossierExporter = QueryFinder::getInstance($this->defaultDossierService, $this->transformer);

 PreparingExportJob::dispatch($dossierExporter, $request->all())->delay(now()->addSecond());

第二个参数是可选的,如果提供,则应实现 ShouldHandleResult。这是您可以对结果执行额外操作并提供 ExportTransformed 容器中头部的位置。如果未提供第二个参数,则头部将是查询中选择的列名。

可导出

class UserExportable implements IsExportable
{

    /**
     * @param array $params
     * @return Builder
     */
    public function query(array $params): Builder
    {
        return  User::select('id', 'name', 'email')
            ->addSelect([
                'title' => Post::selectRaw('SUBSTRING(`content`, 1, 10) as `title`')->limit(1)
            ]);
    }
}

转换器

class UserTransformer implements ShouldHandleResult
{

    public function transform($data): ExportTransformed
    {
        $data =   $data->map(function(User $user){
            $user->most_commented = optional($user->posts()->withCount('comments')->first())->comments_count;
            $user->comments_count = $user->comments()->count();
            $user->posts_count = $user->posts()->count();
            return $user;
        });

        return  new ExportTransformed(['id', 'name', 'email', 'title', 'Most commented', 'Comments count', 'Posts count'], $data->toArray());
    }
}