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
This commit is contained in:
parent
b886c39ac9
commit
5bbe08ded5
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
__debug*
|
||||
.*.sw*
|
24
anvildb.go
Normal file
24
anvildb.go
Normal file
@ -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)
|
43
main.go
43
main.go
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user