Documentation
¶
Overview ¶
Package database provides SQLite database operations for the LLM Proxy.
Package database provides database operations for the LLM Proxy.
Index ¶
- Variables
- func DBInitForTests(d *DB) error
- func ExportTokenData(t Token) token.TokenData
- func MigrationsPathForDriver(driver DriverType) (string, error)
- func TestMockTokenStore_EdgeCases(t *testing.T)
- func ToProxyProject(dbProject Project) proxy.Project
- type AuditEvent
- type AuditEventFilters
- type AuditStore
- type Config
- type DB
- func (d *DB) BackupDatabase(ctx context.Context, backupPath string) error
- func (d *DB) CleanExpiredTokens(ctx context.Context) (int64, error)
- func (d *DB) Close() error
- func (d *DB) CountAuditEvents(ctx context.Context, filters AuditEventFilters) (int, error)
- func (d *DB) CreateProject(ctx context.Context, p proxy.Project) error
- func (d *DB) CreateToken(ctx context.Context, token Token) error
- func (d *DB) DB() *sql.DB
- func (d *DB) DBCreateProject(ctx context.Context, project Project) error
- func (d *DB) DBDeleteProject(ctx context.Context, projectID string) error
- func (d *DB) DBGetProjectByID(ctx context.Context, projectID string) (Project, error)
- func (d *DB) DBListProjects(ctx context.Context) ([]Project, error)
- func (d *DB) DBUpdateProject(ctx context.Context, project Project) error
- func (d *DB) DeleteProject(ctx context.Context, id string) error
- func (d *DB) DeleteToken(ctx context.Context, tokenID string) error
- func (d *DB) Driver() DriverType
- func (d *DB) ExecContextRebound(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (d *DB) GetAPIKeyForProject(ctx context.Context, projectID string) (string, error)
- func (d *DB) GetAuditEventByID(ctx context.Context, id string) (*AuditEvent, error)
- func (d *DB) GetProjectActive(ctx context.Context, projectID string) (bool, error)
- func (d *DB) GetProjectByID(ctx context.Context, id string) (proxy.Project, error)
- func (d *DB) GetProjectByName(ctx context.Context, name string) (Project, error)
- func (d *DB) GetStats(ctx context.Context) (map[string]interface{}, error)
- func (d *DB) GetTokenByID(ctx context.Context, id string) (Token, error)
- func (d *DB) GetTokenByToken(ctx context.Context, tokenString string) (Token, error)
- func (d *DB) GetTokensByProjectID(ctx context.Context, projectID string) ([]Token, error)
- func (d *DB) HealthCheck(ctx context.Context) error
- func (d *DB) IncrementCacheHitCount(ctx context.Context, tokenID string, delta int) error
- func (d *DB) IncrementCacheHitCountBatch(ctx context.Context, deltas map[string]int) error
- func (d *DB) IncrementTokenUsage(ctx context.Context, tokenID string) error
- func (d *DB) IncrementTokenUsageBatch(ctx context.Context, deltas map[string]int, lastUsedAt time.Time) error
- func (d *DB) IsTokenValid(ctx context.Context, tokenID string) (bool, error)
- func (d *DB) ListAuditEvents(ctx context.Context, filters AuditEventFilters) ([]AuditEvent, error)
- func (d *DB) ListProjects(ctx context.Context) ([]proxy.Project, error)
- func (d *DB) ListTokens(ctx context.Context) ([]Token, error)
- func (d *DB) MaintainDatabase(ctx context.Context) error
- func (d *DB) Placeholder(n int) string
- func (d *DB) PlaceholderList(n int) string
- func (d *DB) Placeholders(n int) []string
- func (d *DB) QueryContextRebound(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (d *DB) QueryRowContextRebound(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (d *DB) RebindQuery(query string) string
- func (d *DB) StoreAuditEvent(ctx context.Context, event *audit.Event) error
- func (d *DB) Transaction(ctx context.Context, fn func(*sql.Tx) error) error
- func (d *DB) UpdateProject(ctx context.Context, p proxy.Project) error
- func (d *DB) UpdateToken(ctx context.Context, token Token) error
- type DBTokenStoreAdapter
- func (a *DBTokenStoreAdapter) CreateToken(ctx context.Context, td token.TokenData) error
- func (a *DBTokenStoreAdapter) DeleteToken(ctx context.Context, tokenID string) error
- func (a *DBTokenStoreAdapter) GetTokenByID(ctx context.Context, id string) (token.TokenData, error)
- func (a *DBTokenStoreAdapter) GetTokenByToken(ctx context.Context, tokenString string) (token.TokenData, error)
- func (a *DBTokenStoreAdapter) GetTokensByProjectID(ctx context.Context, projectID string) ([]token.TokenData, error)
- func (a *DBTokenStoreAdapter) IncrementTokenUsage(ctx context.Context, tokenID string) error
- func (a *DBTokenStoreAdapter) ListTokens(ctx context.Context) ([]token.TokenData, error)
- func (a *DBTokenStoreAdapter) RevokeBatchTokens(ctx context.Context, tokenIDs []string) (int, error)
- func (a *DBTokenStoreAdapter) RevokeExpiredTokens(ctx context.Context) (int, error)
- func (a *DBTokenStoreAdapter) RevokeProjectTokens(ctx context.Context, projectID string) (int, error)
- func (a *DBTokenStoreAdapter) RevokeToken(ctx context.Context, tokenID string) error
- func (a *DBTokenStoreAdapter) UpdateToken(ctx context.Context, td token.TokenData) error
- type DriverType
- type FullConfig
- type MockProjectStore
- func (m *MockProjectStore) CreateMockProject(projectID, name, apiKey string) (Project, error)
- func (m *MockProjectStore) CreateProject(ctx context.Context, p proxy.Project) error
- func (m *MockProjectStore) DBCreateProject(ctx context.Context, project Project) error
- func (m *MockProjectStore) DBDeleteProject(ctx context.Context, projectID string) error
- func (m *MockProjectStore) DBGetProjectByID(ctx context.Context, projectID string) (Project, error)
- func (m *MockProjectStore) DBListProjects(ctx context.Context) ([]Project, error)
- func (m *MockProjectStore) DBUpdateProject(ctx context.Context, project Project) error
- func (m *MockProjectStore) DeleteProject(ctx context.Context, id string) error
- func (m *MockProjectStore) GetAPIKeyForProject(ctx context.Context, projectID string) (string, error)
- func (m *MockProjectStore) GetProjectActive(ctx context.Context, projectID string) (bool, error)
- func (m *MockProjectStore) GetProjectByID(ctx context.Context, id string) (proxy.Project, error)
- func (m *MockProjectStore) ListProjects(ctx context.Context) ([]proxy.Project, error)
- func (m *MockProjectStore) UpdateProject(ctx context.Context, p proxy.Project) error
- type MockTokenStore
- func (m *MockTokenStore) CleanExpiredTokens(ctx context.Context) (int64, error)
- func (m *MockTokenStore) CreateMockToken(tokenID, projectID string, expiresIn time.Duration, isActive bool, ...) (Token, error)
- func (m *MockTokenStore) CreateToken(ctx context.Context, token Token) error
- func (m *MockTokenStore) DeleteToken(ctx context.Context, tokenID string) error
- func (m *MockTokenStore) GetTokenByID(ctx context.Context, tokenID string) (Token, error)
- func (m *MockTokenStore) GetTokensByProjectID(ctx context.Context, projectID string) ([]Token, error)
- func (m *MockTokenStore) IncrementTokenUsage(ctx context.Context, tokenID string) error
- func (m *MockTokenStore) ListTokens(ctx context.Context) ([]Token, error)
- func (m *MockTokenStore) UpdateToken(ctx context.Context, token Token) error
- type Project
- type Token
- type TokenStoreAdapter
- func (a *TokenStoreAdapter) CreateToken(ctx context.Context, td token.TokenData) error
- func (a *TokenStoreAdapter) GetTokenByID(ctx context.Context, tokenID string) (token.TokenData, error)
- func (a *TokenStoreAdapter) GetTokensByProjectID(ctx context.Context, projectID string) ([]token.TokenData, error)
- func (a *TokenStoreAdapter) IncrementTokenUsage(ctx context.Context, tokenID string) error
- func (a *TokenStoreAdapter) ListTokens(ctx context.Context) ([]token.TokenData, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrProjectNotFound is returned when a project is not found. ErrProjectNotFound = errors.New("project not found") // ErrProjectExists is returned when a project already exists. ErrProjectExists = errors.New("project already exists") )
var ( // ErrTokenNotFound is returned when a token is not found. ErrTokenNotFound = errors.New("token not found") // ErrTokenExists is returned when a token already exists. ErrTokenExists = errors.New("token already exists") )
Functions ¶
func DBInitForTests ¶
DBInitForTests is a helper to ensure schema exists in tests. No-op if db is nil.
func ExportTokenData ¶
func MigrationsPathForDriver ¶
func MigrationsPathForDriver(driver DriverType) (string, error)
MigrationsPathForDriver returns the migrations directory for the given driver type. Note: Only PostgreSQL and MySQL use migrations. SQLite uses schema.sql directly. This ensures CLI and server code share the same dialect-aware lookup logic.
func ToProxyProject ¶
ToProxyProject converts a database.Project to a proxy.Project
Types ¶
type AuditEvent ¶
type AuditEvent struct {
ID string `json:"id"`
Timestamp time.Time `json:"timestamp"`
Action string `json:"action"`
Actor string `json:"actor"`
ProjectID *string `json:"project_id,omitempty"`
RequestID *string `json:"request_id,omitempty"`
CorrelationID *string `json:"correlation_id,omitempty"`
ClientIP *string `json:"client_ip,omitempty"`
Method *string `json:"method,omitempty"`
Path *string `json:"path,omitempty"`
UserAgent *string `json:"user_agent,omitempty"`
Outcome string `json:"outcome"`
Reason *string `json:"reason,omitempty"`
TokenID *string `json:"token_id,omitempty"`
Metadata *string `json:"metadata,omitempty"` // JSON string
}
AuditEvent represents an audit log entry in the database.
type AuditEventFilters ¶
type AuditEventFilters struct {
Action string
ClientIP string
ProjectID string
StartTime *string // RFC3339 format
EndTime *string // RFC3339 format
Outcome string
Actor string
RequestID string
CorrelationID string
Method string
Path string
Search string // Full-text search over reason/metadata
Limit int
Offset int
}
AuditEventFilters provides filtering options for audit event queries
type AuditStore ¶
type AuditStore interface {
StoreAuditEvent(ctx context.Context, event *audit.Event) error
ListAuditEvents(ctx context.Context, filters AuditEventFilters) ([]AuditEvent, error)
CountAuditEvents(ctx context.Context, filters AuditEventFilters) (int, error)
GetAuditEventByID(ctx context.Context, id string) (*AuditEvent, error)
}
AuditStore defines the interface for persisting audit events to database
type Config ¶
type Config struct {
// Path is the path to the SQLite database file.
Path string
// MaxOpenConns is the maximum number of open connections.
MaxOpenConns int
// MaxIdleConns is the maximum number of idle connections.
MaxIdleConns int
// ConnMaxLifetime is the maximum amount of time a connection may be reused.
ConnMaxLifetime time.Duration
}
Config contains the database configuration.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a default database configuration.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents the database connection.
func NewFromConfig ¶
func NewFromConfig(config FullConfig) (*DB, error)
NewFromConfig creates a new database connection based on the configuration.
func (*DB) BackupDatabase ¶
BackupDatabase creates a backup of the database. Note: This function is SQLite-specific. For PostgreSQL, use pg_dump. For MySQL, use mysqldump.
func (*DB) CleanExpiredTokens ¶
CleanExpiredTokens deletes expired tokens from the database.
func (*DB) CountAuditEvents ¶
CountAuditEvents returns the total count of audit events matching the given filters
func (*DB) CreateToken ¶
CreateToken creates a new token in the database. If token.ID is empty, a UUID will be generated automatically.
func (*DB) DBCreateProject ¶
func (*DB) DBDeleteProject ¶
func (*DB) DBGetProjectByID ¶
func (*DB) DBListProjects ¶
Rename CRUD methods for DB store
func (*DB) DBUpdateProject ¶
func (*DB) DeleteToken ¶
DeleteToken deletes a token from the database.
func (*DB) ExecContextRebound ¶
func (d *DB) ExecContextRebound(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
ExecContextRebound executes a query with automatic placeholder rebinding.
func (*DB) GetAPIKeyForProject ¶
GetAPIKeyForProject retrieves the API key for a project by ID
func (*DB) GetAuditEventByID ¶
GetAuditEventByID retrieves a specific audit event by its ID
func (*DB) GetProjectActive ¶
GetProjectActive retrieves the active status for a project by ID
func (*DB) GetProjectByID ¶
func (*DB) GetProjectByName ¶
GetProjectByName retrieves a project by name.
func (*DB) GetTokenByID ¶
GetTokenByID retrieves a token by its UUID.
func (*DB) GetTokenByToken ¶
GetTokenByToken retrieves a token by its token string (for authentication).
func (*DB) GetTokensByProjectID ¶
GetTokensByProjectID retrieves all tokens for a project.
func (*DB) HealthCheck ¶
HealthCheck performs a health check on the database connection. It verifies that the database is reachable and responsive.
func (*DB) IncrementCacheHitCount ¶
IncrementCacheHitCount increments the cache_hit_count for a single token.
func (*DB) IncrementCacheHitCountBatch ¶
IncrementCacheHitCountBatch increments cache_hit_count for multiple tokens in batch. The deltas map has token IDs as keys and increment values as values.
func (*DB) IncrementTokenUsage ¶
IncrementTokenUsage increments the request count and updates the last_used_at timestamp.
func (*DB) IncrementTokenUsageBatch ¶
func (d *DB) IncrementTokenUsageBatch(ctx context.Context, deltas map[string]int, lastUsedAt time.Time) error
IncrementTokenUsageBatch increments request_count for multiple tokens and updates last_used_at. The token IDs are token strings (sk-...).
func (*DB) IsTokenValid ¶
IsTokenValid checks if a token is valid (exists, is active, not expired, and not rate limited).
func (*DB) ListAuditEvents ¶
func (d *DB) ListAuditEvents(ctx context.Context, filters AuditEventFilters) ([]AuditEvent, error)
ListAuditEvents retrieves audit events from the database with optional filtering
func (*DB) ListProjects ¶
--- proxy.ProjectStore interface adapters ---
func (*DB) ListTokens ¶
ListTokens retrieves all tokens from the database.
func (*DB) MaintainDatabase ¶
MaintainDatabase performs regular maintenance on the database. WARNING: VACUUM and ANALYZE can be expensive operations. In production, schedule this function to run periodically (e.g., daily) rather than on every call. The caller is responsible for scheduling.
func (*DB) Placeholder ¶
Placeholder returns the appropriate placeholder for the driver. For SQLite and MySQL: ?, for PostgreSQL: $1, $2, etc.
func (*DB) PlaceholderList ¶
PlaceholderList returns a comma-separated list of placeholders. For n=3: SQLite/MySQL return "?, ?, ?", PostgreSQL returns "$1, $2, $3".
func (*DB) Placeholders ¶
Placeholders returns a slice of placeholders for the driver. For n=3: SQLite/MySQL return ["?", "?", "?"], PostgreSQL returns ["$1", "$2", "$3"].
func (*DB) QueryContextRebound ¶
func (d *DB) QueryContextRebound(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryContextRebound queries multiple rows with automatic placeholder rebinding.
func (*DB) QueryRowContextRebound ¶
func (d *DB) QueryRowContextRebound(ctx context.Context, query string, args ...interface{}) *sql.Row
QueryRowContextRebound queries a single row with automatic placeholder rebinding.
func (*DB) RebindQuery ¶
RebindQuery converts a query from ? placeholders to the appropriate placeholder style for the database driver.
IMPORTANT: This function performs a simple character replacement and does NOT handle ? characters inside SQL string literals (e.g., "WHERE name = 'what?'"). Since this codebase exclusively uses parameterized queries with ? as placeholders, this limitation does not affect normal usage. If you need to use literal ? in string values, use parameterized queries: "WHERE name = ?" with the value passed as an argument.
func (*DB) StoreAuditEvent ¶
StoreAuditEvent persists an audit event to the database
func (*DB) Transaction ¶
Transaction executes the given function within a transaction.
type DBTokenStoreAdapter ¶
type DBTokenStoreAdapter struct {
// contains filtered or unexported fields
}
--- token.TokenStore interface adapter for *DB ---
func NewDBTokenStoreAdapter ¶
func NewDBTokenStoreAdapter(db *DB) *DBTokenStoreAdapter
func (*DBTokenStoreAdapter) CreateToken ¶
func (*DBTokenStoreAdapter) DeleteToken ¶
func (a *DBTokenStoreAdapter) DeleteToken(ctx context.Context, tokenID string) error
DeleteToken completely removes a token from storage
func (*DBTokenStoreAdapter) GetTokenByID ¶
func (*DBTokenStoreAdapter) GetTokenByToken ¶
func (*DBTokenStoreAdapter) GetTokensByProjectID ¶
func (*DBTokenStoreAdapter) IncrementTokenUsage ¶
func (a *DBTokenStoreAdapter) IncrementTokenUsage(ctx context.Context, tokenID string) error
func (*DBTokenStoreAdapter) ListTokens ¶
func (*DBTokenStoreAdapter) RevokeBatchTokens ¶
func (a *DBTokenStoreAdapter) RevokeBatchTokens(ctx context.Context, tokenIDs []string) (int, error)
RevokeBatchTokens revokes multiple tokens at once
func (*DBTokenStoreAdapter) RevokeExpiredTokens ¶
func (a *DBTokenStoreAdapter) RevokeExpiredTokens(ctx context.Context) (int, error)
RevokeExpiredTokens revokes all tokens that have expired
func (*DBTokenStoreAdapter) RevokeProjectTokens ¶
func (a *DBTokenStoreAdapter) RevokeProjectTokens(ctx context.Context, projectID string) (int, error)
RevokeProjectTokens revokes all tokens for a project
func (*DBTokenStoreAdapter) RevokeToken ¶
func (a *DBTokenStoreAdapter) RevokeToken(ctx context.Context, tokenID string) error
RevokeToken disables a token by setting is_active to false and deactivated_at to current time
func (*DBTokenStoreAdapter) UpdateToken ¶
type DriverType ¶
type DriverType string
DriverType represents the database driver type.
const ( // DriverSQLite represents the SQLite database driver. DriverSQLite DriverType = "sqlite" // DriverPostgres represents the PostgreSQL database driver. DriverPostgres DriverType = "postgres" // DriverMySQL represents the MySQL database driver. DriverMySQL DriverType = "mysql" )
type FullConfig ¶
type FullConfig struct {
// Driver specifies which database driver to use (sqlite, postgres, mysql).
Driver DriverType
// SQLite-specific configuration
// Path is the path to the SQLite database file.
Path string
// PostgreSQL and MySQL-specific configuration
// DatabaseURL is the PostgreSQL or MySQL connection string.
DatabaseURL string
// Connection pool settings (used by all drivers)
// MaxOpenConns is the maximum number of open connections.
MaxOpenConns int
// MaxIdleConns is the maximum number of idle connections.
MaxIdleConns int
// ConnMaxLifetime is the maximum amount of time a connection may be reused.
ConnMaxLifetime time.Duration
}
FullConfig contains the complete database configuration for all drivers.
func ConfigFromEnv ¶
func ConfigFromEnv() FullConfig
ConfigFromEnv creates a FullConfig from environment variables. Invalid configuration values are logged as warnings and defaults are used.
func DefaultFullConfig ¶
func DefaultFullConfig() FullConfig
DefaultFullConfig returns a default database configuration.
type MockProjectStore ¶
type MockProjectStore struct {
// contains filtered or unexported fields
}
MockProjectStore is an in-memory implementation of ProjectStore for testing and development
func NewMockProjectStore ¶
func NewMockProjectStore() *MockProjectStore
NewMockProjectStore creates a new MockProjectStore
func (*MockProjectStore) CreateMockProject ¶
func (m *MockProjectStore) CreateMockProject(projectID, name, apiKey string) (Project, error)
CreateMockProject creates a new project in the mock store with the given parameters
func (*MockProjectStore) CreateProject ¶
func (*MockProjectStore) DBCreateProject ¶
func (m *MockProjectStore) DBCreateProject(ctx context.Context, project Project) error
CreateProject creates a new project in the store
func (*MockProjectStore) DBDeleteProject ¶
func (m *MockProjectStore) DBDeleteProject(ctx context.Context, projectID string) error
DeleteProject deletes a project from the store
func (*MockProjectStore) DBGetProjectByID ¶
GetProjectByID retrieves a project by ID
func (*MockProjectStore) DBListProjects ¶
func (m *MockProjectStore) DBListProjects(ctx context.Context) ([]Project, error)
ListProjects retrieves all projects from the store
func (*MockProjectStore) DBUpdateProject ¶
func (m *MockProjectStore) DBUpdateProject(ctx context.Context, project Project) error
UpdateProject updates a project in the store
func (*MockProjectStore) DeleteProject ¶
func (m *MockProjectStore) DeleteProject(ctx context.Context, id string) error
func (*MockProjectStore) GetAPIKeyForProject ¶
func (m *MockProjectStore) GetAPIKeyForProject(ctx context.Context, projectID string) (string, error)
GetAPIKeyForProject retrieves the API key for a project
func (*MockProjectStore) GetProjectActive ¶
GetProjectActive retrieves the active status for a project by ID
func (*MockProjectStore) GetProjectByID ¶
func (*MockProjectStore) ListProjects ¶
--- proxy.ProjectStore interface adapters ---
func (*MockProjectStore) UpdateProject ¶
type MockTokenStore ¶
type MockTokenStore struct {
// contains filtered or unexported fields
}
MockTokenStore is an in-memory implementation of TokenStore for testing and development
func NewMockTokenStore ¶
func NewMockTokenStore() *MockTokenStore
NewMockTokenStore creates a new MockTokenStore
func (*MockTokenStore) CleanExpiredTokens ¶
func (m *MockTokenStore) CleanExpiredTokens(ctx context.Context) (int64, error)
CleanExpiredTokens deletes expired tokens from the store
func (*MockTokenStore) CreateMockToken ¶
func (m *MockTokenStore) CreateMockToken(tokenID, projectID string, expiresIn time.Duration, isActive bool, maxRequests *int) (Token, error)
CreateMockToken creates a new token in the mock store with the given parameters
func (*MockTokenStore) CreateToken ¶
func (m *MockTokenStore) CreateToken(ctx context.Context, token Token) error
CreateToken creates a new token in the store
func (*MockTokenStore) DeleteToken ¶
func (m *MockTokenStore) DeleteToken(ctx context.Context, tokenID string) error
DeleteToken deletes a token from the store
func (*MockTokenStore) GetTokenByID ¶
GetTokenByID retrieves a token by ID
func (*MockTokenStore) GetTokensByProjectID ¶
func (m *MockTokenStore) GetTokensByProjectID(ctx context.Context, projectID string) ([]Token, error)
GetTokensByProjectID retrieves all tokens for a project
func (*MockTokenStore) IncrementTokenUsage ¶
func (m *MockTokenStore) IncrementTokenUsage(ctx context.Context, tokenID string) error
IncrementTokenUsage increments the request count and updates the last_used_at timestamp
func (*MockTokenStore) ListTokens ¶
func (m *MockTokenStore) ListTokens(ctx context.Context) ([]Token, error)
ListTokens retrieves all tokens from the store
func (*MockTokenStore) UpdateToken ¶
func (m *MockTokenStore) UpdateToken(ctx context.Context, token Token) error
UpdateToken updates a token in the store
type Project ¶
type Project struct {
ID string `json:"id"`
Name string `json:"name"`
APIKey string `json:"-"` // Sensitive data, not included in JSON. Encrypted when ENCRYPTION_KEY is set.
IsActive bool `json:"is_active"`
DeactivatedAt *time.Time `json:"deactivated_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
Project represents a project in the database.
func ToDBProject ¶
ToDBProject converts a proxy.Project to a database.Project
func (*Project) IsDeactivated ¶
IsDeactivated returns true if the project has been explicitly deactivated.
type Token ¶
type Token struct {
ID string `json:"id"`
Token string `json:"token"`
ProjectID string `json:"project_id"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
IsActive bool `json:"is_active"`
DeactivatedAt *time.Time `json:"deactivated_at,omitempty"`
RequestCount int `json:"request_count"`
MaxRequests *int `json:"max_requests,omitempty"`
CreatedAt time.Time `json:"created_at"`
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
CacheHitCount int `json:"cache_hit_count"`
}
Token represents a token in the database.
func ImportTokenData ¶
ImportTokenData and ExportTokenData helpers
func (*Token) IsDeactivated ¶
IsDeactivated returns true if the token has been explicitly deactivated.
func (*Token) IsRateLimited ¶
IsRateLimited returns true if the token has reached its maximum number of requests.
type TokenStoreAdapter ¶
type TokenStoreAdapter struct {
// contains filtered or unexported fields
}
TokenStoreAdapter adapts the database.DB to the token.TokenStore interface
func NewTokenStoreAdapter ¶
func NewTokenStoreAdapter(store *MockTokenStore) *TokenStoreAdapter
NewTokenStoreAdapter creates a new TokenStoreAdapter
func (*TokenStoreAdapter) CreateToken ¶
CreateToken creates a new token in the store
func (*TokenStoreAdapter) GetTokenByID ¶
func (a *TokenStoreAdapter) GetTokenByID(ctx context.Context, tokenID string) (token.TokenData, error)
GetTokenByID retrieves a token by ID
func (*TokenStoreAdapter) GetTokensByProjectID ¶
func (a *TokenStoreAdapter) GetTokensByProjectID(ctx context.Context, projectID string) ([]token.TokenData, error)
GetTokensByProjectID retrieves all tokens for a project
func (*TokenStoreAdapter) IncrementTokenUsage ¶
func (a *TokenStoreAdapter) IncrementTokenUsage(ctx context.Context, tokenID string) error
IncrementTokenUsage increments the request count and updates the last_used_at timestamp
func (*TokenStoreAdapter) ListTokens ¶
ListTokens retrieves all tokens from the store
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package migrations provides database migration functionality using goose.
|
Package migrations provides database migration functionality using goose. |