How can we use sqlc with pgx (postgres) for dealing with transactions?

153 Views Asked by At

I'm new to sqlc and i want to integrate transactions in my microservice.

Should i pass a pool & queries at the same time ?

my repository defines only queries

type OrgRepository struct {
    queries *query.Queries
}
// versions:
//   sqlc v1.25.0

package query

import (
    "context"

    "github.com/jackc/pgx/v5"
    "github.com/jackc/pgx/v5/pgconn"
)

type DBTX interface {
    Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
    Query(context.Context, string, ...interface{}) (pgx.Rows, error)
    QueryRow(context.Context, string, ...interface{}) pgx.Row
}

func New(db DBTX) *Queries {
    return &Queries{db: db}
}

type Queries struct {
    db DBTX
}

func (q *Queries) WithTx(tx pgx.Tx) *Queries {
    return &Queries{
        db: tx,
    }
}
0

There are 0 best solutions below