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:
27
hwsauth/db.go
Normal file
27
hwsauth/db.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package hwsauth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// DBTransaction represents a database transaction that can be committed or rolled back.
|
||||
// This interface can be implemented by standard library sql.Tx, or by ORM transactions
|
||||
// from libraries like bun, gorm, sqlx, etc.
|
||||
type DBTransaction interface {
|
||||
Commit() error
|
||||
Rollback() error
|
||||
}
|
||||
|
||||
// DBConnection represents a database connection that can begin transactions.
|
||||
// This interface can be implemented by standard library sql.DB, or by ORM connections
|
||||
// from libraries like bun, gorm, sqlx, etc.
|
||||
type DBConnection interface {
|
||||
BeginTx(ctx context.Context, opts *sql.TxOptions) (DBTransaction, error)
|
||||
}
|
||||
|
||||
// Ensure *sql.Tx implements DBTransaction
|
||||
var _ DBTransaction = (*sql.Tx)(nil)
|
||||
|
||||
// Ensure *sql.DB implements DBConnection
|
||||
var _ DBConnection = (*sql.DB)(nil)
|
||||
Reference in New Issue
Block a user