fixture

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Analyses added in v0.0.2

type Analyses interface {
	AnalysesReader
	AnalysesWriter
}

Analyses exposes all queries in its generated store group.

type AnalysesReader added in v0.0.2

type AnalysesReader interface {
	// GetAnalysis executes the generated GetAnalysis query.
	GetAnalysis(ctx context.Context, arg *GetAnalysisParams, storeOptions ...QueryOption) (*Analysis, error)
}

AnalysesReader exposes read queries in the Analyses store group.

type AnalysesWriter added in v0.0.2

type AnalysesWriter interface {
}

AnalysesWriter exposes write queries in the Analyses store group.

type Analysis

type Analysis struct {
	ID           int64
	TenantID     int64
	Description  *string
	State        NullAnalysisState
	Source       netip.Addr
	ActiveWindow pgtype.Range[pgtype.Timestamptz]
}

type AnalysisState

type AnalysisState string
const (
	AnalysisStatePending  AnalysisState = "pending"
	AnalysisStateComplete AnalysisState = "complete"
)

func (*AnalysisState) Scan

func (e *AnalysisState) Scan(src interface{}) error

type CreateUserParams

type CreateUserParams struct {
	ID       int64
	TenantID int64
	Name     string
}

type DBTX

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
}

type GetAnalysisParams

type GetAnalysisParams struct {
	TenantID int64
	ID       int64
}

type GetUserParams

type GetUserParams struct {
	TenantID int64
	ID       int64
}

type NullAnalysisState

type NullAnalysisState struct {
	AnalysisState AnalysisState
	Valid         bool // Valid is true if AnalysisState is not NULL
}

func (*NullAnalysisState) Scan

func (ns *NullAnalysisState) Scan(value interface{}) error

Scan implements the Scanner interface.

func (NullAnalysisState) Value

func (ns NullAnalysisState) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Querier

type Querier interface {
	// kind: write
	// shard: tenant(tenant_id)
	// store: Users
	CreateUser(ctx context.Context, arg *CreateUserParams) (*User, error)
	// kind: read
	// shard: tenant(tenant_id)
	// store: Analyses
	GetAnalysis(ctx context.Context, arg *GetAnalysisParams) (*Analysis, error)
	// kind: read
	// shard: tenant(tenant_id)
	// store: Users
	GetUser(ctx context.Context, arg *GetUserParams) (*User, error)
	// kind: write
	// shard: tenant(tenant_id)
	// store: Users
	UpdateUserName(ctx context.Context, arg *UpdateUserNameParams) (*User, error)
}

type Queries

type Queries struct {
	// contains filtered or unexported fields
}

func New

func New(db DBTX) *Queries

func (*Queries) CreateUser

func (q *Queries) CreateUser(ctx context.Context, arg *CreateUserParams) (*User, error)

kind: write shard: tenant(tenant_id) store: Users

func (*Queries) GetAnalysis

func (q *Queries) GetAnalysis(ctx context.Context, arg *GetAnalysisParams) (*Analysis, error)

kind: read shard: tenant(tenant_id) store: Analyses

func (*Queries) GetUser

func (q *Queries) GetUser(ctx context.Context, arg *GetUserParams) (*User, error)

kind: read shard: tenant(tenant_id) store: Users

func (*Queries) UpdateUserName added in v0.0.2

func (q *Queries) UpdateUserName(ctx context.Context, arg *UpdateUserNameParams) (*User, error)

kind: write shard: tenant(tenant_id) store: Users

func (*Queries) WithTx

func (q *Queries) WithTx(tx pgx.Tx) *Queries

type QueryOption added in v0.0.2

type QueryOption func(*queryOptions)

QueryOption customizes routing for one generated query call.

func ReadFromPrimary

func ReadFromPrimary() QueryOption

ReadFromPrimary routes a read query to the primary database.

func WithTx

func WithTx(tx pgx.Tx) QueryOption

WithTx executes a query through tx and suppresses write mirrors.

type ShardResolver

type ShardResolver[SK any] interface {
	// Tenant resolves the "tenant" shard route.
	Tenant(tenantID int64) SK
}

ShardResolver resolves generated query parameters to shard keys.

type ShardedOption added in v0.0.2

type ShardedOption func(*shardedOptions)

ShardedOption customizes a sharded topology.

func WithReplicaSet added in v0.0.2

func WithReplicaSet(name string, primary DBTX, replicas ...DBTX) ShardedOption

WithReplicaSet appends a named primary and its optional read replicas.

func WithVShardMapping added in v0.0.2

func WithVShardMapping(mainReplicaSet string, vshards []uint64, mirrorReplicaSets ...string) ShardedOption

WithVShardMapping maps virtual shards to a main replica set and optional ordered write mirrors.

type SingletonOption added in v0.0.2

type SingletonOption func(*singletonConfig)

SingletonOption customizes a single-database topology.

func WithDatabaseName added in v0.0.2

func WithDatabaseName(name string) SingletonOption

WithDatabaseName identifies the database in telemetry. An empty name defaults to "default".

func WithReadReplicas added in v0.0.2

func WithReadReplicas(databases ...DBTX) SingletonOption

WithReadReplicas appends databases used for round-robin reads.

func WithWriteMirrors added in v0.0.2

func WithWriteMirrors(databases ...DBTX) SingletonOption

WithWriteMirrors appends databases that synchronously receive writes.

type Store added in v0.0.2

type Store interface {
	// Analyses returns the Analyses query group.
	Analyses() Analyses
	// Users returns the Users query group.
	Users() Users
}

Store is the topology-independent generated query API.

Example
log := &callLog{}
queries, err := NewStore(
	context.Background(),
	Sharded(
		1,
		pgmesh.ConstantShardHashFor[uint64](0),
		tenantResolver{},
		WithReplicaSet(
			"main",
			&fakeDB{name: "primary", log: log},
			&fakeDB{name: "replica", log: log},
		),
		WithReplicaSet("mirror", &fakeDB{name: "mirror", log: log}),
		WithVShardMapping("main", []uint64{0}, "mirror"),
	),
)
if err != nil {
	panic(err)
}

ctx := context.Background()
users := queries.Users()
if _, err := users.GetUser(ctx, &GetUserParams{TenantID: 10, ID: 20}); err != nil {
	panic(err)
}
if _, err := users.GetUser(ctx, &GetUserParams{TenantID: 10, ID: 20}, ReadFromPrimary()); err != nil {
	panic(err)
}
if _, err := users.CreateUser(ctx, &CreateUserParams{ID: 20, TenantID: 10, Name: "user"}); err != nil {
	panic(err)
}

fmt.Println(log.snapshot())
Output:
[replica primary primary mirror]

func NewStore added in v0.0.2

func NewStore(ctx context.Context, topology Topology, options ...StoreOption) (Store, error)

NewStore creates the generated query API from an opaque topology configuration.

type StoreOption added in v0.0.2

type StoreOption func(*storeOptions)

StoreOption customizes telemetry for a generated store.

func WithLogger added in v0.0.2

func WithLogger(logger *slog.Logger) StoreOption

WithLogger configures optional structured logging for routed queries. A nil logger disables logging.

func WithMeterProvider added in v0.0.2

func WithMeterProvider(provider metric.MeterProvider) StoreOption

WithMeterProvider configures the provider used for routed query metrics. A nil provider uses the global OpenTelemetry meter provider.

func WithTracerProvider added in v0.0.2

func WithTracerProvider(provider trace.TracerProvider) StoreOption

WithTracerProvider configures the provider used for routed query spans. A nil provider uses the global OpenTelemetry tracer provider.

type Topology added in v0.0.2

type Topology interface {
	// contains filtered or unexported methods
}

Topology is an opaque database topology for Store.

func Sharded added in v0.0.2

func Sharded[SK any](numVShards uint64, shardHasher pgmesh.ShardHasher[SK], resolver ShardResolver[SK], options ...ShardedOption) Topology

Sharded returns an opaque sharded topology.

func Singleton added in v0.0.2

func Singleton(primary DBTX, options ...SingletonOption) Topology

Singleton returns a topology with one primary database.

type UpdateUserNameParams added in v0.0.2

type UpdateUserNameParams struct {
	TenantID int64
	ID       int64
	Name     string
}

type User

type User struct {
	ID       int64
	TenantID int64
	Name     string
}

type Users added in v0.0.2

type Users interface {
	UsersReader
	UsersWriter
}

Users exposes all queries in its generated store group.

type UsersReader added in v0.0.2

type UsersReader interface {
	// GetUser executes the generated GetUser query.
	GetUser(ctx context.Context, arg *GetUserParams, storeOptions ...QueryOption) (*User, error)
}

UsersReader exposes read queries in the Users store group.

type UsersWriter added in v0.0.2

type UsersWriter interface {
	// CreateUser executes the generated CreateUser query.
	CreateUser(ctx context.Context, arg *CreateUserParams, storeOptions ...QueryOption) (*User, error)
	// UpdateUserName executes the generated UpdateUserName query.
	UpdateUserName(ctx context.Context, arg *UpdateUserNameParams, storeOptions ...QueryOption) (*User, error)
}

UsersWriter exposes write queries in the Users store group.

Directories

Path Synopsis
commands
separate

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL