Documentation
¶
Overview ¶
Package stdentsaas provides re-usable SaaS code using Ent as the ORM.
Index ¶
- func AuthenticatedUser(ctx context.Context) (string, bool)
- func NoTestForMaxQueryPlanCosts(ctx context.Context) bool
- func WithAuthenticatedOrganizations(ctx context.Context, first OrganizationRole, more ...OrganizationRole) context.Context
- func WithAuthenticatedUser(ctx context.Context, userID string) context.Context
- func WithNoTestForMaxQueryPlanCosts(ctx context.Context) context.Context
- type Driver
- type DriverOption
- func AnonymousUserID(s string) DriverOption
- func AuthenticatedOrganizationsSetting(s string) DriverOption
- func AuthenticatedUserSetting(s string) DriverOption
- func DiscourageSequentialScans() DriverOption
- func TestForMaxQueryPlanCosts(maxCost float64) DriverOption
- func TxExecQueryLoggingLevel(v zapcore.Level) DriverOption
- type OrganizationRole
- type Tx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AuthenticatedUser ¶
AuthenticatedUser returns the user that the context is authenticated for.
func NoTestForMaxQueryPlanCosts ¶ added in v0.0.63
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 ¶
WithAuthenticatedUser declares on the context that it is has access to a user with the provided id.
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 ¶
BeginTx calls the base driver's method if it's supported and calls our hook.
func (Driver) Exec ¶
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.
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.