Saving of nodes

This commit is contained in:
Magnus Åhall 2025-07-03 13:25:08 +02:00
parent c5bec0afa6
commit 08fd2cf4e9
16 changed files with 852 additions and 42 deletions

55
node.go
View file

@ -5,6 +5,7 @@ import (
"github.com/jmoiron/sqlx"
// Standard
"encoding/json"
"time"
)
@ -14,19 +15,56 @@ type Node struct {
ParentID int `db:"parent_id"`
TypeID int `db:"type_id"`
TypeName string `db:"type_name"`
TypeIcon string `db:"type_icon"`
TypeID int `db:"type_id"`
TypeName string `db:"type_name"`
TypeSchema any `db:"type_schema"`
TypeSchemaRaw []byte `db:"type_schema_raw" json:"-"`
TypeIcon string `db:"type_icon"`
Updated time.Time
Data any
RawData []byte `db:"raw_data"`
DataRaw []byte `db:"data_raw" json:"-"`
NumChildren int `db:"num_children"`
Children []*Node
}
func GetNode(startNodeID, maxDepth int) (topNode *Node, err error) {
func GetNode(nodeID int) (node Node, err error) {
row := db.QueryRowx(`
SELECT
n.id,
n.name,
n.updated,
n.data AS data_raw,
t.id AS type_id,
t.name AS type_name,
t.schema AS type_schema_raw,
t.schema->>'icon' AS type_icon
FROM public.node n
INNER JOIN public.type t ON n.type_id = t.id
WHERE
n.id = $1
`, nodeID)
err = row.StructScan(&node)
if err != nil {
return
}
err = json.Unmarshal(node.TypeSchemaRaw, &node.TypeSchema)
if err != nil {
return
}
err = json.Unmarshal(node.DataRaw, &node.Data)
if err != nil {
return
}
return
}
func GetNodeTree(startNodeID, maxDepth int) (topNode *Node, err error) {
nodes := make(map[int]*Node)
var rows *sqlx.Rows
rows, err = GetNodeRows(startNodeID, maxDepth)
@ -85,7 +123,7 @@ func GetNodeRows(startNodeID, maxDepth int) (rows *sqlx.Rows, err error) {
t.name AS type_name,
COALESCE(t.schema->>'icon', '') AS type_icon,
n.updated,
n.data AS raw_data,
n.data AS data_raw,
COUNT(c.child) AS num_children
FROM nodes ns
INNER JOIN public.node n ON ns.id = n.id
@ -122,3 +160,8 @@ func ComposeTree(nodes map[int]*Node, node *Node) {
nodes[node.ID] = node
}
func UpdateNode(nodeID int, data []byte) (err error) {
_, err = db.Exec(`UPDATE public.node SET data=$2 WHERE id=$1`, nodeID, data)
return
}