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 ( import (
"context" "context"
"database/sql"
"errors" "errors"
"fmt" "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 var feeds []Feed
@ -22,64 +16,55 @@ func main() {
ctx := context.Background() ctx := context.Background()
dsn := "postgres://apache:@localhost:5432/miniflux?sslmode=disable" dsn := "postgres://apache:@localhost:5432/miniflux?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432" // 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. // Turn these into funcs with error handling, logging, return vals etc.
feederr := db.NewSelect(). feederr := anvildb.NewSelect().
Model(&feeds). Model(&Feed{}).
OrderExpr("title ASC"). OrderExpr("title ASC").
Limit(10). Limit(10).
Scan(ctx) Scan(ctx, &feeds)
if feederr != nil { if feederr != nil {
errors.Join(feederr, err) errors.Join(feederr, err)
} }
categoryerr := db.NewSelect(). categoryerr := anvildb.NewSelect().
Model(&categories). Model(&Category{}).
OrderExpr("title ASC"). OrderExpr("title ASC").
Scan(ctx) Scan(ctx, &categories)
if categoryerr != nil { if categoryerr != nil {
errors.Join(categoryerr, err) errors.Join(categoryerr, err)
} }
entryerr := db.NewSelect(). entryerr := anvildb.NewSelect().
Model(&entries). Model(&Entry{}).
OrderExpr("published_at ASC"). OrderExpr("published_at ASC").
Limit(10). Limit(10).
Scan(ctx) Scan(ctx, &entries)
if entryerr != nil { if entryerr != nil {
errors.Join(entryerr, err) errors.Join(entryerr, err)
} }
usererr := db.NewSelect(). usererr := anvildb.NewSelect().
Model(&users). Model(&User{}).
OrderExpr("id ASC"). OrderExpr("id ASC").
Scan(ctx) Scan(ctx, &users)
if usererr != nil { if usererr != nil {
errors.Join(usererr, err) errors.Join(usererr, err)
} }
fmt.Printf("[]Entry\n")
for _, val := range entries { for _, val := range entries {
fmt.Printf("val = %s\n", val) fmt.Printf("val = %s\n", val)
} }
fmt.Printf("[]Category\n")
for _, val := range categories { for _, val := range categories {
fmt.Printf("val = %s\n", val) fmt.Printf("val = %s\n", val)
} }
fmt.Printf("[]Feed\n")
for _, val := range feeds { for _, val := range feeds {
fmt.Printf("val = %s\n", val) fmt.Printf("val = %s\n", val)
} }
fmt.Printf("[]User\n")
for _, val := range users { for _, val := range users {
fmt.Printf("val = %s\n", val) fmt.Printf("val = %s\n", val)
} }
// fmt.Printf("%+v\n", entries) // fmt.Printf("%+v\n", entries)
// fmt.Printf("%+v\n", categories) // fmt.Printf("%+v\n", categories)
// fmt.Printf("%+v\n", feeds) // fmt.Printf("%+v\n", feeds)

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

Loading…
Cancel
Save