data-work
Mike Holloway 3 months ago
parent ff6de84731
commit b886c39ac9
  1. 119
      main.go
  2. 43
      models.go

@ -1,56 +1,87 @@
package main
import (
"fmt"
"context"
"context"
"database/sql"
"errors"
"database/sql"
"fmt"
"github.com/uptrace/bun"
"github.com/uptrace/bun/extra/bundebug"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
)
var feeds []Feed
var categories []Category
var entries []Entry
var users []User
var feeds []Feed
var categories []Category
var entries []Entry
var users []User
func main() {
var err error
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"),
))
// Turn these into funcs with error handling, logging, return vals etc.
feederr := db.NewSelect().Model(&feeds).OrderExpr("title ASC").Limit(10).Scan(ctx)
if feederr != nil {
errors.Join(feederr, err)
}
categoryerr := db.NewSelect().Model(&categories).OrderExpr("title ASC").Scan(ctx)
if categoryerr != nil {
errors.Join(categoryerr, err)
}
entryerr := db.NewSelect().Model(&entries).OrderExpr("published_at ASC").Limit(10).Scan(ctx)
if entryerr != nil {
errors.Join(entryerr, err)
}
usererr := db.NewSelect().Model(&users).OrderExpr("id ASC").Scan(ctx)
if usererr != nil {
errors.Join(usererr, err)
}
fmt.Printf("%+v\n", entries)
fmt.Printf("%+v\n", categories)
fmt.Printf("%+v\n", feeds)
fmt.Printf("%+v\n", users)
//
var err error
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"),
))
// Turn these into funcs with error handling, logging, return vals etc.
feederr := db.NewSelect().
Model(&feeds).
OrderExpr("title ASC").
Limit(10).
Scan(ctx)
if feederr != nil {
errors.Join(feederr, err)
}
categoryerr := db.NewSelect().
Model(&categories).
OrderExpr("title ASC").
Scan(ctx)
if categoryerr != nil {
errors.Join(categoryerr, err)
}
entryerr := db.NewSelect().
Model(&entries).
OrderExpr("published_at ASC").
Limit(10).
Scan(ctx)
if entryerr != nil {
errors.Join(entryerr, err)
}
usererr := db.NewSelect().
Model(&users).
OrderExpr("id ASC").
Scan(ctx)
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)
// fmt.Printf("%+v\n", users)
}

@ -4,34 +4,43 @@ 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"`
bun.BaseModel `bun:"table:users"`
ID int64 `bun:"id,pk,autoincrement"`
Username string `bun:"username"`
ID int64 `bun:"id,pk,autoincrement"`
Username string `bun:"username"`
}
type Feed struct {
bun.BaseModel `bun:"table:feeds"`
bun.BaseModel `bun:"table:feeds"`
ID int64 `bun:"id,pk,autoincrement"`
CategoryID int64 `bun:"category_id"`
Title string `bun:"title"`
//SELECT id,category_id,title FROM feeds ORDER BY id;
ID int64 `bun:"id,pk,autoincrement"`
CategoryID int64 `bun:"category_id"`
Title string `bun:"title"`
// SELECT id,category_id,title FROM feeds ORDER BY id;
}
type Entry struct {
bun.BaseModel `bun:"table:entries"`
bun.BaseModel `bun:"table:entries"`
ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title"`
Author string `bun:"author"`
//SELECT id,title,author FROM entries ORDER BY published_at LIMIT 10;
ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title"`
Author string `bun:"author"`
// SELECT id,title,author FROM entries ORDER BY published_at LIMIT 10;
}
type Category struct {
bun.BaseModel `bun:"table:categories"`
bun.BaseModel `bun:"table:categories"`
ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title"`
//SELECT id,title from categories;
ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title"`
//SELECT id,title from categories;
}

Loading…
Cancel
Save