One-to-many support
- Dominant language
- Go
- Stars
- 14
- Forks
- 3
- PR merge metrics
- No merged PRs in 30d
Description
This new mapper looks great actually, even if it's brand new. However it would be nice if it could support this kind of querying.
```golang
package main
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/jackc/pgconn"
"github.com/jackc/pgsql"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgxrecord"
)
type PermissionOverride struct {
Name pgtype.Varchar
Revoke pgtype.Bool
}
type User struct {
ID int
Username string
Overrides []PermissionOverride
}
func (user *User) String() string {
s := fmt.Sprintf("ID:%d Username:%s Overrides:[", user.ID, user.Username)
for _, over := range user.Overrides {
s = s + fmt.Sprintf("(%s, %t),", over.Name.Get(), over.Revoke.Get())
}
s = s + "]"
return s
}
func (user *User) SelectStatementOptions() []pgsql.StatementOption {
return []pgsql.StatementOption{
pgsql.Select("users.id, users.username, permission_overrides.name, permission_overrides.revoke"),
pgsql.From("users LEFT JOIN permission_overrides ON users.id = permission_overrides.user_id"),
}
}
func (user *User) SelectScan(rows pgx.Rows) error {
var over PermissionOverride
err := rows.Scan(&user.ID, &user.Username, &over.Name, &over.Revoke)
user.Overrides = append(user.Overrides, over)
for rows.Next() {
var over PermissionOverride
err = rows.Scan(&user.ID, &user.Username, &over.Name, &over.Revoke)
user.Overrides = append(user.Overrides, over)
}
return err
}
func main() {
user := &User{}
withTx(func(ctx context.Context, tx pgx.Tx) {
err := pgxrecord.SelectOne(ctx, tx, user, pgsql.Where("users.id=?", 285592))
fmt.Println(err)
fmt.Println(user.String())
})
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reading the pgxrecord.SelectOne entry point and the SelectScan method shown in the issue, along with how pgsql.Select and pgx.Rows are handled. Done should mean a one-to-many query like the provided User and PermissionOverride example can map joined rows into the user's Overrides slice.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, postgresql
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100