refactor to improve database operability in hwsauth

This commit is contained in:
2026-01-11 23:00:50 +11:00
parent ae4094d426
commit 4c5af63ea2
17 changed files with 582 additions and 96 deletions

View File

@@ -1,27 +1,22 @@
package hwsauth
import (
"context"
"database/sql"
"git.haelnorr.com/h/golib/jwt"
)
// 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
}
// This is an alias to jwt.DBTransaction.
//
// Standard library *sql.Tx implements this interface automatically.
// ORM transactions (GORM, Bun, etc.) should also implement this interface.
type DBTransaction = jwt.DBTransaction
// 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)
// BeginTX is a function type for creating database transactions.
// This is an alias to jwt.BeginTX.
//
// Example:
//
// beginTx := func(ctx context.Context) (hwsauth.DBTransaction, error) {
// return db.BeginTx(ctx, nil)
// }
type BeginTX = jwt.BeginTX