Skip to content

Commit

Permalink
Add support for multiple relationships with the same type
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueleite42 committed Nov 24, 2023
1 parent 3758d6f commit 9e68605
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib",
"css.lint.unknownAtRules": "ignore"
"css.lint.unknownAtRules": "ignore",
"cSpell.words": ["datamodel", "DMMF", "reactflow", "Virtuals"]
}
1 change: 1 addition & 0 deletions components/ModelNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const ModelNode = ({ data }: ModelNodeProps) => {
const targetHandleId = relationEdgeTargetHandleId(
data.name,
col.relationData.name,
col.name,
);
const sourceHandleId = relationEdgeSourceHandleId(
data.name,
Expand Down
22 changes: 16 additions & 6 deletions util/prismaToFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ export const relationEdgeSourceHandleId = (
column: string,
) => `${table}-${relation}-${column}`;

// TODO: might need to include column name for multiple relations of same type??
export const relationEdgeTargetHandleId = (table: string, relation: string) =>
`${table}-${relation}`;
export const relationEdgeTargetHandleId = (
table: string,
relation: string,
column: string,
) => `${table}-${relation}-${column}`;

const virtualTableName = (relation: string, table: string) =>
`${relation}-${table}`;
Expand Down Expand Up @@ -231,7 +233,12 @@ const relationsToEdges = (
};

const source = rel.fields.find((f) => f.side === "source")!;
const target = rel.fields.find((f) => f.side === "target")!;
let target = rel.fields.find((f) => f.side === "target");

if (!target && rel.virtual)
target = rel.fields.find((f) => f.tableName === rel.virtual?.name);

if (!target) throw new Error("Invalid target");

result.push({
...base,
Expand All @@ -242,9 +249,12 @@ const relationsToEdges = (
rel.name,
source.name,
),
targetHandle: relationEdgeTargetHandleId(target.tableName, rel.name),
targetHandle: relationEdgeTargetHandleId(
target.tableName,
rel.name,
target.name,
),
});
// }
}

return result;
Expand Down

0 comments on commit 9e68605

Please sign in to comment.