-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
35 lines (27 loc) · 714 Bytes
/
main.go
File metadata and controls
35 lines (27 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"context"
"log"
"github.com/immannino/transactor"
"github.com/jackc/pgx/v4/pgxpool"
)
type App struct {
db *pgxpool.Pool
txer transactor.Transactor
}
func main() {
db, _ := pgxpool.Connect(context.Background(), "postgres://admin:admin@localhost:5432")
txer, _ := transactor.New(db)
app := &App{db, txer}
// do some stuff
_ = app.txer.WithinTransaction(context.Background(), func(ctx context.Context) error {
tx := transactor.ExtractTx(ctx)
_, err := tx.Exec(ctx, "INSERT INTO users (name, phone) VALUES ('Spongebob', '80012345678')")
if err != nil {
log.Println("Error with txn")
return err
}
log.Println("Successfully inserted users")
return nil
})
}