this is the proposal i mentioned in the review

This commit is contained in:
adlerhurst
2025-06-13 11:59:22 +02:00
parent 54d1a7fdc6
commit abae73ef1e
3 changed files with 57 additions and 2 deletions

View File

@@ -87,7 +87,7 @@ type InstanceRepository interface {
// Member() MemberRepository // Member() MemberRepository
Get(ctx context.Context, opts ...database.Condition) (*Instance, error) Get(ctx context.Context, opts ...database.Condition) (*Instance, error)
List(ctx context.Context, opts ...database.Condition) ([]Instance, error) List(ctx context.Context, opts ...database.Condition) ([]*Instance, error)
Create(ctx context.Context, instance *Instance) error Create(ctx context.Context, instance *Instance) error
Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error) Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error)

View File

@@ -61,3 +61,11 @@ type Row interface {
type Rows interface { type Rows interface {
pgx.Rows pgx.Rows
} }
// Collector is an interface for collecting rows into a specific type.
type Collector[T any] interface {
// Collect collects a single row into the specified type.
Collect(Row) (T, error)
// CollectRows collects multiple rows into a slice of the specified type.
CollectRows(Rows) ([]T, error)
}

View File

@@ -1,11 +1,58 @@
package postgres package postgres
import ( import (
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"github.com/zitadel/zitadel/backend/v3/storage/database" "github.com/zitadel/zitadel/backend/v3/storage/database"
) )
var _ database.Rows = (*Rows)(nil) var (
_ database.Rows = (*Rows)(nil)
_ database.CollectableRows = (*Rows)(nil)
)
type Rows struct{ pgx.Rows } type Rows struct{ pgx.Rows }
// Collect implements [database.CollectableRows].
// See [this page](https://github.com/georgysavva/scany/blob/master/dbscan/doc.go#L8) for additional details.
func (r *Rows) Collect(dest any) (err error) {
defer func() {
closeErr := r.Close()
if err == nil {
err = closeErr
}
}()
return pgxscan.ScanAll(dest, r.Rows)
}
// CollectFirst implements [database.CollectableRows].
// See [this page](https://github.com/georgysavva/scany/blob/master/dbscan/doc.go#L8) for additional details.
func (r *Rows) CollectFirst(dest any) (err error) {
defer func() {
closeErr := r.Close()
if err == nil {
err = closeErr
}
}()
return pgxscan.ScanRow(dest, r.Rows)
}
// CollectExactlyOneRow implements [database.CollectableRows].
// See [this page](https://github.com/georgysavva/scany/blob/master/dbscan/doc.go#L8) for additional details.
func (r *Rows) CollectExactlyOneRow(dest any) (err error) {
defer func() {
closeErr := r.Close()
if err == nil {
err = closeErr
}
}()
return pgxscan.ScanOne(dest, r.Rows)
}
// Close implements [database.Rows].
// Subtle: this method shadows the method (Rows).Close of Rows.Rows.
func (r *Rows) Close() error {
r.Rows.Close()
return nil
}