diff --git a/front/js/svelte/records/Graph.svelte b/front/js/svelte/records/Graph.svelte index a1991f0..7612eaf 100644 --- a/front/js/svelte/records/Graph.svelte +++ b/front/js/svelte/records/Graph.svelte @@ -13,14 +13,16 @@ } let backlinks = graph.parentEdges.map(edge => { - let schema = channel.schemas.find((s) => s.name === edge.sourceSchema); + const parentRecord = graph.records.find( record => record.id === edge.source); + + let schema = channel.schemas.find((s) => s.name === parentRecord.schema); let edgeField = findEdgeField(schema,edge.field); if(!edgeField){ return null; } return { field: edgeField.label, - record: graph.records.find( record => record.id === edge.source) + record: parentRecord } }).filter( edgeOrNull => !!edgeOrNull) diff --git a/front/js/svelte/records/elements/reference.js b/front/js/svelte/records/elements/reference.js index f815007..3df3c8e 100644 --- a/front/js/svelte/records/elements/reference.js +++ b/front/js/svelte/records/elements/reference.js @@ -6,8 +6,6 @@ export function insertEdges(graph, sourceRecord, targetRecords, fieldName, actio return { target: r.id, source: sourceRecord.id, - sourceSchema: sourceRecord.schema, - targetSchema: r.schema, field: fieldName, depth: 1, rank: "" diff --git a/src/Commands/SetupDatabase.php b/src/Commands/SetupDatabase.php index c08c2b2..f3f16e5 100644 --- a/src/Commands/SetupDatabase.php +++ b/src/Commands/SetupDatabase.php @@ -79,11 +79,8 @@ class SetupDatabase extends Command Database::make()->getSchemaBuilder()->create('edges', function (Blueprint $table) { $table->uuid('source'); $table->uuid('target'); - $table->string('sourceSchema'); - $table->string('targetSchema'); $table->string('field'); $table->string('rank'); - $table->unique(['source', 'target', "field"]); }); } diff --git a/src/Edge/Edge.php b/src/Edge/Edge.php index 911a3f1..28596eb 100644 --- a/src/Edge/Edge.php +++ b/src/Edge/Edge.php @@ -14,8 +14,6 @@ final class Edge public string $source, public string $target, - public string $sourceSchema, - public string $targetSchema, public string $field, public string $rank = "a", public int $depth = 0, @@ -29,7 +27,7 @@ final class Edge public function equal(Edge $edge): bool { - return $this->targetSchema === $edge->targetSchema && $this->field === $edge->field && $this->target === $edge->target && $this->source === $edge->source; + return $this->field === $edge->field && $this->target === $edge->target && $this->source === $edge->source; } public function toArray(): array @@ -51,8 +49,6 @@ final class Edge source: data_get($data, 'source'), target: data_get($data, 'target'), - sourceSchema: data_get($data, 'sourceSchema'), - targetSchema: data_get($data, 'targetSchema'), field: data_get($data, 'field'), rank: data_get($data, 'rank'), depth: data_get($data, 'depth', 0), diff --git a/src/Edge/EdgeRepo.php b/src/Edge/EdgeRepo.php index cfee348..6b671ea 100644 --- a/src/Edge/EdgeRepo.php +++ b/src/Edge/EdgeRepo.php @@ -78,8 +78,6 @@ class EdgeRepo return new Edge( source: $edge->source, target: $edge->target, - sourceSchema: $edge->sourceSchema, - targetSchema: $edge->targetSchema, field: $edge->field, rank: $edge->rank, depth: $edge->depth ?? 0 @@ -92,8 +90,6 @@ class EdgeRepo Database::make()->table("edges") ->where("source", $edge->source) ->where("target", $edge->target) - ->where("sourceSchema", $edge->sourceSchema) - ->where("targetSchema", $edge->targetSchema) ->where("field", $edge->field) ->delete(); } diff --git a/src/Edge/EdgeService.php b/src/Edge/EdgeService.php index 2c34f52..b02e3cf 100644 --- a/src/Edge/EdgeService.php +++ b/src/Edge/EdgeService.php @@ -14,8 +14,6 @@ class EdgeService public function create( string $source, string $target, - string $sourceSchema, - string $targetSchema, string $field, string $rank, ): Edge @@ -23,11 +21,8 @@ class EdgeService $edge = new Edge( - source: $source, target: $target, - sourceSchema: $sourceSchema, - targetSchema: $targetSchema, field: $field, rank: $rank, ); @@ -37,14 +32,12 @@ class EdgeService /** * @param string $source - * @param string $sourceSchema * @param list $edges * @return list * @throws LucentException */ public function createManyForRecord( string $source, - string $sourceSchema, array $edges, ): array { @@ -55,8 +48,6 @@ class EdgeService $edgeCollection[] = new Edge( source: $source, target: $edge->target, - sourceSchema: $sourceSchema, - targetSchema: $edge->targetSchema, field: $edge->field, rank: $lastRank->get(), ); @@ -68,13 +59,11 @@ class EdgeService /** * @param string $source - * @param string $sourceSchema * @param list $edges * @return list */ public function replaceManyForRecord( string $source, - string $sourceSchema, array $edges, ): array { @@ -85,8 +74,6 @@ class EdgeService $edgeCollection[] = new Edge( source: $source, target: $edge->target, - sourceSchema: $sourceSchema, - targetSchema: $edge->targetSchema, field: $edge->field, rank: $lastRank->get(), ); diff --git a/src/Http/Controller/EdgeController.php b/src/Http/Controller/EdgeController.php index b0b1224..7ea08f8 100644 --- a/src/Http/Controller/EdgeController.php +++ b/src/Http/Controller/EdgeController.php @@ -6,6 +6,7 @@ use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Lucent\Edge\EdgeService; use Lucent\Query\Query; +use Lucent\Record\InputData\EdgeInputData; class EdgeController extends Controller { @@ -19,13 +20,11 @@ class EdgeController extends Controller public function insertMany(Request $request) { - $this->edgeService->createMany( + $this->edgeService->createManyForRecord( source: $request->input("source"), - sourceSchema: $request->input("sourceSchema"), - targetSchema: $request->input("targetSchema"), - field: $request->input("field"), - targets: $request->input("targets"), + edges: array_map(fn(string $target)=> new EdgeInputData($target,$request->input("field")),$request->input("targets")) ); + $graph = $this->query ->filter(["id" => $request->input("source")]) ->limit(1) diff --git a/src/Query/DatabaseGraph/PgsqlDatabaseGraph.php b/src/Query/DatabaseGraph/PgsqlDatabaseGraph.php index 606e1b1..0aca993 100644 --- a/src/Query/DatabaseGraph/PgsqlDatabaseGraph.php +++ b/src/Query/DatabaseGraph/PgsqlDatabaseGraph.php @@ -15,7 +15,7 @@ class PgsqlDatabaseGraph implements DatabaseGraph public function getChildren(array $ids, QueryOptions $options): array { $subquery = Database::make()->table('edges AS g') - ->select(DB::raw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field, 1 as depth ')) + ->select(DB::raw('g.source,g.target,g.rank,g.field, 1 as depth ')) ->whereIn('source', $ids); if (!empty($options->childrenFields)) { @@ -25,7 +25,7 @@ class PgsqlDatabaseGraph implements DatabaseGraph $subquery->limit($options->childrenLimit) ->unionAll( Database::make()->table(DB::raw("edges AS g, search_graph AS sg ")) - ->selectRaw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field,sg.depth + 1 as depth') + ->selectRaw('g.source,g.target,g.rank,g.field,sg.depth + 1 as depth') ->whereRaw("g.source = sg.target") ->where("depth", "<", $options->childrenDepth) ->orderBy("rank") @@ -43,19 +43,18 @@ class PgsqlDatabaseGraph implements DatabaseGraph public function getParents(array $ids, QueryOptions $options): array { $subquery = Database::make()->table('edges AS g') - ->select(DB::raw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field, 1 as depth ')) + ->select(DB::raw('g.source,g.target,g.rank,g.field, 1 as depth ')) ->limit($options->parentsLimit) ->whereIn('g.target', $ids) ->unionAll( Database::make()->table(DB::raw("edges AS g, search_graph AS sg ")) - ->selectRaw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field,sg.depth + 1 as depth') + ->selectRaw('g.source,g.target,g.rank,g.field,sg.depth + 1 as depth') ->whereRaw("g.target = sg.source") ->where("depth", "<", $options->parentsDepth) ->orderBy("rank") ); return Database::make()->table('search_graph') - // ->select(DB::raw('sg.source,sg.target,sg.rank,sg."sourceSchema",sg."targetSchema",sg.field,sg.depth')) ->withRecursiveExpression('search_graph', $subquery) ->get()->toArray(); } diff --git a/src/Query/DatabaseGraph/SqliteDatabaseGraph.php b/src/Query/DatabaseGraph/SqliteDatabaseGraph.php index 423bad0..92dafd3 100644 --- a/src/Query/DatabaseGraph/SqliteDatabaseGraph.php +++ b/src/Query/DatabaseGraph/SqliteDatabaseGraph.php @@ -14,7 +14,7 @@ class SqliteDatabaseGraph implements DatabaseGraph public function getChildren(array $ids, QueryOptions $options): array { $subquery = Database::make()->table('edges AS g') - ->select(DB::raw('g.source,g.target,g.rank,g.sourceSchema,g.targetSchema,g.field, 1 as depth ')) + ->select(DB::raw('g.source,g.target,g.rank,g.field, 1 as depth ')) ->whereIn('source', $ids); if (!empty($options->childrenFields)) { @@ -24,14 +24,13 @@ class SqliteDatabaseGraph implements DatabaseGraph $subquery->limit($options->childrenLimit) ->unionAll( Database::make()->table(DB::raw("edges AS g, search_graph AS sg ")) - ->selectRaw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field,sg.depth + 1 as depth') + ->selectRaw('g.source,g.target,g.rank,g.field,sg.depth + 1 as depth') ->whereRaw("g.source = sg.target") ->where("depth", "<", $options->childrenDepth) ->orderBy("rank") ); return Database::make()->table('search_graph') - // ->select(DB::raw("*, 1 as depth ")) ->withRecursiveExpression('search_graph', $subquery) ->get()->toArray(); } @@ -42,19 +41,18 @@ class SqliteDatabaseGraph implements DatabaseGraph public function getParents(array $ids, QueryOptions $options): array { $subquery = Database::make()->table('edges AS g') - ->select(DB::raw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field, 1 as depth ')) + ->select(DB::raw('g.source,g.target,g.rank,g.field, 1 as depth ')) ->limit($options->parentsLimit) ->whereIn('g.target', $ids) ->unionAll( Database::make()->table(DB::raw("edges AS g, search_graph AS sg ")) - ->selectRaw('g.source,g.target,g.rank,"g"."sourceSchema","g"."targetSchema",g.field,sg.depth + 1 as depth') + ->selectRaw('g.source,g.target,g.rank,g.field,sg.depth + 1 as depth') ->whereRaw("g.target = sg.source") ->where("depth", "<", $options->parentsDepth) ->orderBy("rank") ); return Database::make()->table('search_graph') - // ->select(DB::raw('sg.source,sg.target,sg.rank,sg."sourceSchema",sg."targetSchema",sg.field,sg.depth')) ->withRecursiveExpression('search_graph', $subquery) ->get()->toArray(); } diff --git a/src/Query/Graph.php b/src/Query/Graph.php index 55e34ac..49ac64b 100644 --- a/src/Query/Graph.php +++ b/src/Query/Graph.php @@ -14,11 +14,11 @@ final class Graph * @param Collection $edges * */ public function __construct( - public Collection $records, - public Collection $edges, - public Collection $parentEdges, + public Collection $records, + public Collection $edges, + public Collection $parentEdges, public QueryOptions $queryOptions, - public ?int $total = null, + public ?int $total = null, ) { } @@ -47,12 +47,12 @@ final class Graph public function findChildren(QueryRecord $record, int $depth = 1): QueryRecord { - if($this->queryOptions->childrenDepth < $depth){ + if ($this->queryOptions->childrenDepth < $depth) { return $record; } $recordEdges = $this->edges - ->filter(fn(Edge $ed) => $ed->source === $record->id ) - ->unique(fn(Edge $ed) => $ed->targetSchema . $ed->field . $ed->target . $ed->source) + ->filter(fn(Edge $ed) => $ed->source === $record->id) + ->unique(fn(Edge $ed) => $ed->field . $ed->target . $ed->source) ->sort(fn($a, $b) => $a->rank <=> $b->rank)->values(); $groupRecordEdges = []; foreach ($recordEdges as $element) { @@ -78,7 +78,7 @@ final class Graph public function findParents(QueryRecord $record, int $depth = 1): QueryRecord { - if($this->queryOptions->parentsDepth < $depth){ + if ($this->queryOptions->parentsDepth < $depth) { return $record; } $recordEdges = $this->parentEdges->filter(fn(Edge $ed) => $ed->target === $record->id)->where("depth", $depth)->values()->sort(fn($a, $b) => $a->rank <=> $b->rank)->values(); diff --git a/src/Record/InputData/EdgeInputData.php b/src/Record/InputData/EdgeInputData.php index 64a5435..f4834b0 100644 --- a/src/Record/InputData/EdgeInputData.php +++ b/src/Record/InputData/EdgeInputData.php @@ -7,7 +7,6 @@ class EdgeInputData { public function __construct( public string $target, - public string $targetSchema, public string $field, ) { @@ -15,6 +14,6 @@ class EdgeInputData public static function fromArray(array $data): self { - return new self($data['target'], $data['targetSchema'], $data['field']); + return new self($data['target'], $data['field']); } } \ No newline at end of file diff --git a/src/Record/RecordService.php b/src/Record/RecordService.php index c3f29bf..2febaf7 100644 --- a/src/Record/RecordService.php +++ b/src/Record/RecordService.php @@ -127,7 +127,7 @@ readonly class RecordService } RecordRepo::create($record); - $newEdges = $this->edgeService->createManyForRecord($record->id, $record->schema, $edges); + $newEdges = $this->edgeService->createManyForRecord($record->id, $edges); $this->revisionService->create($record, $newEdges); return $record->id; @@ -246,7 +246,6 @@ readonly class RecordService ->values() ->map(fn(Edge $edge) => new EdgeInputData( target: $edge->target, - targetSchema: $edge->targetSchema, field: $edge->field, ))->toArray(); @@ -297,7 +296,7 @@ readonly class RecordService id: $revision->recordId, data: $revision->data->toArray(), status: Status::DRAFT, - edges: array_map(fn(Edge $edge) => new EdgeInputData($edge->target, $edge->targetSchema, $edge->field), $revision->_edges), + edges: array_map(fn(Edge $edge) => new EdgeInputData($edge->target, $edge->field), $revision->_edges), ); }