Repository navigation
Labels
Description
Schema Support for Relational Fields - Implementation
Overview
Implement runtime schema-driven relationships in NodeSpace. Users can define custom node types with relationships via NLP/MCP, and NodeSpace automatically generates SurrealDB edge tables.
Architecture Reference: See docs/architecture/data/schema-relational-fields.md for complete architectural decisions.
Key Design Decisions (Finalized)
1. Edge-Only Storage
- Relationships stored only in edge tables - no spoke table fields
- Both directions query the same edge table
- Single source of truth, no sync issues
2. Relationships Separate from Fields
- Add
relationshipsarray to schema spoke (separate fromfields) - Edge tables are fundamentally different from spoke fields
- Clearer for NLP parsing
3. Computed Reverse Relationship Discovery
- Do NOT mutate target schemas when relationship created
- Use query-time discovery:
get_inbound_relationships(node_type) - Avoids write amplification, OCC conflicts, user confusion
4. Forward-Looking Schema Changes (Grandfathering)
- Schema changes apply to new data only
- Existing data is grandfathered (null allowed for historical)
- Optional backfill for simple types (edge fields)
- Consistent with existing field migration behavior
5. Soft-Delete for Relationship Removal
- When relationship removed from schema: edge table + data preserved
- Schema node is source of truth for what's "active"
- User can recover by re-adding relationship
- Matches existing field behavior (FLEXIBLE tables)
6. Atomic Schema + Edge Table Creation
- Extends existing
create_schema_node_atomicpattern - Single transaction: schema node + spoke + edge tables
- Already proven to work with DDL in transactions
7. Structural Relations Remain Hardcoded
has_childandmentionsstay in schema.surql- User-defined relationships use new schema-driven approach
Example Schema with Relationships
{
"id": "invoice",
"nodeType": "schema",
"content": "Invoice",
"fields": [
{"name": "amount", "type": "number", "required": true},
{"name": "due_date", "type": "date"}
],
"relationships": [
{
"name": "billed_to",
"targetType": "customer",
"direction": "out",
"cardinality": "one",
"required": true,
"reverseName": "invoices",
"reverseCardinality": "many",
"edgeFields": [
{"name": "billing_date", "type": "date", "required": true},
{"name": "payment_terms", "type": "string"}
]
}
]
}Implementation Phases
Phase 1: Data Model Extensions
- Add
SchemaRelationshipstruct topackages/core/src/models/schema.rs - Add
EdgeFieldstruct (simpler than SchemaField) - Update
packages/core/src/db/schema.surql- addrelationshipsfield - Add serialization/deserialization tests
Phase 2: DDL Generation for Edge Tables
- Add
generate_relationship_ddl_statements()toSchemaTableManager - Implement edge table name computation:
{source}_{name}_{target} - Generate edge field DDL with
IF NOT EXISTS - Generate bidirectional indexes (in, out, unique)
- Add tests for edge table DDL generation
Phase 3: Schema CRUD API
- Extend
create_schema_node_atomicto include edge table DDL - Extend
update_schema_node_atomicfor relationship changes - Add relationship validation (target schema exists if required)
- Add tests for schema CRUD with relationships
Phase 4: Relationship CRUD API
- Add
create_relationship(source_id, relationship_name, target_id, edge_data) - Add
delete_relationship(source_id, relationship_name, target_id) - Add
get_related_nodes(node_id, relationship_name, direction) - Add tests for relationship CRUD
Phase 5: NLP Discovery API
- Add
get_schema_with_relationships(schema_id)helper - Add
get_inbound_relationships(node_type)computed lookup - Cache relationship metadata for fast NLP access
- Add documentation for NLP integration
Phase 6: MCP Handler Integration
- Extend MCP schema handlers to support relationships
- Add relationship CRUD handlers
- Add integration tests
Migration Behavior (Documented)
NodeSpace uses a forward-looking, soft-delete migration pattern for both fields and relationships:
| Change | Historical Data | New Data |
|---|---|---|
| Add relationship | N/A | Must provide if required |
| Remove relationship | Edges preserved (soft-delete) | Can't create new |
Set required: true |
Null allowed (grandfathered) | Must provide |
| Add edge field | Null for historical | Required if marked |
| Remove edge field | Data preserved | Not stored |
Breaking changes require decision points (e.g., cardinality change from many→one).
Acceptance Criteria
- Architecture documented in
docs/architecture/data/schema-relational-fields.md - Migration behavior documented (consistent with existing field pattern)
-
SchemaRelationshipandEdgeFieldstructs defined and tested - Schema spoke table supports
relationshipsarray - Edge table DDL generation working with tests
- Atomic schema + relationship creation working
- Relationship CRUD API working
- NLP discovery API working
- MCP handlers support relationship CRUD
Technical Notes
Edge Table Naming: {source_type}_{relationship_name}_{target_type}
Query Patterns:
-- Forward: invoice -> customer
SELECT ->billed_to->customer.* FROM invoice:INV-001;
-- Reverse: customer -> invoices
SELECT <-billed_to<-invoice.* FROM customer:acme;
-- With edge properties
SELECT in, out, billing_date FROM invoice_billed_to_customer
WHERE billing_date > time::now() - 30d;Dependencies
- Consolidate Database Schema Seeding to Backend Service #691 - Schema seeding and DDL generation foundation
- Simplify Schema Architecture: Remove SchemaDefinition and SchemaService #690 - Schema simplification (completed)
Definition of Done
- All acceptance criteria met
- All tests passing
- Architecture documentation complete
- MCP handlers functional
- NLP can discover and query relationships
Activity
malibio commentedon Dec 1, 2025
Test baseline established:
Starting architecture design work for schema relational fields support.
malibio commentedon Dec 1, 2025
Implementation Guide - Schema Relational Fields
Based on our discussion, here is the complete implementation plan for adding runtime schema-driven relationships to NodeSpace.
Architecture Overview
Goal: Enable users to define custom node types with relationships via NLP/MCP. NodeSpace automatically generates SurrealDB edge tables and provides schema-driven query capabilities.
Key Principles:
Phase 1: Data Model Extensions
1.1 Add
SchemaRelationshipStructFile:
packages/core/src/models/schema.rsAdd new struct alongside
SchemaField:1.2 Update Schema Spoke Table DDL
File:
packages/core/src/db/schema.surqlAdd
relationshipsfield toschematable:1.3 Add Tests for SchemaRelationship Serialization
File:
packages/core/src/models/schema.rs(test module)Phase 2: DDL Generation for Edge Tables
2.1 Add Edge Table DDL Generation Method
File:
packages/core/src/services/schema_table_manager.rsAdd new method:
Document architecture for schema relational fields (closes #703)
malibio commentedon Dec 1, 2025
Architecture phase complete and merged in PR #707.
Reopening issue to track implementation of the 6 phases documented in the architecture.
Next: Begin Phase 1 (Data Model Extensions) when ready.
malibio commentedon Dec 1, 2025
Test baseline: Frontend 1477 passed (74 files), Rust 482 passed (16 ignored). Starting Phase 1 implementation.
Implement Schema Relationship Data Model and DDL Generation (closes #703
WIP: Schema Relationship Data Model and DDL Generation - Phases 1-2 c…
Phase 3 Complete: Schema CRUD API with Relationship Support (Issue #703)
Phase 4 Complete: Relationship CRUD API (Issue #703)
WIP: Schema Relationship Implementation - Phases 1-4 Complete
Phase 5-6 Complete: NLP Discovery API and MCP Handler Integration (Is…
malibio commentedon Dec 1, 2025
Phases 5-6 Complete! 🎉
Phase 5: NLP Discovery API ✅
relationshipsfield toSchemaNodestructget_all_schemas()method toSurrealStoreandNodeServiceInboundRelationshipCachewith time-based (60s TTL) and event-driven invalidationget_inbound_relationships()for reverse relationship discoveryget_relationship_graph()for complete model visualizationget_schema_with_relationships()convenience methodPhase 6: MCP Handler Integration ✅
Created
relationships.rshandler module with 6 new MCP tools:create_relationship- Create edges between nodesdelete_relationship- Remove relationship edgesget_related_nodes- Query connected nodes (supports direction: 'out'/'in')get_relationship_graph- Get complete relationship graph summaryget_inbound_relationships- Discover reverse relationships by target typeget_all_schemas- Get all schema definitions with relationshipsTest Results
Commit
b2b2d4e- Phase 5-6 Complete: NLP Discovery API and MCP Handler IntegrationSchema Support for Relational Fields - Architecture Design (#708)
Add schema management MCP tools (Issue #703)
Add schema management MCP tools (Issue #703) (#713)
Schema Support for Relational Fields - Architecture Design (#707)
Schema Support for Relational Fields - Architecture Design (#708)
Add schema management MCP tools (Issue #703) (#713)
Schema Support for Relational Fields - Architecture Design (#707)
Schema Support for Relational Fields - Architecture Design (#708)
Add schema management MCP tools (Issue #703) (#713)
Schema Support for Relational Fields - Architecture Design (#708)
Add schema management MCP tools (Issue #703) (#713)