Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joeychilson/pghook
A Go library for executing hooks on insert, update, and delete events in PostgreSQL.
https://github.com/joeychilson/pghook
events go golang hooks postgres postgresql pubsub
Last synced: 1 day ago
JSON representation
A Go library for executing hooks on insert, update, and delete events in PostgreSQL.
- Host: GitHub
- URL: https://github.com/joeychilson/pghook
- Owner: joeychilson
- License: mit
- Created: 2023-03-16T11:38:56.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-15T06:43:02.000Z (2 days ago)
- Last Synced: 2024-11-15T07:30:31.958Z (2 days ago)
- Topics: events, go, golang, hooks, postgres, postgresql, pubsub
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pghook
A Go library for executing hooks on insert, update, and delete events in PostgreSQL.
## Installation
```bash
go get github.com/joeychilson/pghook
```## Example
```go
package mainimport (
"context"
"fmt"
"log"
"log/slog"
"os""github.com/jackc/pgx/v5/pgxpool"
"github.com/joeychilson/pghook"
)func main() {
ctx := context.Background()logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))pool, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}type User struct {
ID int `json:"id"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
}hook := pghook.New[User](pool, pghook.WithLogger(logger))
if err != nil {
log.Fatal(err)
}hook.OnInsert("users", func(ctx context.Context, e pghook.InsertPayload[User]) error {
fmt.Println("inserted", e.Row)
return nil
})hook.OnUpdate("users", func(ctx context.Context, e pghook.UpdatePayload[User]) error {
fmt.Println("updated", e.OldRow, e.NewRow)
return nil
})hook.OnDelete("users", func(ctx context.Context, e pghook.DeletePayload[User]) error {
fmt.Println("deleted", e.Row)
return nil
})if err := hook.Listen(ctx); err != nil {
log.Fatal(err)
}
}
```