Saltar al contenido principal
Version: 0.6.0

Client API Reference

Client wraps a *sql.DB and owns Quark configuration: dialect, logger, SQLGuard, limits, middleware, observers, and cache store.

New

New(driverName, dataSource string, opts ...any) (*Client, error)

Opens a database connection, pings it, and returns a configured *Client. The dialect is auto-detected from driverName; override it with WithDialect only when the driver name is ambiguous (e.g. registering a custom dialect under a shared driver name like pgx).

import (
"log/slog"

"github.com/jcsvwinston/quark"
_ "github.com/lib/pq"
)

client, err := quark.New("postgres", "postgres://user:pass@localhost/app?sslmode=disable",
quark.WithLogger(slog.Default()),
)
if err != nil {
return err
}
defer client.Close()

Driver name → dialect mapping (auto-detected):

Driver nameDialect
sqlite, sqlite3SQLite
postgres, pgxPostgreSQL
mysqlMySQL
mariadbMariaDB
sqlserver, mssqlMSSQL
oracle, godrorOracle

opts accepts both client options (Option) and pool options (PoolOption). Pool options are applied before the ping; client options after.

Errors:

CaseError
sql.Open failswraps ErrConnection
PingContext failswraps ErrConnection

Options

WithDialect(d Dialect) Option

Overrides the dialect that would otherwise be auto-detected from the driver name. Useful when a driver name is shared between engines (e.g. pgx for both PostgreSQL and CockroachDB) or when registering a custom dialect.

client, err := quark.New("pgx", dsn, quark.WithDialect(quark.PostgreSQL()))

Available constructors:

ConstructorDialect name
quark.PostgreSQL()postgres
quark.MySQL()mysql
quark.MariaDB()mariadb
quark.SQLite()sqlite
quark.MSSQL()mssql
quark.Oracle()oracle

WithLogger(l *slog.Logger) Option

Sets the structured logger.

logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
client, err := quark.New("postgres", dsn,
quark.WithLogger(logger),
)

Default: slog.Default().

WithLimits(l Limits) Option

Sets security and performance limits.

limits := quark.DefaultLimits()
limits.AllowRawQueries = true

client, err := quark.New("postgres", dsn,
quark.WithLimits(limits),
)
type Limits struct {
MaxQueryLength int
MaxResults int
MaxJoins int
MaxWhereConditions int
QueryTimeout time.Duration
AllowRawQueries bool
SafeMigrations bool
}

DefaultLimits() Limits

Returns:

FieldDefault
MaxQueryLength10 * 1024
MaxResults10000
MaxJoins5
MaxWhereConditions20
QueryTimeout30 * time.Second
AllowRawQueriesfalse
SafeMigrationstrue

WithCacheStore(s CacheStore) Option

Attaches a cache backend.

store := memory.New()

client, err := quark.New("sqlite", "file:app.db?cache=shared",
quark.WithCacheStore(store),
)

WithMiddleware(m Middleware) Option

Adds middleware to the execution chain.

client, err := quark.New("postgres", dsn,
quark.WithMiddleware(quarkotel.New()),
)

WithQueryObserver(o QueryObserver) Option

Adds a post-execution observer.

client, err := quark.New("postgres", dsn,
quark.WithQueryObserver(&MetricsObserver{}),
)

For

For[T any](ctx context.Context, provider ClientProvider) *Query[T]

Creates a typed query builder for model T.

users, err := quark.For[User](ctx, client).
Where("active", "=", true).
OrderBy("created_at", "DESC").
Limit(50).
List()

provider can be a *Client or *TenantRouter.

type ClientProvider interface {
GetClient(ctx context.Context) (*Client, error)
}

If a provider cannot return a client, the returned query stores that error and will return it on execution.

RawQuery

RawQuery(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Executes raw SQL that returns rows.

limits := quark.DefaultLimits()
limits.AllowRawQueries = true

client, err := quark.New("postgres", dsn,
quark.WithLimits(limits),
)

rows, err := client.RawQuery(ctx,
"SELECT id, email FROM users WHERE active = $1",
true,
)

Raw queries are disabled by default. RawQuery also asks SQLGuard to validate that placeholders are present and that obvious injection patterns are absent.

Exec

Exec(ctx context.Context, query string, args ...any) error

Executes raw SQL that does not return rows.

err := client.Exec(ctx,
"CREATE INDEX idx_users_email ON users(email)",
)

Exec requires AllowRawQueries: true and runs raw-query validation.

Raw

Raw() *sql.DB

Returns the underlying database handle.

db := client.Raw()
stats := db.Stats()

Operations performed on Raw() bypass Quark validation, middleware, cache invalidation, observers, and tenant routing.

Close

Close() error

Closes the underlying *sql.DB.

defer client.Close()

Dialect

Dialect() Dialect

Returns the active dialect.

if client.Dialect().Name() == "postgres" {
// PostgreSQL-specific integration
}