Refactor database interface to use *sql.DB directly
Simplified the database layer by removing custom interface wrappers and using standard library *sql.DB and *sql.Tx types directly. Changes: - Removed DBConnection and DBTransaction interfaces from database.go - Removed NewDBConnection() wrapper function - Updated TokenGenerator to use *sql.DB instead of DBConnection - Updated all validation and revocation methods to accept *sql.Tx - Updated TableManager to work with *sql.DB directly - Updated all tests to use db.Begin() instead of custom wrappers - Fixed GeneratorConfig.DB field (was DBConn) - Updated documentation in doc.go with correct API usage Benefits: - Simpler API with fewer abstractions - Works directly with database/sql standard library - Compatible with GORM (via gormDB.DB()) and Bun (share same *sql.DB) - Easier to understand and maintain - No unnecessary wrapper layers Breaking changes: - GeneratorConfig.DBConn renamed to GeneratorConfig.DB - Removed NewDBConnection() function - pass *sql.DB directly - ValidateAccess/ValidateRefresh now accept *sql.Tx instead of DBTransaction - Token.Revoke/CheckNotRevoked now accept *sql.Tx instead of DBTransaction 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
48
jwt/database.go
Normal file
48
jwt/database.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package jwt
|
||||
|
||||
// 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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user