Documentation
¶
Overview ¶
Package database provides database access functionality for the MCP system.
Index ¶
- Variables
- func NewRelationshipRepository(db *Database) relationship.Repository
- func NewUUID() string
- func NormalizeVector(vector []float32, method string) ([]float32, error)
- type Config
- type ContextReference
- type Database
- func (d *Database) Close() error
- func (db *Database) CreateContext(ctx context.Context, contextData *models.Context) error
- func (d *Database) CreateContextReference(ctx context.Context, context *models.Context) error
- func (d *Database) CreateContextReferenceTable(ctx context.Context) error
- func (d *Database) DB() *sqlx.DB
- func (db *Database) DeleteContext(ctx context.Context, contextID string) error
- func (d *Database) DeleteContextReference(ctx context.Context, id string) error
- func (db *Database) DeleteGitHubContent(ctx context.Context, owner, repo, contentType, contentID string) error
- func (db *Database) EnsureRelationshipTables(ctx context.Context) error
- func (db *Database) GetContext(ctx context.Context, contextID string) (*models.Context, error)
- func (d *Database) GetContextReference(ctx context.Context, id string) (*ContextReference, error)
- func (d *Database) GetDB() *sqlx.DB
- func (db *Database) GetGitHubContent(ctx context.Context, owner, repo, contentType, contentID string) (*storage.ContentMetadata, error)
- func (db *Database) GetGitHubContentByChecksum(ctx context.Context, checksum string) (*storage.ContentMetadata, error)
- func (db *Database) InitializeTables(ctx context.Context) error
- func (d *Database) ListContextReferences(ctx context.Context, agentID, sessionID string, options map[string]any) ([]*ContextReference, error)
- func (db *Database) ListContexts(ctx context.Context, agentID string, sessionID string, options map[string]any) ([]*models.Context, error)
- func (db *Database) ListGitHubContent(ctx context.Context, owner, repo, contentType string, limit int) ([]*storage.ContentMetadata, error)
- func (d *Database) Ping() error
- func (d *Database) RefreshConnection(ctx context.Context) error
- func (d *Database) RunTransaction(ctx context.Context, fn func(*sqlx.Tx) error) error
- func (db *Database) SearchContexts(ctx context.Context, agentID string, query string, limit int) ([]*models.Context, error)
- func (db *Database) StoreGitHubContent(ctx context.Context, metadata *storage.ContentMetadata) error
- func (d *Database) Transaction(ctx context.Context, fn func(*sqlx.Tx) error) error
- func (db *Database) UpdateContext(ctx context.Context, contextData *models.Context) error
- func (d *Database) UpdateContextReference(ctx context.Context, context *models.Context) error
- type Embedding
- type EntityRelationshipRecord
- type GitHubContentMetadata
- type MockDatabase
- type RelationshipRepository
- func (r *RelationshipRepository) CreateRelationship(ctx context.Context, relationship *models.EntityRelationship) error
- func (r *RelationshipRepository) DeleteRelationship(ctx context.Context, relationshipID string) error
- func (r *RelationshipRepository) DeleteRelationshipsBetween(ctx context.Context, source models.EntityID, target models.EntityID) error
- func (r *RelationshipRepository) GetDirectRelationships(ctx context.Context, entityID models.EntityID, direction string, ...) ([]*models.EntityRelationship, error)
- func (r *RelationshipRepository) GetRelationship(ctx context.Context, relationshipID string) (*models.EntityRelationship, error)
- func (r *RelationshipRepository) GetRelationshipsByType(ctx context.Context, relType models.RelationshipType, limit int, offset int) ([]*models.EntityRelationship, error)
- func (r *RelationshipRepository) UpdateRelationship(ctx context.Context, relationship *models.EntityRelationship) error
- type Repository
- type Transaction
- type Tx
- type UnitOfWork
- type VectorConfig
- type VectorDatabase
- func (vdb *VectorDatabase) BatchDeleteEmbeddings(ctx context.Context, tx *sqlx.Tx, contentType string, contentID string, ...) (int, error)
- func (vdb *VectorDatabase) CalculateSimilarity(ctx context.Context, vector1, vector2 []float32, method string) (float64, error)
- func (vdb *VectorDatabase) CheckVectorDimensions(ctx context.Context) ([]int, error)
- func (vdb *VectorDatabase) Close() error
- func (vdb *VectorDatabase) CreateVector(ctx context.Context, vector []float32) (string, error)
- func (vdb *VectorDatabase) DeleteEmbedding(ctx context.Context, tx *sqlx.Tx, id string) error
- func (vdb *VectorDatabase) EnsureSchema(ctx context.Context) error
- func (vdb *VectorDatabase) GetEmbeddingByID(ctx context.Context, tx *sqlx.Tx, id string) (*Embedding, error)
- func (vdb *VectorDatabase) GetVectorSearchConfig() *VectorConfig
- func (vdb *VectorDatabase) Initialize(ctx context.Context) error
- func (vdb *VectorDatabase) SearchEmbeddings(ctx context.Context, tx *sqlx.Tx, queryVector []float32, contextID string, ...) ([]*Embedding, error)
- func (vdb *VectorDatabase) StoreEmbedding(ctx context.Context, tx *sqlx.Tx, embedding *Embedding) error
- func (vdb *VectorDatabase) Transaction(ctx context.Context, fn func(tx *sqlx.Tx) error) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrMissingAWSRegion = errors.New("AWS region is required when using IAM authentication") ErrMissingRDSHost = errors.New("RDS host is required when using AWS RDS") ErrInvalidDatabaseConfig = errors.New("invalid database configuration: missing required fields") ErrNotFound = errors.New("record not found") ErrDuplicateKey = errors.New("duplicate key violation") )
Common errors
Functions ¶
func NewRelationshipRepository ¶
func NewRelationshipRepository(db *Database) relationship.Repository
NewRelationshipRepository creates a new PostgreSQL repository for entity relationships
Types ¶
type Config ¶
type Config struct {
// Core database settings
Driver string
DSN string
Host string
Port int
Database string
Username string
Password string
SSLMode string
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime time.Duration
// TLS Configuration
TLS *securitytls.Config
// Timeout configurations (best practice)
QueryTimeout time.Duration // Default: 30s
ConnectTimeout time.Duration // Default: 10s
// AWS RDS specific settings (optional)
UseAWS bool
UseIAM bool
AWSRegion string
AWSRoleARN string
// RDS-specific configuration
RDSHost string
RDSPort int
RDSDatabase string
RDSUsername string
RDSTokenExpiration int // seconds
RDSEnablePooling bool
RDSMinPoolSize int
RDSMaxPoolSize int
RDSConnectionTimeout int // seconds
// Migration settings
AutoMigrate bool
MigrationsPath string
FailOnMigrationError bool
}
Config defines what the database package needs - no external imports!
type ContextReference ¶
type ContextReference struct {
ID string `db:"id" json:"id"`
AgentID string `db:"agent_id" json:"agent_id"`
ModelID string `db:"model_id" json:"model_id"`
SessionID string `db:"session_id" json:"session_id"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
ExpiresAt time.Time `db:"expires_at" json:"expires_at"`
TokenCount int `db:"token_count" json:"token_count"`
MessageCount int `db:"message_count" json:"message_count"`
StoragePath string `db:"storage_path" json:"storage_path"`
}
ContextReference represents a reference to a context stored in S3 or another storage
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database represents the database access layer
func NewDatabase ¶
NewDatabase creates a new database connection
func NewDatabaseWithConnection ¶
NewDatabaseWithConnection creates a new Database instance with an existing connection
func NewTestDatabase ¶
NewTestDatabase is a helper that creates a new test database with a background context Maintained for backward compatibility with existing code
func NewTestDatabaseWithContext ¶
NewTestDatabaseWithContext creates a new database instance for testing purposes.
func (*Database) CreateContext ¶
CreateContext creates a new context in the database
func (*Database) CreateContextReference ¶
CreateContextReference creates a new context reference
func (*Database) CreateContextReferenceTable ¶
CreateContextReferenceTable creates the context_references table if it doesn't exist
func (*Database) DB ¶
DB returns the underlying sqlx.DB instance (compatible with the field name used in the API)
func (*Database) DeleteContext ¶
DeleteContext deletes a context from the database
func (*Database) DeleteContextReference ¶
DeleteContextReference deletes a context reference by ID
func (*Database) DeleteGitHubContent ¶
func (db *Database) DeleteGitHubContent(ctx context.Context, owner, repo, contentType, contentID string) error
DeleteGitHubContent deletes GitHub content metadata from the database
func (*Database) EnsureRelationshipTables ¶
EnsureRelationshipTables creates the necessary database tables for entity relationships
func (*Database) GetContext ¶
GetContext retrieves a context from the database
func (*Database) GetContextReference ¶
GetContextReference retrieves a context reference by ID
func (*Database) GetGitHubContent ¶
func (db *Database) GetGitHubContent(ctx context.Context, owner, repo, contentType, contentID string) (*storage.ContentMetadata, error)
GetGitHubContent retrieves GitHub content metadata from the database
func (*Database) GetGitHubContentByChecksum ¶
func (db *Database) GetGitHubContentByChecksum(ctx context.Context, checksum string) (*storage.ContentMetadata, error)
GetGitHubContentByChecksum retrieves GitHub content metadata from the database using checksum
func (*Database) InitializeTables ¶
InitializeTables initializes all required database tables
func (*Database) ListContextReferences ¶
func (d *Database) ListContextReferences(ctx context.Context, agentID, sessionID string, options map[string]any) ([]*ContextReference, error)
ListContextReferences lists context references filtered by the given criteria
func (*Database) ListContexts ¶
func (db *Database) ListContexts(ctx context.Context, agentID string, sessionID string, options map[string]any) ([]*models.Context, error)
ListContexts lists contexts for an agent
func (*Database) ListGitHubContent ¶
func (db *Database) ListGitHubContent(ctx context.Context, owner, repo, contentType string, limit int) ([]*storage.ContentMetadata, error)
ListGitHubContent lists GitHub content metadata for a repository
func (*Database) RefreshConnection ¶
RefreshConnection refreshes the database connection (especially for IAM auth)
func (*Database) RunTransaction ¶
Transaction is a helper function for running a transaction
func (*Database) SearchContexts ¶
func (db *Database) SearchContexts(ctx context.Context, agentID string, query string, limit int) ([]*models.Context, error)
SearchContexts searches for contexts based on a text query
func (*Database) StoreGitHubContent ¶
func (db *Database) StoreGitHubContent(ctx context.Context, metadata *storage.ContentMetadata) error
StoreGitHubContent stores GitHub content metadata in the database
func (*Database) Transaction ¶
Transaction executes a function within a database transaction
func (*Database) UpdateContext ¶
UpdateContext updates a context in the database
type Embedding ¶
type Embedding struct {
ID string `json:"id" db:"id"`
Vector []float32 `json:"vector" db:"vector"`
Dimensions int `json:"dimensions" db:"dimensions"`
ModelID string `json:"model_id" db:"model_id"`
ContentType string `json:"content_type" db:"content_type"`
ContentID string `json:"content_id" db:"content_id"`
Namespace string `json:"namespace" db:"namespace"`
ContextID string `json:"context_id" db:"context_id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
Metadata map[string]any `json:"metadata" db:"metadata"`
Similarity float64 `json:"similarity" db:"similarity"`
}
Embedding represents a vector embedding in the database
type EntityRelationshipRecord ¶
type EntityRelationshipRecord struct {
ID string `db:"id"`
Type string `db:"relationship_type"`
Direction string `db:"direction"`
SourceType string `db:"source_type"`
SourceOwner string `db:"source_owner"`
SourceRepo string `db:"source_repo"`
SourceID string `db:"source_id"`
TargetType string `db:"target_type"`
TargetOwner string `db:"target_owner"`
TargetRepo string `db:"target_repo"`
TargetID string `db:"target_id"`
Strength float64 `db:"strength"`
Context string `db:"context"`
Metadata []byte `db:"metadata"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
EntityRelationshipRecord represents the database record for an entity relationship
type GitHubContentMetadata ¶
type GitHubContentMetadata struct {
ID string `db:"id"`
Owner string `db:"owner"`
Repo string `db:"repo"`
ContentType string `db:"content_type"`
ContentID string `db:"content_id"`
Checksum string `db:"checksum"`
URI string `db:"uri"`
Size int64 `db:"size"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
ExpiresAt time.Time `db:"expires_at"`
Metadata []byte `db:"metadata"`
}
GitHubContentMetadata represents a database record for GitHub content metadata
type MockDatabase ¶
MockDatabase provides a mock implementation of Database for testing
func (*MockDatabase) Transaction ¶
Transaction executes the mock transaction function
type RelationshipRepository ¶
type RelationshipRepository struct {
// contains filtered or unexported fields
}
RelationshipRepository implements relationship.Repository
func (*RelationshipRepository) CreateRelationship ¶
func (r *RelationshipRepository) CreateRelationship(ctx context.Context, relationship *models.EntityRelationship) error
CreateRelationship creates a new entity relationship
func (*RelationshipRepository) DeleteRelationship ¶
func (r *RelationshipRepository) DeleteRelationship(ctx context.Context, relationshipID string) error
DeleteRelationship removes a relationship by ID
func (*RelationshipRepository) DeleteRelationshipsBetween ¶
func (r *RelationshipRepository) DeleteRelationshipsBetween(ctx context.Context, source models.EntityID, target models.EntityID) error
DeleteRelationshipsBetween removes all relationships between two entities
func (*RelationshipRepository) GetDirectRelationships ¶
func (r *RelationshipRepository) GetDirectRelationships( ctx context.Context, entityID models.EntityID, direction string, relTypes []models.RelationshipType, ) ([]*models.EntityRelationship, error)
GetDirectRelationships gets direct relationships for an entity
func (*RelationshipRepository) GetRelationship ¶
func (r *RelationshipRepository) GetRelationship(ctx context.Context, relationshipID string) (*models.EntityRelationship, error)
GetRelationship retrieves a relationship by ID
func (*RelationshipRepository) GetRelationshipsByType ¶
func (r *RelationshipRepository) GetRelationshipsByType( ctx context.Context, relType models.RelationshipType, limit int, offset int, ) ([]*models.EntityRelationship, error)
GetRelationshipsByType gets relationships of a specific type
func (*RelationshipRepository) UpdateRelationship ¶
func (r *RelationshipRepository) UpdateRelationship(ctx context.Context, relationship *models.EntityRelationship) error
UpdateRelationship updates an existing relationship
type Repository ¶
type Repository interface {
FindByID(ctx context.Context, id string) (any, error)
FindAll(ctx context.Context, options models.QueryOptions) (any, error)
Create(ctx context.Context, entity any) error
Update(ctx context.Context, entity any) error
Delete(ctx context.Context, id string) error
}
Repository interface for data access
type Transaction ¶
type Transaction interface {
// Exec executes a query without returning any rows
Exec(query string, args ...interface{}) (sql.Result, error)
// ExecContext executes a query with context
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
// Query executes a query that returns rows
Query(query string, args ...interface{}) (*sql.Rows, error)
// QueryContext executes a query with context
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
// QueryRow executes a query that returns at most one row
QueryRow(query string, args ...interface{}) *sql.Row
// QueryRowContext executes a query with context that returns at most one row
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
// Get executes a query and scans the result into dest (sqlx)
Get(dest interface{}, query string, args ...interface{}) error
// Select executes a query and scans the results into dest (sqlx)
Select(dest interface{}, query string, args ...interface{}) error
// NamedExec executes a named query (sqlx)
NamedExec(query string, arg interface{}) (sql.Result, error)
// Savepoint creates a savepoint with the given name
Savepoint(name string) error
// RollbackToSavepoint rolls back to the named savepoint
RollbackToSavepoint(name string) error
// ReleaseSavepoint releases the named savepoint
ReleaseSavepoint(name string) error
// Commit commits the transaction
Commit() error
// Rollback rolls back the transaction
Rollback() error
// ID returns the unique transaction ID for tracking
ID() string
}
Transaction represents a database transaction with additional functionality
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx represents a database transaction
type UnitOfWork ¶
type UnitOfWork interface {
// BeginTx starts a new transaction with the given context and options
BeginTx(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
// Execute runs a function within a transaction, automatically handling commit/rollback
Execute(ctx context.Context, fn func(tx Transaction) error) error
// ExecuteWithOptions runs a function within a transaction with custom options
ExecuteWithOptions(ctx context.Context, opts *sql.TxOptions, fn func(tx Transaction) error) error
}
UnitOfWork represents a unit of work pattern for managing database transactions
func NewUnitOfWork ¶
func NewUnitOfWork(db *sqlx.DB, logger observability.Logger, metrics observability.MetricsClient) UnitOfWork
NewUnitOfWork creates a new UnitOfWork instance
type VectorConfig ¶
type VectorConfig struct {
Enabled bool
ExtensionSchema string
IndexType string
DistanceMetric string
MaxDimensions int
DefaultDimensions int
}
VectorConfig contains vector-specific database configuration
type VectorDatabase ¶
type VectorDatabase struct {
// contains filtered or unexported fields
}
VectorDatabase provides specialized database operations for vector data
func NewVectorDatabase ¶
func NewVectorDatabase(db *sqlx.DB, cfg any, logger observability.Logger) (*VectorDatabase, error)
NewVectorDatabase creates a new vector database
func (*VectorDatabase) BatchDeleteEmbeddings ¶
func (vdb *VectorDatabase) BatchDeleteEmbeddings( ctx context.Context, tx *sqlx.Tx, contentType string, contentID string, contextID string, ) (int, error)
BatchDeleteEmbeddings deletes multiple embeddings matching criteria
func (*VectorDatabase) CalculateSimilarity ¶
func (vdb *VectorDatabase) CalculateSimilarity(ctx context.Context, vector1, vector2 []float32, method string) (float64, error)
CalculateSimilarity calculates the similarity between two vectors
func (*VectorDatabase) CheckVectorDimensions ¶
func (vdb *VectorDatabase) CheckVectorDimensions(ctx context.Context) ([]int, error)
CheckVectorDimensions returns the available vector dimensions in the database
func (*VectorDatabase) Close ¶
func (vdb *VectorDatabase) Close() error
Close closes the vector database connection pool
func (*VectorDatabase) CreateVector ¶
CreateVector creates a new vector from float32 array
func (*VectorDatabase) DeleteEmbedding ¶
DeleteEmbedding deletes an embedding from the database
func (*VectorDatabase) EnsureSchema ¶
func (vdb *VectorDatabase) EnsureSchema(ctx context.Context) error
EnsureSchema ensures the vector database schema exists
func (*VectorDatabase) GetEmbeddingByID ¶
func (vdb *VectorDatabase) GetEmbeddingByID(ctx context.Context, tx *sqlx.Tx, id string) (*Embedding, error)
GetEmbeddingByID retrieves an embedding by ID
func (*VectorDatabase) GetVectorSearchConfig ¶
func (vdb *VectorDatabase) GetVectorSearchConfig() *VectorConfig
GetVectorSearchConfig returns the current vector search configuration
func (*VectorDatabase) Initialize ¶
func (vdb *VectorDatabase) Initialize(ctx context.Context) error
Initialize ensures the vector database is properly set up
func (*VectorDatabase) SearchEmbeddings ¶
func (vdb *VectorDatabase) SearchEmbeddings( ctx context.Context, tx *sqlx.Tx, queryVector []float32, contextID string, modelID string, limit int, similarityThreshold float64, ) ([]*Embedding, error)
SearchEmbeddings searches for similar embeddings in the vector database
func (*VectorDatabase) StoreEmbedding ¶
func (vdb *VectorDatabase) StoreEmbedding(ctx context.Context, tx *sqlx.Tx, embedding *Embedding) error
StoreEmbedding stores an embedding vector in the database
func (*VectorDatabase) Transaction ¶
Transaction runs a vector database operation in a transaction