Add replacement key for replaceable/addressable events. Update constraints/indexes.

This commit is contained in:
Jay
2026-05-21 12:52:00 -04:00
parent 24a76b23ce
commit 96693304e8
2 changed files with 60 additions and 11 deletions
+24 -7
View File
@@ -25,20 +25,37 @@ func ConnectNeo4j(ctx context.Context, uri, user, password string) (neo4j.Driver
// the database // the database
func SetNeo4jSchema(ctx context.Context, driver neo4j.Driver) error { func SetNeo4jSchema(ctx context.Context, driver neo4j.Driver) error {
schemaQueries := []string{ schemaQueries := []string{
`CREATE CONSTRAINT user_pubkey IF NOT EXISTS // Constraints
// User.pubkey
`CREATE CONSTRAINT user IF NOT EXISTS
FOR (n:User) REQUIRE n.pubkey IS UNIQUE`, FOR (n:User) REQUIRE n.pubkey IS UNIQUE`,
`CREATE INDEX user_pubkey IF NOT EXISTS // Relay.url
FOR (n:User) ON (n.pubkey)`, `CREATE CONSTRAINT url IF NOT EXISTS
FOR (n:Relay) REQUIRE n.url IS UNIQUE`,
`CREATE INDEX event_id IF NOT EXISTS // Event.id
FOR (n:Event) ON (n.id)`, `CREATE CONSTRAINT event IF NOT EXISTS
FOR (n:Event) REQUIRE n.id IS UNIQUE`,
// Tag.(name, value)
`CREATE CONSTRAINT tag IF NOT EXISTS
FOR (n:Tag) REQUIRE (n.name, n.value) IS UNIQUE`,
// ReplacementKey.(pubkey, kind)
`CREATE CONSTRAINT replacement_key IF NOT EXISTS
FOR (n:ReplacementKey) REQUIRE (n.pubkey, n.kind) IS UNIQUE`,
// Indexes
// Event.kind
`CREATE INDEX event_kind IF NOT EXISTS `CREATE INDEX event_kind IF NOT EXISTS
FOR (n:Event) ON (n.kind)`, FOR (n:Event) ON (n.kind)`,
`CREATE INDEX tag_name_value IF NOT EXISTS // Event.created_at
FOR (n:Tag) ON (n.name, n.value)`, `CREATE INDEX event_created_at IF NOT EXISTS
FOR (n:Event) ON (n.created_at)`,
} }
// Create indexes and constraints // Create indexes and constraints
+32
View File
@@ -15,6 +15,7 @@ func NewSimpleMatchKeys() *SimpleMatchKeys {
"Relay": {"url"}, "Relay": {"url"},
"Event": {"id"}, "Event": {"id"},
"Tag": {"name", "value"}, "Tag": {"name", "value"},
"ReplacementKey": {"pubkey", "kind"},
}, },
} }
} }
@@ -41,6 +42,13 @@ func NewTagNode(name string, value string) *Node {
"value": value}) "value": value})
} }
func NewReplacementKeyNode(pubkey string, kind int) *Node {
return NewNode("ReplacementKey", Properties{
"pubkey": pubkey,
"kind": kind,
})
}
// ======================================== // ========================================
// Relationship Constructors // Relationship Constructors
// ======================================== // ========================================
@@ -70,6 +78,30 @@ func NewReferencesUserRel(
"REFERENCES", "Tag", "User", start, end, props) "REFERENCES", "Tag", "User", start, end, props)
} }
func NewIsReplaceableRel(
start *Node, end *Node, props Properties) *Relationship {
return NewRelationshipWithValidation(
"IS_REPLACEABLE", "Event", "ReplacementKey", start, end, props)
}
func NewForUserRel(
start *Node, end *Node, props Properties) *Relationship {
return NewRelationshipWithValidation(
"FOR_USER", "ReplacementKey", "User", start, end, props)
}
func NewWithDTagRel(
start *Node, end *Node, props Properties) *Relationship {
return NewRelationshipWithValidation(
"WITH_D_TAG", "ReplacementKey", "Tag", start, end, props)
}
func NewReferencesReplacementKeyRel(
start *Node, end *Node, props Properties) *Relationship {
return NewRelationshipWithValidation(
"REFERENCES", "Tag", "ReplacementKey", start, end, props)
}
// ======================================== // ========================================
// Relationship Constructor Helpers // Relationship Constructor Helpers
// ======================================== // ========================================