go example

This commit is contained in:
2025-07-27 06:10:24 +00:00
parent 36e4145365
commit 63b71692b5

View File

@@ -41,4 +41,38 @@ Plain SQL queries are not inherently bad but come with challenges:
Run the following command to generate schema files:
```bash
go run code.partial.tech/go/pgm/cmd -o ./db ./schema.sql
go run code.partial.tech/go/pgm/cmd -o ./db ./schema.sql
```
once you have the schama files created you can use `pgm` as
```go
package main
import "code.partial.tech/go/pgm"
type MyModel struct {
ID string
Email string
}
func main() {
println("Initializing pgx connection pool")
pgm.InitPool(pgm.Config{
ConnString: url,
})
// Select query to fetch the first record
// Assumes the schema is defined in the "db" package with a User table
var v MyModel
err := db.User.Select(db.User.ID, db.User.Email).
Where(db.User.Email.Like("anki%")).
First(context.TODO(), &v.ID, &v.Email)
if err != nil {
println("Error:", err.Error())
return
}
println("User email:", v.Email)
}
```