go example
This commit is contained in:
34
README.md
34
README.md
@@ -42,3 +42,37 @@ Run the following command to generate schema files:
|
|||||||
|
|
||||||
```bash
|
```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