Skip to content

Commit

Permalink
feat: improve cache of eloquent datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasalexandre9 committed Nov 12, 2024
1 parent 0b033c1 commit 0460b00
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
14 changes: 11 additions & 3 deletions packages/BaseDatasource/src/BaseDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,24 @@ private function makeDoctrineConnection(array $databaseConfig): void
/**
* @codeCoverageIgnore
*/
public function __sleep(): array
public function __serialize(): array
{
return ['collections', 'charts', 'databaseConfig'];
return [
'collections' => $this->collections,
'charts' => $this->charts,
'databaseConfig' => $this->databaseConfig,
];
}

/**
* @codeCoverageIgnore
*/
public function __wakeup(): void
public function __unserialize(array $data): void
{
$this->collections = $data['collections'];
$this->charts = $data['charts'];
$this->databaseConfig = $data['databaseConfig'];

$this->makeOrm($this->databaseConfig);
$this->makeDoctrineConnection($this->databaseConfig);
}
Expand Down
29 changes: 29 additions & 0 deletions packages/DatasourceEloquent/src/EloquentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,33 @@ private function nullablePolymorphicMorphManyRelationFields($record, $relationNa
// Do nothing
}
}

public function __serialize(): array
{
return [
'fields' => $this->fields,
'actions' => $this->actions,
'segments' => $this->segments,
'charts' => $this->charts,
'schema' => $this->schema,
'dataSource' => $this->dataSource,
'name' => $this->name,
'tableName' => $this->tableName,
'model' => $this->model::class,
];
}

public function __unserialize(array $data): void
{
$this->fields = $data['fields'];
$this->actions = $data['actions'];
$this->segments = $data['segments'];
$this->charts = $data['charts'];
$this->schema = $data['schema'];
$this->dataSource = $data['dataSource'];
$this->name = $data['name'];
$this->tableName = $data['tableName'];
$this->model = new $data['model']();
$this->nativeDriver = null;
}
}
22 changes: 22 additions & 0 deletions packages/DatasourceEloquent/src/EloquentDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,26 @@ public function getModels(): array
{
return $this->models;
}

/**
* @codeCoverageIgnore
*/
public function __serialize(): array
{
return array_merge(
parent::__serialize(),
['supportPolymorphicRelations' => $this->supportPolymorphicRelations]
);
}

/**
* @codeCoverageIgnore
*/
public function __unserialize(array $data): void
{
parent::__unserialize($data);

$finder = new ClassFinder(config('projectDir'));
$this->models = $finder->getModelsInNamespace('App');
}
}
4 changes: 0 additions & 4 deletions packages/DatasourceToolkit/src/CollectionMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ public function setSegments(array $segments): Collection
*/
public function addField(string $name, ColumnSchema|RelationSchema $field): void
{
if ($this->fields->has($name)) {
throw new ForestException('Field ' . $name . ' already defined in collection');
}

$this->fields->put($name, $field);
}

Expand Down

0 comments on commit 0460b00

Please sign in to comment.