database

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound          = errors.New("record not found")
	ErrAlreadyExists     = errors.New("record already exists")
	ErrInvalidInput      = errors.New("invalid input")
	ErrDatabase          = errors.New("database error")
	ErrInvalidVersion    = errors.New("invalid version: cannot publish duplicate version")
	ErrMaxServersReached = errors.New("maximum number of versions for this server reached (10000): please reach out at https://github.com/modelcontextprotocol/registry to explain your use case")
)

Common database errors

Functions

func InTransactionT added in v1.2.1

func InTransactionT[T any](ctx context.Context, db Database, fn func(ctx context.Context, tx pgx.Tx) (T, error)) (T, error)

InTransactionT is a generic helper that wraps InTransaction for functions returning a value This exists because Go does not support generic methods on interfaces - only the Database interface method InTransaction (without generics) can exist, so we provide this generic wrapper function. This is a common pattern in Go for working around this language limitation.

Types

type Database

type Database interface {
	// CreateServer inserts a new server version with official metadata
	CreateServer(ctx context.Context, tx pgx.Tx, serverJSON *apiv0.ServerJSON, officialMeta *apiv0.RegistryExtensions) (*apiv0.ServerResponse, error)
	// UpdateServer updates an existing server record
	UpdateServer(ctx context.Context, tx pgx.Tx, serverName, version string, serverJSON *apiv0.ServerJSON) (*apiv0.ServerResponse, error)
	// SetServerStatus updates the status of a specific server version
	SetServerStatus(ctx context.Context, tx pgx.Tx, serverName, version string, status string) (*apiv0.ServerResponse, error)
	// ListServers retrieve server entries with optional filtering
	ListServers(ctx context.Context, tx pgx.Tx, filter *ServerFilter, cursor string, limit int) ([]*apiv0.ServerResponse, string, error)
	// GetServerByName retrieve a single server by its name
	GetServerByName(ctx context.Context, tx pgx.Tx, serverName string) (*apiv0.ServerResponse, error)
	// GetServerByNameAndVersion retrieve specific version of a server by server name and version
	GetServerByNameAndVersion(ctx context.Context, tx pgx.Tx, serverName string, version string) (*apiv0.ServerResponse, error)
	// GetAllVersionsByServerName retrieve all versions of a server by server name
	GetAllVersionsByServerName(ctx context.Context, tx pgx.Tx, serverName string) ([]*apiv0.ServerResponse, error)
	// GetCurrentLatestVersion retrieve the current latest version of a server by server name
	GetCurrentLatestVersion(ctx context.Context, tx pgx.Tx, serverName string) (*apiv0.ServerResponse, error)
	// CountServerVersions count the number of versions for a server
	CountServerVersions(ctx context.Context, tx pgx.Tx, serverName string) (int, error)
	// CheckVersionExists check if a specific version exists for a server
	CheckVersionExists(ctx context.Context, tx pgx.Tx, serverName, version string) (bool, error)
	// UnmarkAsLatest marks the current latest version of a server as no longer latest
	UnmarkAsLatest(ctx context.Context, tx pgx.Tx, serverName string) error
	// AcquirePublishLock acquires an exclusive advisory lock for publishing a server
	// This prevents race conditions when multiple versions are published concurrently
	AcquirePublishLock(ctx context.Context, tx pgx.Tx, serverName string) error
	// InTransaction executes a function within a database transaction
	InTransaction(ctx context.Context, fn func(ctx context.Context, tx pgx.Tx) error) error
	// Close closes the database connection
	Close() error
}

Database defines the interface for database operations

func NewTestDB added in v1.1.0

func NewTestDB(t *testing.T) Database

NewTestDB creates an isolated PostgreSQL database for each test by copying a template. The template database has migrations pre-applied, so each test is fast. Requires PostgreSQL to be running on localhost:5432 (e.g., via docker-compose).

type Executor added in v1.2.1

type Executor interface {
	Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

Executor is an interface for executing queries (satisfied by both pgx.Tx and pgxpool.Pool)

type Migration

type Migration struct {
	Version int
	Name    string
	SQL     string
}

Migration represents a database migration

type Migrator

type Migrator struct {
	// contains filtered or unexported fields
}

Migrator handles database migrations

func NewMigrator

func NewMigrator(conn *pgx.Conn) *Migrator

NewMigrator creates a new migrator instance

func (*Migrator) Migrate

func (m *Migrator) Migrate(ctx context.Context) error

Migrate runs all pending migrations

type PostgreSQL

type PostgreSQL struct {
	// contains filtered or unexported fields
}

PostgreSQL is an implementation of the Database interface using PostgreSQL

func NewPostgreSQL

func NewPostgreSQL(ctx context.Context, connectionURI string) (*PostgreSQL, error)

NewPostgreSQL creates a new instance of the PostgreSQL database

func (*PostgreSQL) AcquirePublishLock added in v1.2.1

func (db *PostgreSQL) AcquirePublishLock(ctx context.Context, tx pgx.Tx, serverName string) error

AcquirePublishLock acquires an exclusive advisory lock for publishing a server This prevents race conditions when multiple versions are published concurrently Using pg_advisory_xact_lock which auto-releases on transaction end

func (*PostgreSQL) CheckVersionExists added in v1.2.1

func (db *PostgreSQL) CheckVersionExists(ctx context.Context, tx pgx.Tx, serverName, version string) (bool, error)

CheckVersionExists checks if a specific version exists for a server

func (*PostgreSQL) Close

func (db *PostgreSQL) Close() error

Close closes the database connection

func (*PostgreSQL) CountServerVersions added in v1.2.1

func (db *PostgreSQL) CountServerVersions(ctx context.Context, tx pgx.Tx, serverName string) (int, error)

CountServerVersions counts the number of versions for a server

func (*PostgreSQL) CreateServer

func (db *PostgreSQL) CreateServer(ctx context.Context, tx pgx.Tx, serverJSON *apiv0.ServerJSON, officialMeta *apiv0.RegistryExtensions) (*apiv0.ServerResponse, error)

CreateServer inserts a new server version with official metadata

func (*PostgreSQL) GetAllVersionsByServerName added in v1.2.1

func (db *PostgreSQL) GetAllVersionsByServerName(ctx context.Context, tx pgx.Tx, serverName string) ([]*apiv0.ServerResponse, error)

GetAllVersionsByServerName retrieves all versions of a server by server name

func (*PostgreSQL) GetCurrentLatestVersion added in v1.2.1

func (db *PostgreSQL) GetCurrentLatestVersion(ctx context.Context, tx pgx.Tx, serverName string) (*apiv0.ServerResponse, error)

GetCurrentLatestVersion retrieves the current latest version of a server by server name

func (*PostgreSQL) GetServerByName added in v1.2.1

func (db *PostgreSQL) GetServerByName(ctx context.Context, tx pgx.Tx, serverName string) (*apiv0.ServerResponse, error)

GetServerByName retrieves the latest version of a server by server name

func (*PostgreSQL) GetServerByNameAndVersion added in v1.2.1

func (db *PostgreSQL) GetServerByNameAndVersion(ctx context.Context, tx pgx.Tx, serverName string, version string) (*apiv0.ServerResponse, error)

GetServerByNameAndVersion retrieves a specific version of a server by server name and version

func (*PostgreSQL) InTransaction added in v1.2.1

func (db *PostgreSQL) InTransaction(ctx context.Context, fn func(ctx context.Context, tx pgx.Tx) error) error

InTransaction executes a function within a database transaction

func (*PostgreSQL) ListServers added in v1.2.1

func (db *PostgreSQL) ListServers(
	ctx context.Context,
	tx pgx.Tx,
	filter *ServerFilter,
	cursor string,
	limit int,
) ([]*apiv0.ServerResponse, string, error)

func (*PostgreSQL) SetServerStatus added in v1.2.1

func (db *PostgreSQL) SetServerStatus(ctx context.Context, tx pgx.Tx, serverName, version string, status string) (*apiv0.ServerResponse, error)

SetServerStatus updates the status of a specific server version

func (*PostgreSQL) UnmarkAsLatest added in v1.2.1

func (db *PostgreSQL) UnmarkAsLatest(ctx context.Context, tx pgx.Tx, serverName string) error

UnmarkAsLatest marks the current latest version of a server as no longer latest

func (*PostgreSQL) UpdateServer

func (db *PostgreSQL) UpdateServer(ctx context.Context, tx pgx.Tx, serverName, version string, serverJSON *apiv0.ServerJSON) (*apiv0.ServerResponse, error)

UpdateServer updates an existing server record with new server details

type ServerFilter

type ServerFilter struct {
	Name          *string    // for finding versions of same server
	RemoteURL     *string    // for duplicate URL detection
	UpdatedSince  *time.Time // for incremental sync filtering
	SubstringName *string    // for substring search on name
	Version       *string    // for exact version matching
	IsLatest      *bool      // for filtering latest versions only
}

ServerFilter defines filtering options for server queries

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL