mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-13 10:07:34 +00:00
this is the proposal i mentioned in the review
This commit is contained in:
@@ -87,7 +87,7 @@ type InstanceRepository interface {
|
||||
// Member() MemberRepository
|
||||
|
||||
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
|
||||
Update(ctx context.Context, condition database.Condition, changes ...database.Change) (int64, error)
|
||||
|
@@ -61,3 +61,11 @@ type Row interface {
|
||||
type Rows interface {
|
||||
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)
|
||||
}
|
||||
|
@@ -1,11 +1,58 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"github.com/georgysavva/scany/v2/pgxscan"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"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 }
|
||||
|
||||
// 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user