Skip to content

Commit

Permalink
Merge pull request #612 from FalkorDB/change-node-properties-format-i…
Browse files Browse the repository at this point in the history
…n-schema

Fix change node properties format in schema
  • Loading branch information
Anchel123 authored Jan 16, 2025
2 parents a1ae9a9 + 36fc3bb commit e6316f2
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 25 deletions.
49 changes: 36 additions & 13 deletions app/api/graph/model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
/* eslint-disable one-var */
/* eslint-disable no-param-reassign */
/* eslint-disable @typescript-eslint/no-explicit-any */

import { EdgeDataDefinition, NodeDataDefinition } from 'cytoscape';
import { LinkObject, NodeObject } from 'react-force-graph-2d';

const getSchemaValue = (value: string): string[] => {
let unique, required, type, description
if (value.includes("!")) {
value = value.replace("!", "")
unique = "true"
} else {
unique = "false"
}
if (value.includes("*")) {
value = value.replace("*", "")
required = "true"
} else {
required = "false"
}
if (value.includes("-")) {
[type, description] = value.split("-")
} else {
type = "string"
description = ""
}
return [type, description, unique, required]
}

export type Node = NodeObject<{
id: number,
category: string[],
Expand Down Expand Up @@ -209,14 +233,14 @@ export class Graph {
return new Graph(graphName || "", [], [], { nodes: [], links: [] }, new Map<string, Category>(), new Map<string, Category>(), new Map<number, Node>(), new Map<number, Link>(), colors)
}

public static create(id: string, results: { data: Data, metadata: any[] }, colors?: string[]): Graph {
public static create(id: string, results: { data: Data, metadata: any[] }, isCollapsed: boolean, isSchema: boolean, colors?: string[],): Graph {
const graph = Graph.empty(undefined, colors)
graph.extend(results)
graph.extend(results, isCollapsed, isSchema)
graph.id = id
return graph
}

public extendNode(cell: NodeCell, collapsed = false) {
public extendNode(cell: NodeCell, collapsed: boolean, isSchema: boolean) {
// check if category already exists in categories
const categories = this.createCategory(cell.labels.length === 0 ? [""] : cell.labels)
// check if node already exists in nodes or fake node was created
Expand All @@ -233,7 +257,7 @@ export class Graph {
data: {}
}
Object.entries(cell.properties).forEach(([key, value]) => {
node.data[key] = value as string;
node.data[key] = isSchema ? getSchemaValue(value) : value;
});
this.nodesMap.set(cell.id, node)
this.elements.nodes.push(node)
Expand All @@ -248,7 +272,7 @@ export class Graph {
currentNode.expand = false
currentNode.collapsed = collapsed
Object.entries(cell.properties).forEach(([key, value]) => {
currentNode.data[key] = value as string;
currentNode.data[key] = isSchema ? getSchemaValue(value) : value;
});

// remove empty category if there are no more empty nodes category
Expand All @@ -272,9 +296,8 @@ export class Graph {
return currentNode
}

public extendEdge(cell: LinkCell, collapsed = false) {
public extendEdge(cell: LinkCell, collapsed: boolean, isSchema: boolean) {
const label = this.createLabel(cell.relationshipType)

const currentEdge = this.linksMap.get(cell.id)

if (!currentEdge) {
Expand Down Expand Up @@ -369,7 +392,7 @@ export class Graph {
}

Object.entries(cell.properties).forEach(([key, value]) => {
link.data[key] = value as string;
link.data[key] = isSchema ? getSchemaValue(value) : value;
});

this.linksMap.set(cell.id, link)
Expand All @@ -381,7 +404,7 @@ export class Graph {
return currentEdge
}

public extend(results: { data: Data, metadata: any[] }, collapsed = false): (Node | Link)[] {
public extend(results: { data: Data, metadata: any[] }, collapsed = false, isSchema = false): (Node | Link)[] {
const newElements: (Node | Link)[] = []
const data = results?.data

Expand All @@ -399,15 +422,15 @@ export class Graph {
if (cell instanceof Object) {
if (cell.nodes) {
cell.nodes.forEach((node: any) => {
newElements.push(this.extendNode(node, collapsed))
newElements.push(this.extendNode(node, collapsed, isSchema))
})
cell.edges.forEach((edge: any) => {
newElements.push(this.extendEdge(edge, collapsed))
newElements.push(this.extendEdge(edge, collapsed, isSchema))
})
} else if (cell.relationshipType) {
newElements.push(this.extendEdge(cell, collapsed))
newElements.push(this.extendEdge(cell, collapsed, isSchema))
} else if (cell.labels) {
newElements.push(this.extendNode(cell, collapsed))
newElements.push(this.extendNode(cell, collapsed, isSchema))
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion app/graph/Selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default function Selector({ onChange, graphName, setGraphName, queries, r

const json = await result.json()
if (json.result) {
setSchema(Graph.create(name, json.result))
setSchema(Graph.create(name, json.result, false, true))
}
}
onChange(name)
Expand Down
2 changes: 1 addition & 1 deletion app/graph/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function View({ graph, setGraph, selectedValue }: {
const [editable, setEditable] = useState<string>("")

const handlePreferencesChange = (colors?: string[]) => {
setGraph(Graph.create(graph.Id, { data: graph.Data, metadata: graph.Metadata }, colors || colorsArr))
setGraph(Graph.create(graph.Id, { data: graph.Data, metadata: graph.Metadata }, false, true, colors || colorsArr))
if (colors) {
localStorage.removeItem(graph.Id)
}
Expand Down
4 changes: 2 additions & 2 deletions app/graph/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default function Page() {
const queryArr = queries.some(q => q.text === query) ? queries : [...queries, { text: query, metadata: result.metadata }]
setQueries(queryArr)
localStorage.setItem("query history", JSON.stringify(queryArr))
const g = Graph.create(graphName, result, graph.Colors)
const g = Graph.create(graphName, result, false, false, graph.Colors)
setGraph(g)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand All @@ -100,7 +100,7 @@ export default function Page() {
const queryArr = queries.some(q => q.text === query) ? queries : [...queries, { text: query, metadata: result.metadata }]
setQueries(queryArr)
localStorage.setItem("query history", JSON.stringify(queryArr))
setGraph(Graph.create(graphName, result))
setGraph(Graph.create(graphName, result, false, false, graph.Colors))
setHistoryQuery(query)
setQueriesOpen(false)
}
Expand Down
4 changes: 2 additions & 2 deletions app/schema/SchemaCreateElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function SchemaCreateElement({ onCreate, onExpand, selectedNodes,

const handleSetAttribute = (isUndo: boolean, att?: [string, string[]]) => {
const newAtt = att || attribute

if (!newAtt[0] || newAtt[1].some((v) => !v)) {
toast({
title: "Error",
Expand Down Expand Up @@ -194,7 +194,7 @@ export default function SchemaCreateElement({ onCreate, onExpand, selectedNodes,
/>
}
</div>
<p className="flex text-white">{attributes.length} Attributes</p>
<p className="font-medium text-xl"><span className="text-primary">{attributes.length}</span> Attributes</p>
</div>
<div className="w-full h-1 grow flex flex-col justify-between items-start font-medium">
<Table>
Expand Down
2 changes: 1 addition & 1 deletion app/schema/SchemaDataPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default function SchemaDataPanel({ obj, onExpand, onSetAttributes, onRemo
</Button>
<p>{label}</p>
</div>
<p className="flex text-white">{attributes.length} Attributes</p>
<p className="font-medium text-xl"><span className="text-primary">{attributes.length}</span> Attributes</p>
</div>
<Table parentClassName="grow">
<TableHeader>
Expand Down
17 changes: 13 additions & 4 deletions app/schema/SchemaView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ interface Props {
}

const getCreateQuery = (type: boolean, selectedNodes: [Node, Node], attributes: [string, string[]][], label?: string) => {
const formateAttributes: [string, string][] = attributes.map((att) => {
const [key, [t, d, u, r]] = att
let val = `${t}`
if (u === "true") val += "!"
if (r === "true") val += "*"
if (d) val += `-${d}`
return [key, val]
})

if (type) {
return `CREATE (n${label ? `:${label}` : ""}${attributes?.length > 0 ? ` {${attributes.map(([k, [t, d, u, r]]) => `${k}: ["${t}", "${d}", "${u}", "${r}"]`).join(",")}}` : ""}) RETURN n`
return `CREATE (n${label ? `:${label}` : ""}${formateAttributes?.length > 0 ? ` {${formateAttributes.map(([k, v]) => `${k}: "${v}"`).join(",")}}` : ""}) RETURN n`
}
return `MATCH (a), (b) WHERE ID(a) = ${selectedNodes[0].id} AND ID(b) = ${selectedNodes[1].id} CREATE (a)-[e${label ? `:${label}` : ""}${attributes?.length > 0 ? ` {${attributes.map(([k, [t, d, u, un]]) => `${k}: ["${t}", "${d}", "${u}", "${un}"]`).join(",")}}` : ""}]->(b) RETURN e`
return `MATCH (a), (b) WHERE ID(a) = ${selectedNodes[0].id} AND ID(b) = ${selectedNodes[1].id} CREATE (a)-[e${label ? `:${label}` : ""}${formateAttributes?.length > 0 ? ` {${formateAttributes.map(([k, v]) => `${k}: "${v}"`).join(",")}}` : ""}]->(b) RETURN e`
}

export default function SchemaView({ schema, fetchCount, session }: Props) {
Expand Down Expand Up @@ -292,10 +301,10 @@ export default function SchemaView({ schema, fetchCount, session }: Props) {
const json = await result.json()

if (isAddEntity) {
schema.extendNode(json.result.data[0].n)
schema.extendNode(json.result.data[0].n, false, true)
setIsAddEntity(false)
} else {
schema.extendEdge(json.result.data[0].e, true)
schema.extendEdge(json.result.data[0].e, false, true)
setIsAddRelation(false)
}

Expand Down
2 changes: 1 addition & 1 deletion app/schema/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function Page() {
if (!result.ok) return
const json = await result.json()
const colors = localStorage.getItem(schemaName)?.split(/[[\]",]/).filter(c => c)
setSchema(Graph.create(schemaName, json.result, colors))
setSchema(Graph.create(schemaName, json.result, false, true, colors))

fetchCount()

Expand Down

0 comments on commit e6316f2

Please sign in to comment.