nidus-sync/models/notification.bob.go
Eli Ribble a2e67e3d60
Add oauth token failure model and notification
This will allow me to mark when an oauth token fails and surface it to
the user so that they can re-up on their auth token.
2025-11-11 20:10:56 +00:00

683 lines
19 KiB
Go

// Code generated by BobGen psql v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models
import (
"context"
"fmt"
"io"
enums "github.com/Gleipnir-Technology/nidus-sync/enums"
"github.com/aarondl/opt/null"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/psql"
"github.com/stephenafamo/bob/dialect/psql/dialect"
"github.com/stephenafamo/bob/dialect/psql/dm"
"github.com/stephenafamo/bob/dialect/psql/sm"
"github.com/stephenafamo/bob/dialect/psql/um"
"github.com/stephenafamo/bob/expr"
"github.com/stephenafamo/bob/mods"
"github.com/stephenafamo/bob/orm"
"github.com/stephenafamo/bob/types/pgtypes"
)
// Notification is an object representing the database table.
type Notification struct {
ID int32 `db:"id,pk" `
UserID null.Val[int32] `db:"user_id" `
Message null.Val[string] `db:"message" `
Link null.Val[string] `db:"link" `
Type null.Val[enums.Notificationtype] `db:"type" `
R notificationR `db:"-" `
}
// NotificationSlice is an alias for a slice of pointers to Notification.
// This should almost always be used instead of []*Notification.
type NotificationSlice []*Notification
// Notifications contains methods to work with the notification table
var Notifications = psql.NewTablex[*Notification, NotificationSlice, *NotificationSetter]("", "notification", buildNotificationColumns("notification"))
// NotificationsQuery is a query on the notification table
type NotificationsQuery = *psql.ViewQuery[*Notification, NotificationSlice]
// notificationR is where relationships are stored.
type notificationR struct {
UserUser *User // notification.notification_user_id_fkey
}
func buildNotificationColumns(alias string) notificationColumns {
return notificationColumns{
ColumnsExpr: expr.NewColumnsExpr(
"id", "user_id", "message", "link", "type",
).WithParent("notification"),
tableAlias: alias,
ID: psql.Quote(alias, "id"),
UserID: psql.Quote(alias, "user_id"),
Message: psql.Quote(alias, "message"),
Link: psql.Quote(alias, "link"),
Type: psql.Quote(alias, "type"),
}
}
type notificationColumns struct {
expr.ColumnsExpr
tableAlias string
ID psql.Expression
UserID psql.Expression
Message psql.Expression
Link psql.Expression
Type psql.Expression
}
func (c notificationColumns) Alias() string {
return c.tableAlias
}
func (notificationColumns) AliasedAs(alias string) notificationColumns {
return buildNotificationColumns(alias)
}
// NotificationSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type NotificationSetter struct {
ID omit.Val[int32] `db:"id,pk" `
UserID omitnull.Val[int32] `db:"user_id" `
Message omitnull.Val[string] `db:"message" `
Link omitnull.Val[string] `db:"link" `
Type omitnull.Val[enums.Notificationtype] `db:"type" `
}
func (s NotificationSetter) SetColumns() []string {
vals := make([]string, 0, 5)
if s.ID.IsValue() {
vals = append(vals, "id")
}
if !s.UserID.IsUnset() {
vals = append(vals, "user_id")
}
if !s.Message.IsUnset() {
vals = append(vals, "message")
}
if !s.Link.IsUnset() {
vals = append(vals, "link")
}
if !s.Type.IsUnset() {
vals = append(vals, "type")
}
return vals
}
func (s NotificationSetter) Overwrite(t *Notification) {
if s.ID.IsValue() {
t.ID = s.ID.MustGet()
}
if !s.UserID.IsUnset() {
t.UserID = s.UserID.MustGetNull()
}
if !s.Message.IsUnset() {
t.Message = s.Message.MustGetNull()
}
if !s.Link.IsUnset() {
t.Link = s.Link.MustGetNull()
}
if !s.Type.IsUnset() {
t.Type = s.Type.MustGetNull()
}
}
func (s *NotificationSetter) Apply(q *dialect.InsertQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return Notifications.BeforeInsertHooks.RunHooks(ctx, exec, s)
})
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
vals := make([]bob.Expression, 5)
if s.ID.IsValue() {
vals[0] = psql.Arg(s.ID.MustGet())
} else {
vals[0] = psql.Raw("DEFAULT")
}
if !s.UserID.IsUnset() {
vals[1] = psql.Arg(s.UserID.MustGetNull())
} else {
vals[1] = psql.Raw("DEFAULT")
}
if !s.Message.IsUnset() {
vals[2] = psql.Arg(s.Message.MustGetNull())
} else {
vals[2] = psql.Raw("DEFAULT")
}
if !s.Link.IsUnset() {
vals[3] = psql.Arg(s.Link.MustGetNull())
} else {
vals[3] = psql.Raw("DEFAULT")
}
if !s.Type.IsUnset() {
vals[4] = psql.Arg(s.Type.MustGetNull())
} else {
vals[4] = psql.Raw("DEFAULT")
}
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
}))
}
func (s NotificationSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
return um.Set(s.Expressions()...)
}
func (s NotificationSetter) Expressions(prefix ...string) []bob.Expression {
exprs := make([]bob.Expression, 0, 5)
if s.ID.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "id")...),
psql.Arg(s.ID),
}})
}
if !s.UserID.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "user_id")...),
psql.Arg(s.UserID),
}})
}
if !s.Message.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "message")...),
psql.Arg(s.Message),
}})
}
if !s.Link.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "link")...),
psql.Arg(s.Link),
}})
}
if !s.Type.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "type")...),
psql.Arg(s.Type),
}})
}
return exprs
}
// FindNotification retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindNotification(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*Notification, error) {
if len(cols) == 0 {
return Notifications.Query(
sm.Where(Notifications.Columns.ID.EQ(psql.Arg(IDPK))),
).One(ctx, exec)
}
return Notifications.Query(
sm.Where(Notifications.Columns.ID.EQ(psql.Arg(IDPK))),
sm.Columns(Notifications.Columns.Only(cols...)),
).One(ctx, exec)
}
// NotificationExists checks the presence of a single record by primary key
func NotificationExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
return Notifications.Query(
sm.Where(Notifications.Columns.ID.EQ(psql.Arg(IDPK))),
).Exists(ctx, exec)
}
// AfterQueryHook is called after Notification is retrieved from the database
func (o *Notification) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = Notifications.AfterSelectHooks.RunHooks(ctx, exec, NotificationSlice{o})
case bob.QueryTypeInsert:
ctx, err = Notifications.AfterInsertHooks.RunHooks(ctx, exec, NotificationSlice{o})
case bob.QueryTypeUpdate:
ctx, err = Notifications.AfterUpdateHooks.RunHooks(ctx, exec, NotificationSlice{o})
case bob.QueryTypeDelete:
ctx, err = Notifications.AfterDeleteHooks.RunHooks(ctx, exec, NotificationSlice{o})
}
return err
}
// primaryKeyVals returns the primary key values of the Notification
func (o *Notification) primaryKeyVals() bob.Expression {
return psql.Arg(o.ID)
}
func (o *Notification) pkEQ() dialect.Expression {
return psql.Quote("notification", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
}))
}
// Update uses an executor to update the Notification
func (o *Notification) Update(ctx context.Context, exec bob.Executor, s *NotificationSetter) error {
v, err := Notifications.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
if err != nil {
return err
}
o.R = v.R
*o = *v
return nil
}
// Delete deletes a single Notification record with an executor
func (o *Notification) Delete(ctx context.Context, exec bob.Executor) error {
_, err := Notifications.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
return err
}
// Reload refreshes the Notification using the executor
func (o *Notification) Reload(ctx context.Context, exec bob.Executor) error {
o2, err := Notifications.Query(
sm.Where(Notifications.Columns.ID.EQ(psql.Arg(o.ID))),
).One(ctx, exec)
if err != nil {
return err
}
o2.R = o.R
*o = *o2
return nil
}
// AfterQueryHook is called after NotificationSlice is retrieved from the database
func (o NotificationSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
var err error
switch queryType {
case bob.QueryTypeSelect:
ctx, err = Notifications.AfterSelectHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeInsert:
ctx, err = Notifications.AfterInsertHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeUpdate:
ctx, err = Notifications.AfterUpdateHooks.RunHooks(ctx, exec, o)
case bob.QueryTypeDelete:
ctx, err = Notifications.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}
func (o NotificationSlice) pkIN() dialect.Expression {
if len(o) == 0 {
return psql.Raw("NULL")
}
return psql.Quote("notification", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
pkPairs := make([]bob.Expression, len(o))
for i, row := range o {
pkPairs[i] = row.primaryKeyVals()
}
return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
}))
}
// copyMatchingRows finds models in the given slice that have the same primary key
// then it first copies the existing relationships from the old model to the new model
// and then replaces the old model in the slice with the new model
func (o NotificationSlice) copyMatchingRows(from ...*Notification) {
for i, old := range o {
for _, new := range from {
if new.ID != old.ID {
continue
}
new.R = old.R
o[i] = new
break
}
}
}
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o NotificationSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return Notifications.BeforeUpdateHooks.RunHooks(ctx, exec, o)
})
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
var err error
switch retrieved := retrieved.(type) {
case *Notification:
o.copyMatchingRows(retrieved)
case []*Notification:
o.copyMatchingRows(retrieved...)
case NotificationSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a Notification or a slice of Notification
// then run the AfterUpdateHooks on the slice
_, err = Notifications.AfterUpdateHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o NotificationSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return Notifications.BeforeDeleteHooks.RunHooks(ctx, exec, o)
})
q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
var err error
switch retrieved := retrieved.(type) {
case *Notification:
o.copyMatchingRows(retrieved)
case []*Notification:
o.copyMatchingRows(retrieved...)
case NotificationSlice:
o.copyMatchingRows(retrieved...)
default:
// If the retrieved value is not a Notification or a slice of Notification
// then run the AfterDeleteHooks on the slice
_, err = Notifications.AfterDeleteHooks.RunHooks(ctx, exec, o)
}
return err
}))
q.AppendWhere(o.pkIN())
})
}
func (o NotificationSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals NotificationSetter) error {
if len(o) == 0 {
return nil
}
_, err := Notifications.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
return err
}
func (o NotificationSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
_, err := Notifications.Delete(o.DeleteMod()).Exec(ctx, exec)
return err
}
func (o NotificationSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
if len(o) == 0 {
return nil
}
o2, err := Notifications.Query(sm.Where(o.pkIN())).All(ctx, exec)
if err != nil {
return err
}
o.copyMatchingRows(o2...)
return nil
}
// UserUser starts a query for related objects on user_
func (o *Notification) UserUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
return Users.Query(append(mods,
sm.Where(Users.Columns.ID.EQ(psql.Arg(o.UserID))),
)...)
}
func (os NotificationSlice) UserUser(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
pkUserID := make(pgtypes.Array[null.Val[int32]], 0, len(os))
for _, o := range os {
if o == nil {
continue
}
pkUserID = append(pkUserID, o.UserID)
}
PKArgExpr := psql.Select(sm.Columns(
psql.F("unnest", psql.Cast(psql.Arg(pkUserID), "integer[]")),
))
return Users.Query(append(mods,
sm.Where(psql.Group(Users.Columns.ID).OP("IN", PKArgExpr)),
)...)
}
func attachNotificationUserUser0(ctx context.Context, exec bob.Executor, count int, notification0 *Notification, user1 *User) (*Notification, error) {
setter := &NotificationSetter{
UserID: omitnull.From(user1.ID),
}
err := notification0.Update(ctx, exec, setter)
if err != nil {
return nil, fmt.Errorf("attachNotificationUserUser0: %w", err)
}
return notification0, nil
}
func (notification0 *Notification) InsertUserUser(ctx context.Context, exec bob.Executor, related *UserSetter) error {
var err error
user1, err := Users.Insert(related).One(ctx, exec)
if err != nil {
return fmt.Errorf("inserting related objects: %w", err)
}
_, err = attachNotificationUserUser0(ctx, exec, 1, notification0, user1)
if err != nil {
return err
}
notification0.R.UserUser = user1
user1.R.UserNotifications = append(user1.R.UserNotifications, notification0)
return nil
}
func (notification0 *Notification) AttachUserUser(ctx context.Context, exec bob.Executor, user1 *User) error {
var err error
_, err = attachNotificationUserUser0(ctx, exec, 1, notification0, user1)
if err != nil {
return err
}
notification0.R.UserUser = user1
user1.R.UserNotifications = append(user1.R.UserNotifications, notification0)
return nil
}
type notificationWhere[Q psql.Filterable] struct {
ID psql.WhereMod[Q, int32]
UserID psql.WhereNullMod[Q, int32]
Message psql.WhereNullMod[Q, string]
Link psql.WhereNullMod[Q, string]
Type psql.WhereNullMod[Q, enums.Notificationtype]
}
func (notificationWhere[Q]) AliasedAs(alias string) notificationWhere[Q] {
return buildNotificationWhere[Q](buildNotificationColumns(alias))
}
func buildNotificationWhere[Q psql.Filterable](cols notificationColumns) notificationWhere[Q] {
return notificationWhere[Q]{
ID: psql.Where[Q, int32](cols.ID),
UserID: psql.WhereNull[Q, int32](cols.UserID),
Message: psql.WhereNull[Q, string](cols.Message),
Link: psql.WhereNull[Q, string](cols.Link),
Type: psql.WhereNull[Q, enums.Notificationtype](cols.Type),
}
}
func (o *Notification) Preload(name string, retrieved any) error {
if o == nil {
return nil
}
switch name {
case "UserUser":
rel, ok := retrieved.(*User)
if !ok {
return fmt.Errorf("notification cannot load %T as %q", retrieved, name)
}
o.R.UserUser = rel
if rel != nil {
rel.R.UserNotifications = NotificationSlice{o}
}
return nil
default:
return fmt.Errorf("notification has no relationship %q", name)
}
}
type notificationPreloader struct {
UserUser func(...psql.PreloadOption) psql.Preloader
}
func buildNotificationPreloader() notificationPreloader {
return notificationPreloader{
UserUser: func(opts ...psql.PreloadOption) psql.Preloader {
return psql.Preload[*User, UserSlice](psql.PreloadRel{
Name: "UserUser",
Sides: []psql.PreloadSide{
{
From: Notifications,
To: Users,
FromColumns: []string{"user_id"},
ToColumns: []string{"id"},
},
},
}, Users.Columns.Names(), opts...)
},
}
}
type notificationThenLoader[Q orm.Loadable] struct {
UserUser func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}
func buildNotificationThenLoader[Q orm.Loadable]() notificationThenLoader[Q] {
type UserUserLoadInterface interface {
LoadUserUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
}
return notificationThenLoader[Q]{
UserUser: thenLoadBuilder[Q](
"UserUser",
func(ctx context.Context, exec bob.Executor, retrieved UserUserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
return retrieved.LoadUserUser(ctx, exec, mods...)
},
),
}
}
// LoadUserUser loads the notification's UserUser into the .R struct
func (o *Notification) LoadUserUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if o == nil {
return nil
}
// Reset the relationship
o.R.UserUser = nil
related, err := o.UserUser(mods...).One(ctx, exec)
if err != nil {
return err
}
related.R.UserNotifications = NotificationSlice{o}
o.R.UserUser = related
return nil
}
// LoadUserUser loads the notification's UserUser into the .R struct
func (os NotificationSlice) LoadUserUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
if len(os) == 0 {
return nil
}
users, err := os.UserUser(mods...).All(ctx, exec)
if err != nil {
return err
}
for _, o := range os {
if o == nil {
continue
}
for _, rel := range users {
if !o.UserID.IsValue() {
continue
}
if !(o.UserID.IsValue() && o.UserID.MustGet() == rel.ID) {
continue
}
rel.R.UserNotifications = append(rel.R.UserNotifications, o)
o.R.UserUser = rel
break
}
}
return nil
}
type notificationJoins[Q dialect.Joinable] struct {
typ string
UserUser modAs[Q, userColumns]
}
func (j notificationJoins[Q]) aliasedAs(alias string) notificationJoins[Q] {
return buildNotificationJoins[Q](buildNotificationColumns(alias), j.typ)
}
func buildNotificationJoins[Q dialect.Joinable](cols notificationColumns, typ string) notificationJoins[Q] {
return notificationJoins[Q]{
typ: typ,
UserUser: modAs[Q, userColumns]{
c: Users.Columns,
f: func(to userColumns) bob.Mod[Q] {
mods := make(mods.QueryMods[Q], 0, 1)
{
mods = append(mods, dialect.Join[Q](typ, Users.Name().As(to.Alias())).On(
to.ID.EQ(cols.UserID),
))
}
return mods
},
},
}
}