Files
neostr-brainstorm/lib/schema.go
2025-02-16 16:56:23 -05:00

89 lines
2.0 KiB
Go

// This module provides methods for creating nodes and relationships according
// to a defined schema.
package lib
import (
"fmt"
)
// ========================================
// Schema Match Keys
// ========================================
func NewMatchKeys() *MatchKeys {
return &MatchKeys{
keys: map[string][]string{
"User": {"pubkey"},
"Relay": {"url"},
"Event": {"id"},
"Tag": {"name", "value"},
},
}
}
// ========================================
// Node Constructors
// ========================================
func NewUserNode(pubkey string) *Node {
return NewNode("User", Properties{"pubkey": pubkey})
}
func NewRelayNode(url string) *Node {
return NewNode("Relay", Properties{"url": url})
}
func NewEventNode(id string) *Node {
return NewNode("Event", Properties{"id": id})
}
func NewTagNode(name string, value string) *Node {
return NewNode("Tag", Properties{"name": name, "value": value})
}
// ========================================
// Relationship Constructors
// ========================================
func NewSignedRel(
start *Node, end *Node, props Properties) *Relationship {
return NewRelationshipWithValidation(
"SIGNED", "User", "Event", start, end, props)
}
func NewTaggedRel(
start *Node, end *Node, props Properties) *Relationship {
return NewRelationshipWithValidation(
"TAGGED", "Event", "Tag", start, end, props)
}
// ========================================
// Relationship Constructor Helpers
// ========================================
func validateNodeLabel(node *Node, role string, expectedLabel string) {
if !node.Labels.Contains(expectedLabel) {
panic(fmt.Errorf(
"expected %s node to have label '%s'. got %v",
role, expectedLabel, node.Labels.ToArray(),
),
)
}
}
func NewRelationshipWithValidation(
rtype string,
startLabel string,
endLabel string,
start *Node,
end *Node,
props Properties) *Relationship {
validateNodeLabel(start, "start", startLabel)
validateNodeLabel(end, "end", endLabel)
return NewRelationship(rtype, start, end, props)
}