Bun Adaptor updates

This commit is contained in:
Hein 2025-11-07 14:03:40 +02:00
parent abee5c942f
commit f1c6b36374

View File

@ -22,7 +22,10 @@ func NewBunAdapter(db *bun.DB) *BunAdapter {
} }
func (b *BunAdapter) NewSelect() common.SelectQuery { func (b *BunAdapter) NewSelect() common.SelectQuery {
return &BunSelectQuery{query: b.db.NewSelect()} return &BunSelectQuery{
query: b.db.NewSelect(),
db: b.db,
}
} }
func (b *BunAdapter) NewInsert() common.InsertQuery { func (b *BunAdapter) NewInsert() common.InsertQuery {
@ -78,8 +81,9 @@ func (b *BunAdapter) RunInTransaction(ctx context.Context, fn func(common.Databa
// BunSelectQuery implements SelectQuery for Bun // BunSelectQuery implements SelectQuery for Bun
type BunSelectQuery struct { type BunSelectQuery struct {
query *bun.SelectQuery query *bun.SelectQuery
schema string // Separated schema name db bun.IDB // Store DB connection for count queries
tableName string // Just the table name, without schema schema string // Separated schema name
tableName string // Just the table name, without schema
tableAlias string tableAlias string
} }
@ -228,10 +232,10 @@ func (b *BunSelectQuery) Scan(ctx context.Context, dest interface{}) error {
} }
func (b *BunSelectQuery) Count(ctx context.Context) (int, error) { func (b *BunSelectQuery) Count(ctx context.Context) (int, error) {
// Use ColumnExpr with Scan instead of Count() to avoid requiring a model // Bun's Count() method works properly and handles column selections automatically
// This works with just Table() set and avoids "Model(nil)" error // It builds: SELECT COUNT(*) FROM (original query) AS subquery
var count int // This works with both Model() and Table() set
err := b.query.ColumnExpr("count(*)").Scan(ctx, &count) count, err := b.query.Count(ctx)
return count, err return count, err
} }
@ -381,7 +385,10 @@ type BunTxAdapter struct {
} }
func (b *BunTxAdapter) NewSelect() common.SelectQuery { func (b *BunTxAdapter) NewSelect() common.SelectQuery {
return &BunSelectQuery{query: b.tx.NewSelect()} return &BunSelectQuery{
query: b.tx.NewSelect(),
db: b.tx,
}
} }
func (b *BunTxAdapter) NewInsert() common.InsertQuery { func (b *BunTxAdapter) NewInsert() common.InsertQuery {