go example
This commit is contained in:
36
README.md
36
README.md
@@ -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)
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user