stdentsaas

package
v0.0.72 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package stdentsaas provides re-usable SaaS code using Ent as the ORM.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthenticatedUser

func AuthenticatedUser(ctx context.Context) (string, bool)

AuthenticatedUser returns the user that the context is authenticated for.

func NoTestForMaxQueryPlanCosts added in v0.0.63

func NoTestForMaxQueryPlanCosts(ctx context.Context) bool

NoTestForMaxQueryPlanCosts returns whether the cost check is disabled.

func WithAuthenticatedOrganizations

func WithAuthenticatedOrganizations(
	ctx context.Context, first OrganizationRole, more ...OrganizationRole,
) context.Context

WithAuthenticatedOrganizations declares on the context that any calls carrying the context has access to these organizations with the provided role.

func WithAuthenticatedUser

func WithAuthenticatedUser(ctx context.Context, userID string) context.Context

WithAuthenticatedUser declares on the context that it is has access to a user with the provided id.

func WithNoTestForMaxQueryPlanCosts added in v0.0.63

func WithNoTestForMaxQueryPlanCosts(ctx context.Context) context.Context

WithNoTestForMaxQueryPlanCosts allow disabling the plan cost check.

Types

type Driver

type Driver struct {
	entdialect.Driver
	// contains filtered or unexported fields
}

Driver is an opionated Ent driver that wraps a base driver but only allows interactions with the database to be done through a transaction with specific isolation properties and auth settings applied properly.

func NewDriver

func NewDriver(
	base entdialect.Driver,
	opts ...DriverOption,
) *Driver

NewDriver inits the driver.

func (Driver) BeginTx

func (d Driver) BeginTx(ctx context.Context, opts *sql.TxOptions) (entdialect.Tx, error)

BeginTx calls the base driver's method if it's supported and calls our hook.

func (Driver) Exec

func (d Driver) Exec(_ context.Context, _ string, _, _ any) error

Exec executes a query that does not return records. For example, in SQL, INSERT or UPDATE. It scans the result into the pointer v. For SQL drivers, it is dialect/sql.Result.

func (Driver) Query

func (d Driver) Query(_ context.Context, _ string, _, _ any) error

Query executes a query that returns rows, typically a SELECT in SQL. It scans the result into the pointer v. For SQL drivers, it is *dialect/sql.Rows.

func (Driver) Tx

func (d Driver) Tx(ctx context.Context) (entdialect.Tx, error)

Tx will begin a transaction with linearizable isolation level.

type DriverOption

type DriverOption func(*Driver)

func AnonymousUserID

func AnonymousUserID(s string) DriverOption

AnonymousUserID configures the id that will be used in the setting when the user is not authenticated.

func AuthenticatedOrganizationsSetting

func AuthenticatedOrganizationsSetting(s string) DriverOption

AuthenticatedOrganizationsSetting configures the transaction-scoped postgres setting that will cary which organizations the user is autthenticated for.

func AuthenticatedUserSetting

func AuthenticatedUserSetting(s string) DriverOption

AuthenticatedUserSetting configures the transaction-scoped postgres setting that will cary which user is authenticated for.

func DiscourageSequentialScans added in v0.0.35

func DiscourageSequentialScans() DriverOption

DiscourageSequentialScans will dis-incentivize the query planner to use sequential scans for all transactions. This is mainly useful with the TestForMaxQueryPlanCost option to assert that queries under testing are missing an index.

func TestForMaxQueryPlanCosts added in v0.0.33

func TestForMaxQueryPlanCosts(maxCost float64) DriverOption

TestForMaxQueryPlanCosts will enable EXPLAIN on every query that is executed with the driver and fail when the cost of the resulting query is above the maximum. Together with the enable_seqscan=OFF it can help test of infefficient queries do to missing indexes.

func TxExecQueryLoggingLevel added in v0.0.67

func TxExecQueryLoggingLevel(v zapcore.Level) DriverOption

TxExecQueryLoggingLevel configures the level at which transaction's exec and query sql logs are send to the logger.

type OrganizationRole

type OrganizationRole struct {
	OrganizationID string `json:"organization_id"`
	Role           string `json:"role"`
}

OrganizationRole describes a role in an organization. Both fields are encoded as strings such that RLS policies can easily cast it to a type that is relevant in the schema at hand, such as UUID or an enum.

func AuthenticatedOrganizations

func AuthenticatedOrganizations(ctx context.Context) (ors []OrganizationRole, ok bool)

AuthenticatedOrganizations returns the organization and roles from the context or panic.

type Tx added in v0.0.33

type Tx struct {
	entdialect.Tx
	MaxQueryPlanCosts float64
	// contains filtered or unexported fields
}

Tx wraps a Ent transaction to provide us with the ability to hook any sql before it's being executed. In our case we ant to fail tests when the to-be-executed query plan has a cost that is too high.

func (Tx) Exec added in v0.0.33

func (tx Tx) Exec(ctx context.Context, query string, args, v any) error

Exec executes a query that does not return records. For example, in SQL, INSERT or UPDATE. It scans the result into the pointer v. For SQL drivers, it is dialect/sql.Result.

func (Tx) Query added in v0.0.33

func (tx Tx) Query(ctx context.Context, query string, args, v any) error

Query executes a query that returns rows, typically a SELECT in SQL. It scans the result into the pointer v. For SQL drivers, it is *dialect/sql.Rows.

Jump to

Keyboard shortcuts

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