Files
lucent-laravel/src/Revision/RevisionRepo.php
T

111 lines
2.8 KiB
PHP
Raw Normal View History

2023-10-02 23:10:49 +03:00
<?php
namespace Lucent\Revision;
2024-09-07 13:22:58 +03:00
use Lucent\Database\Database;
2023-10-15 19:14:07 +03:00
use Lucent\Edge\Edge;
2023-10-02 23:10:49 +03:00
use Lucent\Primitive\Collection;
2024-08-19 17:48:10 +03:00
use Lucent\Record\FileData;
2023-10-15 19:14:07 +03:00
use Lucent\Record\RecordData;
use Lucent\Record\System;
2023-10-02 23:10:49 +03:00
use PhpOption\Option;
2023-10-15 19:14:07 +03:00
use stdClass;
2023-10-02 23:10:49 +03:00
class RevisionRepo
{
private string $table = "revisions";
public function create(Revision $revision): string
{
2023-10-15 19:14:07 +03:00
$revisionDB = $this->toDB($revision);
2024-09-07 13:22:58 +03:00
Database::make()->table($this->table)->insert($revisionDB);
2023-10-02 23:10:49 +03:00
return $revision->id;
}
/**
* @return Collection<Revision>
**/
public function getByRecordId(string $rid): Collection
{
2024-09-07 13:22:58 +03:00
$revisions = Database::make()->table($this->table)
2023-10-02 23:10:49 +03:00
->where("recordId", $rid)
->get()
2023-10-15 19:14:07 +03:00
->map([$this, 'fromDB'])
2023-10-02 23:10:49 +03:00
->sortByDesc("_sys.version")
->toArray();
return new Collection($revisions);
}
2023-10-15 19:14:07 +03:00
public function cleanupRecord(string $rid, int $numKeep): void
{
2024-09-07 13:22:58 +03:00
$revisionIds = Database::make()->table($this->table)
2023-10-15 19:14:07 +03:00
->where("recordId", $rid)
->orderBy("_sys->version", "desc")
->limit(100)
->skip($numKeep)
->get()
->pluck("id");
2024-09-07 13:22:58 +03:00
Database::make()->table($this->table)
2023-10-15 19:14:07 +03:00
->whereIn("id", $revisionIds)
->delete();
}
2023-10-02 23:10:49 +03:00
/**
* @return Option<Revision>
*/
public function getByRecordIdAndVersion(string $rid, int $version): Option
{
2024-09-07 13:22:58 +03:00
$res = Database::make()->table($this->table)
2023-10-02 23:10:49 +03:00
->where("recordId", $rid)
->where('_sys->version', $version)->first();
if (empty($res)) {
return none();
}
2023-10-15 19:14:07 +03:00
return some($this->fromDB($res));
}
public function toDB(Revision $revision): array
{
return [
"id" => $revision->id,
"recordId" => $revision->recordId,
"schema" => $revision->schema,
"_sys" => json_encode($revision->_sys),
"_file" => json_encode($revision->_file),
"data" => json_encode($revision->data),
2024-08-19 17:48:10 +03:00
"_edges" => json_encode($revision->_edges),
2023-10-15 19:14:07 +03:00
];
}
public function fromDB(stdClass $data): Revision
{
$file = json_decode($data->_file, true);
if (!empty($file)) {
2024-08-19 17:48:10 +03:00
$file = new FileData(...$file);
2023-10-15 19:14:07 +03:00
} else {
$file = null;
}
$edges = array_map(fn($e) => Edge::fromArray($e), json_decode($data->_edges ?? "[]", true));
return new Revision(
id: $data->id,
recordId: $data->recordId,
schema: $data->schema,
_sys: System::fromArray(json_decode($data->_sys, true)),
data: new RecordData(json_decode($data->data, true)),
2024-08-19 17:48:10 +03:00
_edges: $edges,
2023-10-15 19:14:07 +03:00
_file: $file
);
2023-10-02 23:10:49 +03:00
}
}