Initial working version of using jet for SQL building
This commit is contained in:
parent
4bd62b3567
commit
e5a84e09a8
11 changed files with 40 additions and 619 deletions
|
|
@ -25,7 +25,6 @@ psql:
|
|||
- "lob"
|
||||
- "public"
|
||||
- "publicreport"
|
||||
- "stadia"
|
||||
- "tile"
|
||||
shared_schema: "public"
|
||||
queries:
|
||||
|
|
|
|||
|
|
@ -12,9 +12,10 @@ import (
|
|||
//"github.com/georgysavva/scany/v2/pgxscan"
|
||||
//"github.com/jackc/pgx/v5"
|
||||
"github.com/Gleipnir-Technology/bob"
|
||||
"github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/pressly/goose/v3"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
|
@ -22,16 +23,24 @@ import (
|
|||
//go:embed migrations/*.sql
|
||||
var embedMigrations embed.FS
|
||||
|
||||
type postgres struct {
|
||||
type pginstance struct {
|
||||
BobDB bob.DB
|
||||
PGXPool *pgxpool.Pool
|
||||
}
|
||||
|
||||
var (
|
||||
PGInstance *postgres
|
||||
PGInstance *pginstance
|
||||
pgOnce sync.Once
|
||||
)
|
||||
|
||||
func Execute[T any](ctx context.Context, stmt postgres.Statement) (*T, error) {
|
||||
query, args := stmt.Sql()
|
||||
|
||||
row, _ := PGInstance.PGXPool.Query(ctx, query, args...)
|
||||
data, err := pgx.CollectOneRow(row, pgx.RowToAddrOfStructByPos[T])
|
||||
|
||||
return data, err
|
||||
}
|
||||
func doMigrations(connection_string string) error {
|
||||
log.Debug().Str("dsn", connection_string).Msg("Connecting to database")
|
||||
db, err := sql.Open("pgx", connection_string)
|
||||
|
|
@ -98,7 +107,7 @@ func InitializeDatabase(ctx context.Context, uri string) error {
|
|||
pgOnce.Do(func() {
|
||||
db, e := pgxpool.New(ctx, uri)
|
||||
bobDB := bob.NewDB(stdlib.OpenDBFromPool(db))
|
||||
PGInstance = &postgres{bobDB, db}
|
||||
PGInstance = &pginstance{bobDB, db}
|
||||
err = e
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -1,26 +0,0 @@
|
|||
// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package dberrors
|
||||
|
||||
var StadiaAPIRequestErrors = &stadiaAPIRequestErrors{
|
||||
ErrUniqueApiRequestPkey: &UniqueConstraintError{
|
||||
schema: "stadia",
|
||||
table: "api_request",
|
||||
columns: []string{"id"},
|
||||
s: "api_request_pkey",
|
||||
},
|
||||
|
||||
ErrUniqueApiRequestRequestKey: &UniqueConstraintError{
|
||||
schema: "stadia",
|
||||
table: "api_request",
|
||||
columns: []string{"request"},
|
||||
s: "api_request_request_key",
|
||||
},
|
||||
}
|
||||
|
||||
type stadiaAPIRequestErrors struct {
|
||||
ErrUniqueApiRequestPkey *UniqueConstraintError
|
||||
|
||||
ErrUniqueApiRequestRequestKey *UniqueConstraintError
|
||||
}
|
||||
|
|
@ -1,152 +0,0 @@
|
|||
// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package dbinfo
|
||||
|
||||
import "github.com/aarondl/opt/null"
|
||||
|
||||
var StadiaAPIRequests = Table[
|
||||
stadiaAPIRequestColumns,
|
||||
stadiaAPIRequestIndexes,
|
||||
stadiaAPIRequestForeignKeys,
|
||||
stadiaAPIRequestUniques,
|
||||
stadiaAPIRequestChecks,
|
||||
]{
|
||||
Schema: "stadia",
|
||||
Name: "api_request",
|
||||
Columns: stadiaAPIRequestColumns{
|
||||
ID: column{
|
||||
Name: "id",
|
||||
DBType: "bigint",
|
||||
Default: "nextval('stadia.api_request_id_seq'::regclass)",
|
||||
Comment: "",
|
||||
Nullable: false,
|
||||
Generated: false,
|
||||
AutoIncr: false,
|
||||
},
|
||||
Request: column{
|
||||
Name: "request",
|
||||
DBType: "text",
|
||||
Default: "",
|
||||
Comment: "",
|
||||
Nullable: false,
|
||||
Generated: false,
|
||||
AutoIncr: false,
|
||||
},
|
||||
Response: column{
|
||||
Name: "response",
|
||||
DBType: "jsonb",
|
||||
Default: "",
|
||||
Comment: "",
|
||||
Nullable: false,
|
||||
Generated: false,
|
||||
AutoIncr: false,
|
||||
},
|
||||
CreatedAt: column{
|
||||
Name: "created_at",
|
||||
DBType: "timestamp without time zone",
|
||||
Default: "",
|
||||
Comment: "",
|
||||
Nullable: false,
|
||||
Generated: false,
|
||||
AutoIncr: false,
|
||||
},
|
||||
},
|
||||
Indexes: stadiaAPIRequestIndexes{
|
||||
APIRequestPkey: index{
|
||||
Type: "btree",
|
||||
Name: "api_request_pkey",
|
||||
Columns: []indexColumn{
|
||||
{
|
||||
Name: "id",
|
||||
Desc: null.FromCond(false, true),
|
||||
IsExpression: false,
|
||||
},
|
||||
},
|
||||
Unique: true,
|
||||
Comment: "",
|
||||
NullsFirst: []bool{false},
|
||||
NullsDistinct: false,
|
||||
Where: "",
|
||||
Include: []string{},
|
||||
},
|
||||
APIRequestRequestKey: index{
|
||||
Type: "btree",
|
||||
Name: "api_request_request_key",
|
||||
Columns: []indexColumn{
|
||||
{
|
||||
Name: "request",
|
||||
Desc: null.FromCond(false, true),
|
||||
IsExpression: false,
|
||||
},
|
||||
},
|
||||
Unique: true,
|
||||
Comment: "",
|
||||
NullsFirst: []bool{false},
|
||||
NullsDistinct: false,
|
||||
Where: "",
|
||||
Include: []string{},
|
||||
},
|
||||
},
|
||||
PrimaryKey: &constraint{
|
||||
Name: "api_request_pkey",
|
||||
Columns: []string{"id"},
|
||||
Comment: "",
|
||||
},
|
||||
|
||||
Uniques: stadiaAPIRequestUniques{
|
||||
APIRequestRequestKey: constraint{
|
||||
Name: "api_request_request_key",
|
||||
Columns: []string{"request"},
|
||||
Comment: "",
|
||||
},
|
||||
},
|
||||
|
||||
Comment: "",
|
||||
}
|
||||
|
||||
type stadiaAPIRequestColumns struct {
|
||||
ID column
|
||||
Request column
|
||||
Response column
|
||||
CreatedAt column
|
||||
}
|
||||
|
||||
func (c stadiaAPIRequestColumns) AsSlice() []column {
|
||||
return []column{
|
||||
c.ID, c.Request, c.Response, c.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
type stadiaAPIRequestIndexes struct {
|
||||
APIRequestPkey index
|
||||
APIRequestRequestKey index
|
||||
}
|
||||
|
||||
func (i stadiaAPIRequestIndexes) AsSlice() []index {
|
||||
return []index{
|
||||
i.APIRequestPkey, i.APIRequestRequestKey,
|
||||
}
|
||||
}
|
||||
|
||||
type stadiaAPIRequestForeignKeys struct{}
|
||||
|
||||
func (f stadiaAPIRequestForeignKeys) AsSlice() []foreignKey {
|
||||
return []foreignKey{}
|
||||
}
|
||||
|
||||
type stadiaAPIRequestUniques struct {
|
||||
APIRequestRequestKey constraint
|
||||
}
|
||||
|
||||
func (u stadiaAPIRequestUniques) AsSlice() []constraint {
|
||||
return []constraint{
|
||||
u.APIRequestRequestKey,
|
||||
}
|
||||
}
|
||||
|
||||
type stadiaAPIRequestChecks struct{}
|
||||
|
||||
func (c stadiaAPIRequestChecks) AsSlice() []check {
|
||||
return []check{}
|
||||
}
|
||||
3
db/jetgen.sh
Executable file
3
db/jetgen.sh
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
#! /usr/bin/env nix-shell
|
||||
#! nix-shell -i bash -p bash
|
||||
jet -dsn="postgresql://?host=/var/run/postgresql&sslmode=disable&dbname=nidus-sync" -schema=stadia -path=./gen
|
||||
|
|
@ -123,7 +123,6 @@ func Where[Q psql.Filterable]() struct {
|
|||
Signals signalWhere[Q]
|
||||
Sites siteWhere[Q]
|
||||
SpatialRefSys spatialRefSyWhere[Q]
|
||||
StadiaAPIRequests stadiaAPIRequestWhere[Q]
|
||||
TileCachedImages tileCachedImageWhere[Q]
|
||||
TileServices tileServiceWhere[Q]
|
||||
Users userWhere[Q]
|
||||
|
|
@ -235,7 +234,6 @@ func Where[Q psql.Filterable]() struct {
|
|||
Signals signalWhere[Q]
|
||||
Sites siteWhere[Q]
|
||||
SpatialRefSys spatialRefSyWhere[Q]
|
||||
StadiaAPIRequests stadiaAPIRequestWhere[Q]
|
||||
TileCachedImages tileCachedImageWhere[Q]
|
||||
TileServices tileServiceWhere[Q]
|
||||
Users userWhere[Q]
|
||||
|
|
@ -346,7 +344,6 @@ func Where[Q psql.Filterable]() struct {
|
|||
Signals: buildSignalWhere[Q](Signals.Columns),
|
||||
Sites: buildSiteWhere[Q](Sites.Columns),
|
||||
SpatialRefSys: buildSpatialRefSyWhere[Q](SpatialRefSys.Columns),
|
||||
StadiaAPIRequests: buildStadiaAPIRequestWhere[Q](StadiaAPIRequests.Columns),
|
||||
TileCachedImages: buildTileCachedImageWhere[Q](TileCachedImages.Columns),
|
||||
TileServices: buildTileServiceWhere[Q](TileServices.Columns),
|
||||
Users: buildUserWhere[Q](Users.Columns),
|
||||
|
|
|
|||
|
|
@ -1,426 +0,0 @@
|
|||
// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/Gleipnir-Technology/bob"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql/dm"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql/um"
|
||||
"github.com/Gleipnir-Technology/bob/expr"
|
||||
"github.com/Gleipnir-Technology/bob/types"
|
||||
"github.com/aarondl/opt/omit"
|
||||
)
|
||||
|
||||
// StadiaAPIRequest is an object representing the database table.
|
||||
type StadiaAPIRequest struct {
|
||||
ID int64 `db:"id,pk" `
|
||||
Request string `db:"request" `
|
||||
Response types.JSON[json.RawMessage] `db:"response" `
|
||||
CreatedAt time.Time `db:"created_at" `
|
||||
}
|
||||
|
||||
// StadiaAPIRequestSlice is an alias for a slice of pointers to StadiaAPIRequest.
|
||||
// This should almost always be used instead of []*StadiaAPIRequest.
|
||||
type StadiaAPIRequestSlice []*StadiaAPIRequest
|
||||
|
||||
// StadiaAPIRequests contains methods to work with the api_request table
|
||||
var StadiaAPIRequests = psql.NewTablex[*StadiaAPIRequest, StadiaAPIRequestSlice, *StadiaAPIRequestSetter]("stadia", "api_request", buildStadiaAPIRequestColumns("stadia.api_request"))
|
||||
|
||||
// StadiaAPIRequestsQuery is a query on the api_request table
|
||||
type StadiaAPIRequestsQuery = *psql.ViewQuery[*StadiaAPIRequest, StadiaAPIRequestSlice]
|
||||
|
||||
func buildStadiaAPIRequestColumns(alias string) stadiaAPIRequestColumns {
|
||||
return stadiaAPIRequestColumns{
|
||||
ColumnsExpr: expr.NewColumnsExpr(
|
||||
"id", "request", "response", "created_at",
|
||||
).WithParent("stadia.api_request"),
|
||||
tableAlias: alias,
|
||||
ID: psql.Quote(alias, "id"),
|
||||
Request: psql.Quote(alias, "request"),
|
||||
Response: psql.Quote(alias, "response"),
|
||||
CreatedAt: psql.Quote(alias, "created_at"),
|
||||
}
|
||||
}
|
||||
|
||||
type stadiaAPIRequestColumns struct {
|
||||
expr.ColumnsExpr
|
||||
tableAlias string
|
||||
ID psql.Expression
|
||||
Request psql.Expression
|
||||
Response psql.Expression
|
||||
CreatedAt psql.Expression
|
||||
}
|
||||
|
||||
func (c stadiaAPIRequestColumns) Alias() string {
|
||||
return c.tableAlias
|
||||
}
|
||||
|
||||
func (stadiaAPIRequestColumns) AliasedAs(alias string) stadiaAPIRequestColumns {
|
||||
return buildStadiaAPIRequestColumns(alias)
|
||||
}
|
||||
|
||||
// StadiaAPIRequestSetter is used for insert/upsert/update operations
|
||||
// All values are optional, and do not have to be set
|
||||
// Generated columns are not included
|
||||
type StadiaAPIRequestSetter struct {
|
||||
ID omit.Val[int64] `db:"id,pk" `
|
||||
Request omit.Val[string] `db:"request" `
|
||||
Response omit.Val[types.JSON[json.RawMessage]] `db:"response" `
|
||||
CreatedAt omit.Val[time.Time] `db:"created_at" `
|
||||
}
|
||||
|
||||
func (s StadiaAPIRequestSetter) SetColumns() []string {
|
||||
vals := make([]string, 0, 4)
|
||||
if s.ID.IsValue() {
|
||||
vals = append(vals, "id")
|
||||
}
|
||||
if s.Request.IsValue() {
|
||||
vals = append(vals, "request")
|
||||
}
|
||||
if s.Response.IsValue() {
|
||||
vals = append(vals, "response")
|
||||
}
|
||||
if s.CreatedAt.IsValue() {
|
||||
vals = append(vals, "created_at")
|
||||
}
|
||||
return vals
|
||||
}
|
||||
|
||||
func (s StadiaAPIRequestSetter) Overwrite(t *StadiaAPIRequest) {
|
||||
if s.ID.IsValue() {
|
||||
t.ID = s.ID.MustGet()
|
||||
}
|
||||
if s.Request.IsValue() {
|
||||
t.Request = s.Request.MustGet()
|
||||
}
|
||||
if s.Response.IsValue() {
|
||||
t.Response = s.Response.MustGet()
|
||||
}
|
||||
if s.CreatedAt.IsValue() {
|
||||
t.CreatedAt = s.CreatedAt.MustGet()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StadiaAPIRequestSetter) Apply(q *dialect.InsertQuery) {
|
||||
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
|
||||
return StadiaAPIRequests.BeforeInsertHooks.RunHooks(ctx, exec, s)
|
||||
})
|
||||
|
||||
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
||||
vals := make([]bob.Expression, 4)
|
||||
if s.ID.IsValue() {
|
||||
vals[0] = psql.Arg(s.ID.MustGet())
|
||||
} else {
|
||||
vals[0] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if s.Request.IsValue() {
|
||||
vals[1] = psql.Arg(s.Request.MustGet())
|
||||
} else {
|
||||
vals[1] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if s.Response.IsValue() {
|
||||
vals[2] = psql.Arg(s.Response.MustGet())
|
||||
} else {
|
||||
vals[2] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if s.CreatedAt.IsValue() {
|
||||
vals[3] = psql.Arg(s.CreatedAt.MustGet())
|
||||
} else {
|
||||
vals[3] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
||||
}))
|
||||
}
|
||||
|
||||
func (s StadiaAPIRequestSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
||||
return um.Set(s.Expressions()...)
|
||||
}
|
||||
|
||||
func (s StadiaAPIRequestSetter) Expressions(prefix ...string) []bob.Expression {
|
||||
exprs := make([]bob.Expression, 0, 4)
|
||||
|
||||
if s.ID.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "id")...),
|
||||
psql.Arg(s.ID),
|
||||
}})
|
||||
}
|
||||
|
||||
if s.Request.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "request")...),
|
||||
psql.Arg(s.Request),
|
||||
}})
|
||||
}
|
||||
|
||||
if s.Response.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "response")...),
|
||||
psql.Arg(s.Response),
|
||||
}})
|
||||
}
|
||||
|
||||
if s.CreatedAt.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "created_at")...),
|
||||
psql.Arg(s.CreatedAt),
|
||||
}})
|
||||
}
|
||||
|
||||
return exprs
|
||||
}
|
||||
|
||||
// FindStadiaAPIRequest retrieves a single record by primary key
|
||||
// If cols is empty Find will return all columns.
|
||||
func FindStadiaAPIRequest(ctx context.Context, exec bob.Executor, IDPK int64, cols ...string) (*StadiaAPIRequest, error) {
|
||||
if len(cols) == 0 {
|
||||
return StadiaAPIRequests.Query(
|
||||
sm.Where(StadiaAPIRequests.Columns.ID.EQ(psql.Arg(IDPK))),
|
||||
).One(ctx, exec)
|
||||
}
|
||||
|
||||
return StadiaAPIRequests.Query(
|
||||
sm.Where(StadiaAPIRequests.Columns.ID.EQ(psql.Arg(IDPK))),
|
||||
sm.Columns(StadiaAPIRequests.Columns.Only(cols...)),
|
||||
).One(ctx, exec)
|
||||
}
|
||||
|
||||
// StadiaAPIRequestExists checks the presence of a single record by primary key
|
||||
func StadiaAPIRequestExists(ctx context.Context, exec bob.Executor, IDPK int64) (bool, error) {
|
||||
return StadiaAPIRequests.Query(
|
||||
sm.Where(StadiaAPIRequests.Columns.ID.EQ(psql.Arg(IDPK))),
|
||||
).Exists(ctx, exec)
|
||||
}
|
||||
|
||||
// AfterQueryHook is called after StadiaAPIRequest is retrieved from the database
|
||||
func (o *StadiaAPIRequest) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||||
var err error
|
||||
|
||||
switch queryType {
|
||||
case bob.QueryTypeSelect:
|
||||
ctx, err = StadiaAPIRequests.AfterSelectHooks.RunHooks(ctx, exec, StadiaAPIRequestSlice{o})
|
||||
case bob.QueryTypeInsert:
|
||||
ctx, err = StadiaAPIRequests.AfterInsertHooks.RunHooks(ctx, exec, StadiaAPIRequestSlice{o})
|
||||
case bob.QueryTypeUpdate:
|
||||
ctx, err = StadiaAPIRequests.AfterUpdateHooks.RunHooks(ctx, exec, StadiaAPIRequestSlice{o})
|
||||
case bob.QueryTypeDelete:
|
||||
ctx, err = StadiaAPIRequests.AfterDeleteHooks.RunHooks(ctx, exec, StadiaAPIRequestSlice{o})
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// primaryKeyVals returns the primary key values of the StadiaAPIRequest
|
||||
func (o *StadiaAPIRequest) primaryKeyVals() bob.Expression {
|
||||
return psql.Arg(o.ID)
|
||||
}
|
||||
|
||||
func (o *StadiaAPIRequest) pkEQ() dialect.Expression {
|
||||
return psql.Quote("stadia.api_request", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
||||
return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
|
||||
}))
|
||||
}
|
||||
|
||||
// Update uses an executor to update the StadiaAPIRequest
|
||||
func (o *StadiaAPIRequest) Update(ctx context.Context, exec bob.Executor, s *StadiaAPIRequestSetter) error {
|
||||
v, err := StadiaAPIRequests.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = *v
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete deletes a single StadiaAPIRequest record with an executor
|
||||
func (o *StadiaAPIRequest) Delete(ctx context.Context, exec bob.Executor) error {
|
||||
_, err := StadiaAPIRequests.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
// Reload refreshes the StadiaAPIRequest using the executor
|
||||
func (o *StadiaAPIRequest) Reload(ctx context.Context, exec bob.Executor) error {
|
||||
o2, err := StadiaAPIRequests.Query(
|
||||
sm.Where(StadiaAPIRequests.Columns.ID.EQ(psql.Arg(o.ID))),
|
||||
).One(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = *o2
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AfterQueryHook is called after StadiaAPIRequestSlice is retrieved from the database
|
||||
func (o StadiaAPIRequestSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
|
||||
var err error
|
||||
|
||||
switch queryType {
|
||||
case bob.QueryTypeSelect:
|
||||
ctx, err = StadiaAPIRequests.AfterSelectHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeInsert:
|
||||
ctx, err = StadiaAPIRequests.AfterInsertHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeUpdate:
|
||||
ctx, err = StadiaAPIRequests.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||||
case bob.QueryTypeDelete:
|
||||
ctx, err = StadiaAPIRequests.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (o StadiaAPIRequestSlice) pkIN() dialect.Expression {
|
||||
if len(o) == 0 {
|
||||
return psql.Raw("NULL")
|
||||
}
|
||||
|
||||
return psql.Quote("stadia.api_request", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, 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 StadiaAPIRequestSlice) copyMatchingRows(from ...*StadiaAPIRequest) {
|
||||
for i, old := range o {
|
||||
for _, new := range from {
|
||||
if new.ID != old.ID {
|
||||
continue
|
||||
}
|
||||
|
||||
o[i] = new
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
|
||||
func (o StadiaAPIRequestSlice) 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 StadiaAPIRequests.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 *StadiaAPIRequest:
|
||||
o.copyMatchingRows(retrieved)
|
||||
case []*StadiaAPIRequest:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
case StadiaAPIRequestSlice:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
default:
|
||||
// If the retrieved value is not a StadiaAPIRequest or a slice of StadiaAPIRequest
|
||||
// then run the AfterUpdateHooks on the slice
|
||||
_, err = StadiaAPIRequests.AfterUpdateHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
q.AppendWhere(o.pkIN())
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
|
||||
func (o StadiaAPIRequestSlice) 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 StadiaAPIRequests.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 *StadiaAPIRequest:
|
||||
o.copyMatchingRows(retrieved)
|
||||
case []*StadiaAPIRequest:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
case StadiaAPIRequestSlice:
|
||||
o.copyMatchingRows(retrieved...)
|
||||
default:
|
||||
// If the retrieved value is not a StadiaAPIRequest or a slice of StadiaAPIRequest
|
||||
// then run the AfterDeleteHooks on the slice
|
||||
_, err = StadiaAPIRequests.AfterDeleteHooks.RunHooks(ctx, exec, o)
|
||||
}
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
q.AppendWhere(o.pkIN())
|
||||
})
|
||||
}
|
||||
|
||||
func (o StadiaAPIRequestSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals StadiaAPIRequestSetter) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := StadiaAPIRequests.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o StadiaAPIRequestSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := StadiaAPIRequests.Delete(o.DeleteMod()).Exec(ctx, exec)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o StadiaAPIRequestSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
|
||||
if len(o) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
o2, err := StadiaAPIRequests.Query(sm.Where(o.pkIN())).All(ctx, exec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
o.copyMatchingRows(o2...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type stadiaAPIRequestWhere[Q psql.Filterable] struct {
|
||||
ID psql.WhereMod[Q, int64]
|
||||
Request psql.WhereMod[Q, string]
|
||||
Response psql.WhereMod[Q, types.JSON[json.RawMessage]]
|
||||
CreatedAt psql.WhereMod[Q, time.Time]
|
||||
}
|
||||
|
||||
func (stadiaAPIRequestWhere[Q]) AliasedAs(alias string) stadiaAPIRequestWhere[Q] {
|
||||
return buildStadiaAPIRequestWhere[Q](buildStadiaAPIRequestColumns(alias))
|
||||
}
|
||||
|
||||
func buildStadiaAPIRequestWhere[Q psql.Filterable](cols stadiaAPIRequestColumns) stadiaAPIRequestWhere[Q] {
|
||||
return stadiaAPIRequestWhere[Q]{
|
||||
ID: psql.Where[Q, int64](cols.ID),
|
||||
Request: psql.Where[Q, string](cols.Request),
|
||||
Response: psql.Where[Q, types.JSON[json.RawMessage]](cols.Response),
|
||||
CreatedAt: psql.Where[Q, time.Time](cols.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
|
@ -33,6 +33,7 @@
|
|||
pkgs.dart-sass
|
||||
pkgs.esbuild
|
||||
pkgs.go
|
||||
pkgs.go-jet
|
||||
pkgs.goose
|
||||
pkgs.gotools
|
||||
pkgs.lefthook
|
||||
|
|
|
|||
4
go.mod
4
go.mod
|
|
@ -48,8 +48,10 @@ require (
|
|||
github.com/buger/jsonparser v1.1.1 // indirect
|
||||
github.com/chromedp/sysutil v1.1.0 // indirect
|
||||
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-jet/jet/v2 v2.14.1 // indirect
|
||||
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 // indirect
|
||||
github.com/gobwas/httphead v0.1.0 // indirect
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
|
|
@ -75,9 +77,11 @@ require (
|
|||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/qdm12/reprint v0.0.0-20200326205758-722754a53494 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/sethvargo/go-retry v0.3.0 // indirect
|
||||
github.com/stretchr/testify v1.11.1 // indirect
|
||||
github.com/tidwall/geoindex v1.4.4 // indirect
|
||||
github.com/tidwall/gjson v1.12.1 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -81,6 +81,8 @@ github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxI
|
|||
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-jet/jet/v2 v2.14.1 h1:wsfD9e7CGP9h46+IFNlftfncBcmVnKddikbTtapQM3M=
|
||||
github.com/go-jet/jet/v2 v2.14.1/go.mod h1:dqTAECV2Mo3S2NFjbm4vJ1aDruZjhaJ1RAAR8rGUkkc=
|
||||
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 h1:iizUGZ9pEquQS5jTGkh4AqeeHCMbfbjeb0zMt0aEFzs=
|
||||
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
|
||||
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
|
||||
|
|
|
|||
|
|
@ -13,11 +13,13 @@ import (
|
|||
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
||||
bobtypes "github.com/Gleipnir-Technology/bob/types"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/stadia/model"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/stadia/table"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/h3utils"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/stadia"
|
||||
"github.com/aarondl/opt/omit"
|
||||
//"github.com/aarondl/opt/omit"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/uber/h3-go/v4"
|
||||
"resty.dev/v3"
|
||||
|
|
@ -50,7 +52,8 @@ func restyMiddleware(rclient *resty.Client, response *resty.Response) error {
|
|||
//log.Info().Msg("middleware")
|
||||
ctx := context.Background()
|
||||
var body bobtypes.JSON[json.RawMessage]
|
||||
err := body.UnmarshalJSON(response.Bytes())
|
||||
resp_bytes := response.Bytes()
|
||||
err := body.UnmarshalJSON(resp_bytes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unmarshal json in middleware: %w", err)
|
||||
}
|
||||
|
|
@ -59,11 +62,18 @@ func restyMiddleware(rclient *resty.Client, response *resty.Response) error {
|
|||
log.Error().Err(err).Str("url", response.Request.URL).Msg("failed to redact url")
|
||||
return nil
|
||||
}
|
||||
models.StadiaAPIRequests.Insert(&models.StadiaAPIRequestSetter{
|
||||
CreatedAt: omit.From(time.Now()),
|
||||
Request: omit.From(u),
|
||||
Response: omit.From(body),
|
||||
}).One(ctx, db.PGInstance.BobDB)
|
||||
statement := table.APIRequest.INSERT(table.APIRequest.MutableColumns).
|
||||
MODEL(model.APIRequest{
|
||||
CreatedAt: time.Now(),
|
||||
Request: u,
|
||||
Response: string(resp_bytes),
|
||||
}).RETURNING(table.APIRequest.AllColumns)
|
||||
data, err := db.Execute[model.APIRequest](ctx, statement)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to insert stadia request")
|
||||
} else {
|
||||
log.Debug().Int64("id", data.ID).Msg("Created stadia request cache entry")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue