new file: .gitignore

delve debug, vim swap files
new file:   anvildb.go
	ConnectAnvilDB func
modified:   main.go
	Moving things around, investigating interface references
modified:   models.go
	Removed previous AnvilDB, AnvilData struct work
data-work
Mike Holloway 3 months ago
parent b886c39ac9
commit 5bbe08ded5
  1. 2
      .gitignore
  2. 24
      anvildb.go
  3. 43
      main.go
  4. 9
      models.go

2
.gitignore vendored

@ -0,0 +1,2 @@
__debug*
.*.sw*

@ -0,0 +1,24 @@
package main
import (
"database/sql"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
)
func ConnectAnvilDB(dsn string) *bun.DB {
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
db := bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bundebug.NewQueryHook(
bundebug.WithVerbose(true),
bundebug.FromEnv("BUNDEBUG"),
))
return db
}
//func GetFeeds(db *bun.DB)

@ -2,14 +2,8 @@ package main
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
)
var feeds []Feed
@ -22,64 +16,55 @@ func main() {
ctx := context.Background()
dsn := "postgres://apache:@localhost:5432/miniflux?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
db := bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bundebug.NewQueryHook(
bundebug.WithVerbose(true),
bundebug.FromEnv("BUNDEBUG"),
))
anvildb := ConnectAnvilDB(dsn)
// Turn these into funcs with error handling, logging, return vals etc.
feederr := db.NewSelect().
Model(&feeds).
feederr := anvildb.NewSelect().
Model(&Feed{}).
OrderExpr("title ASC").
Limit(10).
Scan(ctx)
Scan(ctx, &feeds)
if feederr != nil {
errors.Join(feederr, err)
}
categoryerr := db.NewSelect().
Model(&categories).
categoryerr := anvildb.NewSelect().
Model(&Category{}).
OrderExpr("title ASC").
Scan(ctx)
Scan(ctx, &categories)
if categoryerr != nil {
errors.Join(categoryerr, err)
}
entryerr := db.NewSelect().
Model(&entries).
entryerr := anvildb.NewSelect().
Model(&Entry{}).
OrderExpr("published_at ASC").
Limit(10).
Scan(ctx)
Scan(ctx, &entries)
if entryerr != nil {
errors.Join(entryerr, err)
}
usererr := db.NewSelect().
Model(&users).
usererr := anvildb.NewSelect().
Model(&User{}).
OrderExpr("id ASC").
Scan(ctx)
Scan(ctx, &users)
if usererr != nil {
errors.Join(usererr, err)
}
fmt.Printf("[]Entry\n")
for _, val := range entries {
fmt.Printf("val = %s\n", val)
}
fmt.Printf("[]Category\n")
for _, val := range categories {
fmt.Printf("val = %s\n", val)
}
fmt.Printf("[]Feed\n")
for _, val := range feeds {
fmt.Printf("val = %s\n", val)
}
fmt.Printf("[]User\n")
for _, val := range users {
fmt.Printf("val = %s\n", val)
}
// fmt.Printf("%+v\n", entries)
// fmt.Printf("%+v\n", categories)
// fmt.Printf("%+v\n", feeds)

@ -4,15 +4,6 @@ import (
"github.com/uptrace/bun"
)
type AnvilDB struct {
DBContext *Context
}
type AnvilData struct {
Feeds []Feed
Categories []Category
Entries []Entry
Users []User
}
type User struct {
bun.BaseModel `bun:"table:users"`

Loading…
Cancel
Save