Simplify readme example

pull/53/head
Vinícius Garcia 2024-10-23 23:39:17 -03:00
parent 64ddb22a00
commit 14b1ba47d3
3 changed files with 21 additions and 21 deletions

View File

@ -72,10 +72,7 @@ type Post struct {
}
// Address have a one to one relationship with User
//
// Note that we are omitting the name of the ID column
// below because "id" is already the default:
var AddressesTable = ksql.NewTable("addresses")
var AddressesTable = ksql.NewTable("addresses", "id")
type Address struct {
ID int `ksql:"id"`
@ -85,7 +82,7 @@ type Address struct {
func main() {
ctx := context.Background()
dbURL, closeDB := startPostgresDB(ctx)
dbURL, closeDB := startExampleDB(ctx)
defer closeDB()
db, err := kpgx.New(ctx, dbURL, ksql.Config{})
@ -94,11 +91,6 @@ func main() {
}
defer db.Close()
err = createTablesAndRecords(ctx, db)
if err != nil {
log.Fatalf("error creating tables: %s", err)
}
// For querying only some attributes you can
// create a custom struct like this:
var count []struct {

View File

@ -30,10 +30,7 @@ type Post struct {
}
// Address have a one to one relationship with User
//
// Note that we are omitting the name of the ID column
// below because "id" is already the default:
var AddressesTable = ksql.NewTable("addresses")
var AddressesTable = ksql.NewTable("addresses", "id")
type Address struct {
ID int `ksql:"id"`
@ -43,7 +40,7 @@ type Address struct {
func main() {
ctx := context.Background()
dbURL, closeDB := startPostgresDB(ctx)
dbURL, closeDB := startExampleDB(ctx)
defer closeDB()
db, err := kpgx.New(ctx, dbURL, ksql.Config{})
@ -52,11 +49,6 @@ func main() {
}
defer db.Close()
err = createTablesAndRecords(ctx, db)
if err != nil {
log.Fatalf("error creating tables: %s", err)
}
// For querying only some attributes you can
// create a custom struct like this:
var count []struct {

View File

@ -11,9 +11,25 @@ import (
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/vingarcia/ksql"
"github.com/vingarcia/ksql/adapters/kpgx"
)
func createTablesAndRecords(ctx context.Context, db ksql.DB) error {
func startExampleDB(ctx context.Context) (dbURL string, closer func()) {
dbURL, closeDB := startPostgresDB(ctx)
db, err := kpgx.New(ctx, dbURL, ksql.Config{})
if err != nil {
log.Fatalf("startExampleDB(): unable connect to database: %s", err)
}
err = populateDatabase(ctx, db)
if err != nil {
log.Fatalf("startExampleDB(): error populating example database: %s", err)
}
return dbURL, closeDB
}
func populateDatabase(ctx context.Context, db ksql.DB) error {
return errors.Join(
exec(ctx, db,
`CREATE TABLE users (