Database Schema Design
AIMatrix Database Schema Specification
Overview
AIMatrix uses a polyglot persistence strategy with different database systems optimized for specific use cases. This document provides complete schema definitions, relationships, and design rationale.
Database Systems
System | Use Case | Data Type | Volume |
---|---|---|---|
PostgreSQL | Transactional data, ACID operations | Structured | 100TB |
MongoDB | Documents, configurations, logs | Semi-structured | 500TB |
Redis | Caching, sessions, real-time data | Key-value | 10TB |
Pinecone | Vector embeddings | Vectors | 1B vectors |
Neo4j | Knowledge graphs | Graph | 10B nodes |
ClickHouse | Analytics, time-series | Columnar | 1PB |
S3 | Binary objects, backups | Blobs | Unlimited |
PostgreSQL Schema
Core Domain Model
|
|
MongoDB Schema
Collections and Document Structures
|
|
Neo4j Graph Schema
Node and Relationship Definitions
// =============================================
// Node Definitions
// =============================================
// User Node
CREATE CONSTRAINT user_id_unique ON (u:User) ASSERT u.id IS UNIQUE;
CREATE INDEX user_email FOR (u:User) ON (u.email);
// Agent Node
CREATE CONSTRAINT agent_id_unique ON (a:Agent) ASSERT a.id IS UNIQUE;
CREATE INDEX agent_name FOR (a:Agent) ON (a.name);
CREATE INDEX agent_type FOR (a:Agent) ON (a.type);
// Document Node
CREATE CONSTRAINT doc_id_unique ON (d:Document) ASSERT d.id IS UNIQUE;
CREATE INDEX doc_title FOR (d:Document) ON (d.title);
// Concept Node (Knowledge Graph)
CREATE CONSTRAINT concept_id_unique ON (c:Concept) ASSERT c.id IS UNIQUE;
CREATE INDEX concept_name FOR (c:Concept) ON (c.name);
// Entity Node
CREATE CONSTRAINT entity_id_unique ON (e:Entity) ASSERT e.id IS UNIQUE;
CREATE INDEX entity_name FOR (e:Entity) ON (e.name);
CREATE INDEX entity_type FOR (e:Entity) ON (e.type);
// Topic Node
CREATE CONSTRAINT topic_id_unique ON (t:Topic) ASSERT t.id IS UNIQUE;
CREATE INDEX topic_name FOR (t:Topic) ON (t.name);
// =============================================
// Relationship Definitions
// =============================================
// User relationships
(:User)-[:OWNS]->(:Agent)
(:User)-[:CREATED]->(:Document)
(:User)-[:MEMBER_OF]->(:Organization)
(:User)-[:HAS_CONVERSATION]->(:Conversation)
// Agent relationships
(:Agent)-[:USES]->(:Model)
(:Agent)-[:HAS_CAPABILITY]->(:Capability)
(:Agent)-[:PROCESSES]->(:Document)
(:Agent)-[:RESPONDS_TO]->(:User)
(:Agent)-[:DEPENDS_ON]->(:Agent)
// Document relationships
(:Document)-[:CONTAINS]->(:Concept)
(:Document)-[:MENTIONS]->(:Entity)
(:Document)-[:RELATES_TO]->(:Topic)
(:Document)-[:REFERENCES]->(:Document)
(:Document)-[:CONTRADICTS]->(:Document)
// Knowledge relationships
(:Concept)-[:IS_A]->(:Concept)
(:Concept)-[:PART_OF]->(:Concept)
(:Concept)-[:RELATED_TO {weight: 0.8}]->(:Concept)
(:Entity)-[:LOCATED_IN]->(:Location)
(:Entity)-[:WORKS_FOR]->(:Organization)
// =============================================
// Graph Algorithms
// =============================================
// PageRank for important concepts
CALL gds.pageRank.stream('knowledge-graph')
YIELD nodeId, score
MATCH (n) WHERE id(n) = nodeId
SET n.importance = score;
// Community detection for topic clustering
CALL gds.louvain.stream('knowledge-graph')
YIELD nodeId, communityId
MATCH (n) WHERE id(n) = nodeId
SET n.community = communityId;
// Similarity for document recommendations
CALL gds.nodeSimilarity.stream('document-graph')
YIELD node1, node2, similarity
WHERE similarity > 0.8
MATCH (d1:Document), (d2:Document)
WHERE id(d1) = node1 AND id(d2) = node2
CREATE (d1)-[:SIMILAR_TO {score: similarity}]->(d2);
// =============================================
// Complex Queries
// =============================================
// Find knowledge paths between concepts
MATCH path = shortestPath(
(c1:Concept {name: $concept1})-[*..5]-(c2:Concept {name: $concept2})
)
RETURN path;
// Get agent collaboration network
MATCH (a1:Agent)-[:DEPENDS_ON*1..3]-(a2:Agent)
WHERE a1.workspace_id = $workspaceId
RETURN a1, a2, count(*) as collaborations
ORDER BY collaborations DESC;
// Knowledge graph expansion
MATCH (d:Document {id: $documentId})
OPTIONAL MATCH (d)-[:CONTAINS]->(c:Concept)
OPTIONAL MATCH (c)-[:RELATED_TO*1..2]-(related:Concept)
OPTIONAL MATCH (d)-[:MENTIONS]->(e:Entity)
RETURN d, collect(DISTINCT c) as concepts,
collect(DISTINCT related) as relatedConcepts,
collect(DISTINCT e) as entities;
This schema specification is version controlled and requires approval for changes.