diff --git a/neo4j.go b/neo4j.go index 88619d9..ccc8571 100644 --- a/neo4j.go +++ b/neo4j.go @@ -25,20 +25,37 @@ func ConnectNeo4j(ctx context.Context, uri, user, password string) (neo4j.Driver // the database func SetNeo4jSchema(ctx context.Context, driver neo4j.Driver) error { 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`, - `CREATE INDEX user_pubkey IF NOT EXISTS - FOR (n:User) ON (n.pubkey)`, + // Relay.url + `CREATE CONSTRAINT url IF NOT EXISTS + FOR (n:Relay) REQUIRE n.url IS UNIQUE`, - `CREATE INDEX event_id IF NOT EXISTS - FOR (n:Event) ON (n.id)`, + // Event.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 FOR (n:Event) ON (n.kind)`, - `CREATE INDEX tag_name_value IF NOT EXISTS - FOR (n:Tag) ON (n.name, n.value)`, + // Event.created_at + `CREATE INDEX event_created_at IF NOT EXISTS + FOR (n:Event) ON (n.created_at)`, } // Create indexes and constraints diff --git a/schema.go b/schema.go index 54d6845..de76989 100644 --- a/schema.go +++ b/schema.go @@ -11,10 +11,11 @@ import ( func NewSimpleMatchKeys() *SimpleMatchKeys { return &SimpleMatchKeys{ Keys: map[string][]string{ - "User": {"pubkey"}, - "Relay": {"url"}, - "Event": {"id"}, - "Tag": {"name", "value"}, + "User": {"pubkey"}, + "Relay": {"url"}, + "Event": {"id"}, + "Tag": {"name", "value"}, + "ReplacementKey": {"pubkey", "kind"}, }, } } @@ -41,6 +42,13 @@ func NewTagNode(name string, value string) *Node { "value": value}) } +func NewReplacementKeyNode(pubkey string, kind int) *Node { + return NewNode("ReplacementKey", Properties{ + "pubkey": pubkey, + "kind": kind, + }) +} + // ======================================== // Relationship Constructors // ======================================== @@ -70,6 +78,30 @@ func NewReferencesUserRel( "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 // ========================================