Files
golib/jwt/database.go

67 lines
2.1 KiB
Go

package jwt
import (
"context"
"database/sql"
)
// DBTransaction represents a database transaction that can execute queries.
// This interface is compatible with *sql.Tx and can be implemented by ORM transactions
// from libraries like GORM (gormDB.Begin()), Bun (bunDB.Begin()), etc.
type DBTransaction interface {
Exec(query string, args ...any) (sql.Result, error)
Query(query string, args ...any) (*sql.Rows, error)
Commit() error
Rollback() error
}
// BeginTX represents a wrapper function that is used to start a transaction with any dependencies injected
type BeginTX func(ctx context.Context) (DBTransaction, error)
// DatabaseType specifies the database system and version being used.
type DatabaseType struct {
Type string // Database type: "postgres", "mysql", "sqlite", "mariadb"
Version string // Version string, e.g., "15.3", "8.0.32", "3.42.0"
}
// Predefined database type constants for easy configuration and validation.
const (
DatabasePostgreSQL = "postgres"
DatabaseMySQL = "mysql"
DatabaseSQLite = "sqlite"
DatabaseMariaDB = "mariadb"
)
// TableConfig configures the JWT blacklist table.
type TableConfig struct {
// TableName is the name of the blacklist table.
// Default: "jwtblacklist"
TableName string
// AutoCreate determines whether to automatically create the table if it doesn't exist.
// Default: true
AutoCreate bool
// EnableAutoCleanup configures database-native automatic cleanup of expired tokens.
// For PostgreSQL: Creates a cleanup function (requires external scheduler or pg_cron)
// For MySQL/MariaDB: Creates a database event
// For SQLite: No automatic cleanup (manual only)
// Default: true
EnableAutoCleanup bool
// CleanupInterval specifies how often automatic cleanup should run (in hours).
// Only used if EnableAutoCleanup is true.
// Default: 24 (daily cleanup)
CleanupInterval int
}
// DefaultTableConfig returns a TableConfig with sensible defaults.
func DefaultTableConfig() TableConfig {
return TableConfig{
TableName: "jwtblacklist",
AutoCreate: true,
EnableAutoCleanup: true,
CleanupInterval: 24,
}
}