From 9c3f1ea5ffe9ad70caf306faff7a3e3948cef6ab Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Mon, 9 Mar 2026 18:47:40 +0100 Subject: feat(config): preload role react --- common/db.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'common') diff --git a/common/db.go b/common/db.go index 76e39a2..992ca37 100644 --- a/common/db.go +++ b/common/db.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "embed" + "errors" "fmt" "log/slog" "path" @@ -55,3 +56,32 @@ func Migrate(ctx context.Context, log *slog.Logger, db *sql.DB, migrations embed } return nil } + +type Loadable interface { + Load(context.Context, *sql.DB, *sql.Rows) error +} + +func GetValues[T Loadable](ctx context.Context, db *sql.DB, table, data, where string, args ...any) ([]T, error) { + arr := make([]T, 0) + rows, err := db.QueryContext( + ctx, + fmt.Sprintf(`SELECT %s FROM %s WHERE %s`, data, table, where), + args..., + ) + if err != nil { + if !errors.Is(err, sql.ErrNoRows) { + return arr, err + } + } else { + defer rows.Close() + for rows.Next() { + var v T + err = v.Load(ctx, db, rows) + if err != nil { + return nil, err + } + arr = append(arr, v) + } + } + return arr, nil +} -- cgit v1.2.3