Skip to content

Commit 4159c4f

Browse files
authored
Merge pull request #17 from yajra/lazy-cursor
Add option to use cursor or lazy to iterate with the results
2 parents 4850d2c + 4471e0c commit 4159c4f

File tree

4 files changed

+94
-19
lines changed

4 files changed

+94
-19
lines changed

src/Commands/DataTablesPurgeExportCommand.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace Yajra\DataTables\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Facades\Log;
68
use Illuminate\Support\Facades\Storage;
9+
use Illuminate\Support\Str;
710

811
class DataTablesPurgeExportCommand extends Command
912
{
@@ -28,10 +31,14 @@ class DataTablesPurgeExportCommand extends Command
2831
*/
2932
public function handle()
3033
{
31-
collect(Storage::listContents('exports'))
32-
->each(function ($file) {
33-
if ($file['timestamp'] < now()->subDay(config('datatables-export.purge.days'))->getTimestamp()) {
34-
Storage::delete($file['path']);
34+
$disk = config('datatables-export.disk', 'local');
35+
$timestamp = now()->subDay(config('datatables-export.purge.days'))->getTimestamp();
36+
37+
collect(Storage::disk($disk)->files())
38+
->each(function ($file) use ($timestamp, $disk) {
39+
$path = Storage::disk($disk)->path($file);
40+
if (File::lastModified($path) < $timestamp && Str::endsWith(strtolower($file), ['xlsx', 'csv'])) {
41+
File::delete($path);
3542
}
3643
});
3744

src/Jobs/DataTableExportJob.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
use Illuminate\Queue\SerializesModels;
1717
use Illuminate\Support\Arr;
1818
use Illuminate\Support\Facades\Auth;
19+
use Illuminate\Support\Facades\File;
20+
use Illuminate\Support\Facades\Storage;
1921
use Illuminate\Support\Str;
2022
use PhpOffice\PhpSpreadsheet\Shared\Date;
23+
use Yajra\DataTables\Exceptions\Exception;
2124
use Yajra\DataTables\Html\Column;
2225
use Yajra\DataTables\Services\DataTable;
2326

@@ -53,6 +56,9 @@ public function __construct(array $dataTable, array $request, $user = null)
5356
* Execute the job.
5457
*
5558
* @return void
59+
* @throws \Box\Spout\Common\Exception\IOException
60+
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
61+
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
5662
*/
5763
public function handle()
5864
{
@@ -71,19 +77,30 @@ public function handle()
7177
$dataTable->skipPaging();
7278

7379
$type = Str::startsWith(request('exportType'), Type::CSV) ? Type::CSV : Type::XLSX;
80+
$disk = config('datatables-export.disk', 'local');
81+
$filename = $this->batchId.'.'.$type;
82+
83+
$path = Storage::disk($disk)->path($filename);
84+
7485
$writer = WriterEntityFactory::createWriter($type);
75-
$writer->openToFile(storage_path('app/exports/' . $this->batchId . '.' . $type));
86+
$writer->openToFile($path);
7687

7788
$columns = $oTable->html()->getColumns()->filter->exportable;
7889
$writer->addRow(
7990
WriterEntityFactory::createRowFromArray(
80-
$columns->map(fn($column) => strip_tags($column['title']))->toArray()
91+
$columns->map(fn ($column) => strip_tags($column['title']))->toArray()
8192
)
8293
);
8394

84-
foreach ($dataTable->getFilteredQuery()->lazy() as $row) {
95+
if (config('datatables-export.method', 'lazy') === 'lazy') {
96+
$query = $dataTable->getFilteredQuery()->lazy(config('datatables-export.chunk', 1000));
97+
} else {
98+
$query = $dataTable->getFilteredQuery()->cursor();
99+
}
100+
101+
foreach ($query as $row) {
85102
$cells = collect();
86-
$columns->map(function (Column $column, $index) use ($row, $cells) {
103+
$columns->map(function (Column $column) use ($row, $cells) {
87104
$property = $column['data'];
88105
$value = Arr::get($row, $property, '');
89106

@@ -117,7 +134,7 @@ public function handle()
117134
*/
118135
protected function wantsDateFormat(Column $column): bool
119136
{
120-
if (!isset($column['exportFormat'])) {
137+
if (! isset($column['exportFormat'])) {
121138
return false;
122139
}
123140

src/Livewire/ExportButtonComponent.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public function updateExportProgress()
4848

4949
public function downloadExport()
5050
{
51-
return Storage::download('exports/'.$this->batchJobId.'.'.$this->getType(), $this->getFilename());
51+
$disk = config('datatables-export.disk', 'local');
52+
53+
return Storage::disk($disk)->download($this->batchJobId.'.'.$this->getType(), $this->getFilename());
5254
}
5355

5456
public function render()

src/config/datatables-export.php

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,58 @@
44

55
return [
66

7-
/**
8-
* Default export format for date.
9-
*/
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Method
10+
|--------------------------------------------------------------------------
11+
|
12+
| Method to use to iterate with the query results.
13+
| Options: lazy, cursor
14+
|
15+
| @link https://laravel.com/docs/eloquent#cursors
16+
| @link https://laravel.com/docs/eloquent#chunking-using-lazy-collections
17+
|
18+
*/
19+
'method' => 'lazy',
20+
21+
/*
22+
|--------------------------------------------------------------------------
23+
| Chunk Size
24+
|--------------------------------------------------------------------------
25+
|
26+
| Chunk size to be used when using lazy method.
27+
|
28+
*/
29+
'chunk' => 1000,
30+
31+
/*
32+
|--------------------------------------------------------------------------
33+
| Export filesystem disk
34+
|--------------------------------------------------------------------------
35+
|
36+
| Export filesystem disk where generated files will be stored.
37+
|
38+
*/
39+
'disk' => 'local',
40+
41+
/*
42+
|--------------------------------------------------------------------------
43+
| Default Date Format
44+
|--------------------------------------------------------------------------
45+
|
46+
| Default export format for date.
47+
|
48+
*/
1049
'default_date_format' => 'yyyy-mm-dd',
1150

12-
/**
13-
* List of valid date formats to be used for auto-detection.
14-
*/
51+
/*
52+
|--------------------------------------------------------------------------
53+
| Valid Date Formats
54+
|--------------------------------------------------------------------------
55+
|
56+
| List of valid date formats to be used for auto-detection.
57+
|
58+
*/
1559
'date_formats' => [
1660
'mm/dd/yyyy',
1761
NumberFormat::FORMAT_DATE_DATETIME,
@@ -37,9 +81,14 @@
3781
NumberFormat::FORMAT_DATE_YYYYMMDDSLASH,
3882
],
3983

40-
/**
41-
* Purge all exported by purge.days old files.
42-
*/
84+
/*
85+
|--------------------------------------------------------------------------
86+
| Purge Options
87+
|--------------------------------------------------------------------------
88+
|
89+
| Purge all exported by purge.days old files.
90+
|
91+
*/
4392
'purge' => [
4493
'days' => 1,
4594
],

0 commit comments

Comments
 (0)