sqlstore

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package sqlstore implements the Auth-All storage boundary over database/sql. One implementation serves every first-party SQL adapter. A Dialect carries the small set of engine-specific behaviors.

Index

Constants

View Source
const SQLiteTimeLayout = "2006-01-02T15:04:05.000000000"

SQLiteTimeLayout is the fixed-width UTC layout used by text timestamp columns. The fixed width keeps lexicographic order equal to time order.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect struct {
	// Name is the schema dialect.
	Name schema.Dialect
	// NumberedPlaceholders selects $1 style placeholders instead of ?.
	NumberedPlaceholders bool
	// TextTime stores timestamps as fixed-width UTC text.
	TextTime bool
	// IsUniqueViolation reports whether err is a uniqueness constraint failure.
	IsUniqueViolation func(err error) bool
	// StringAgg returns the aggregate expression that joins the values of one
	// column with a separator. PostgreSQL uses string_agg and SQLite uses
	// group_concat.
	StringAgg func(expr, separator string) string
}

Dialect carries the engine-specific behavior of one adapter.

type Store

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

Store is the database/sql implementation of store.Store.

func New

func New(db *sql.DB, d Dialect) *Store

New returns a store over db.

func (*Store) APIKeyByHash added in v0.3.0

func (s *Store) APIKeyByHash(ctx context.Context, keyHash string) (*store.APIKey, *store.User, error)

APIKeyByHash implements store.APIKeyStore. The join keeps the credential resolution at one round trip.

func (*Store) APIKeyByID added in v0.3.0

func (s *Store) APIKeyByID(ctx context.Context, id string) (*store.APIKey, error)

APIKeyByID implements store.APIKeyStore.

func (*Store) Accounts

func (s *Store) Accounts() store.AccountStore

Accounts implements store.Store.

func (*Store) ActiveOrganizationOf added in v0.4.0

func (s *Store) ActiveOrganizationOf(ctx context.Context, sessionID string) (string, error)

ActiveOrganizationOf implements store.ActiveOrganizationStore.

func (*Store) AddTeamMember added in v0.4.0

func (s *Store) AddTeamMember(ctx context.Context, teamID, userID string) error

AddTeamMember implements store.TeamStore.

func (*Store) CleanupRateLimits added in v0.3.0

func (s *Store) CleanupRateLimits(ctx context.Context, before time.Time) (int, error)

CleanupRateLimits implements store.RateLimitCounter.

func (*Store) ClearActiveOrganization added in v0.4.0

func (s *Store) ClearActiveOrganization(ctx context.Context, orgID, userID string) error

ClearActiveOrganization implements store.ActiveOrganizationStore.

func (*Store) Close

func (s *Store) Close() error

Close implements store.Store. The database handle stays open because the application owns it.

func (*Store) ConsumeInvitation added in v0.4.0

func (s *Store) ConsumeInvitation(ctx context.Context, tokenHash string, now time.Time) (*store.Invitation, error)

ConsumeInvitation implements store.InvitationStore.

One conditional update changes the status, so two parallel acceptances of one invitation never both pass.

func (*Store) CountAttempt added in v0.3.0

func (s *Store) CountAttempt(ctx context.Context, key string, window time.Duration, now time.Time) (int, time.Time, error)

CountAttempt implements store.RateLimitCounter.

One statement counts the attempt, so two instances cannot both read the same count. The statement needs no lock, so it is safe behind a transaction pooler.

func (*Store) CountMembers added in v0.4.0

func (s *Store) CountMembers(ctx context.Context, orgID string) (int, error)

CountMembers implements store.MembershipStore.

func (*Store) CountPendingInvitations added in v0.4.0

func (s *Store) CountPendingInvitations(ctx context.Context, orgID string, now time.Time) (int, error)

CountPendingInvitations implements store.InvitationStore.

func (*Store) CreateAPIKey added in v0.3.0

func (s *Store) CreateAPIKey(ctx context.Context, k *store.APIKey) error

CreateAPIKey implements store.APIKeyStore.

func (*Store) CreateCustomRole added in v0.4.0

func (s *Store) CreateCustomRole(ctx context.Context, r *store.CustomRole) error

CreateCustomRole implements store.CustomRoleStore.

func (*Store) CreateInvitation added in v0.4.0

func (s *Store) CreateInvitation(ctx context.Context, i *store.Invitation) error

CreateInvitation implements store.InvitationStore.

func (*Store) CreateMembership added in v0.4.0

func (s *Store) CreateMembership(ctx context.Context, m *store.Membership) error

CreateMembership implements store.MembershipStore.

func (*Store) CreateOrganization added in v0.4.0

func (s *Store) CreateOrganization(ctx context.Context, o *store.Organization) error

CreateOrganization implements store.OrganizationStore.

func (*Store) CreateTeam added in v0.4.0

func (s *Store) CreateTeam(ctx context.Context, t *store.Team) error

CreateTeam implements store.TeamStore.

func (*Store) CustomRoleByName added in v0.4.0

func (s *Store) CustomRoleByName(ctx context.Context, orgID, name string) (*store.CustomRole, error)

CustomRoleByName implements store.CustomRoleStore.

func (*Store) DB

func (s *Store) DB() *sql.DB

DB returns the underlying handle. It is owned by the application.

func (*Store) DeleteCustomRole added in v0.4.0

func (s *Store) DeleteCustomRole(ctx context.Context, orgID, name string) error

DeleteCustomRole implements store.CustomRoleStore.

func (*Store) DeleteMembership added in v0.4.0

func (s *Store) DeleteMembership(ctx context.Context, orgID, userID string) error

DeleteMembership implements store.MembershipStore.

func (*Store) DeleteOrganization added in v0.4.0

func (s *Store) DeleteOrganization(ctx context.Context, id string) error

DeleteOrganization implements store.OrganizationStore.

The statements remove every row that belongs to the organization, so no orphan survives on an engine that applies no foreign key.

func (*Store) DeleteRows added in v0.4.0

func (s *Store) DeleteRows(ctx context.Context, table, column string, value any) (int, error)

DeleteRows implements store.RowDeleter.

func (*Store) DeleteSessionsExcept added in v0.3.0

func (s *Store) DeleteSessionsExcept(ctx context.Context, sessionID string) (int, error)

DeleteSessionsExcept implements store.SessionRevoker. One statement finds the owner and removes the other sessions, so no read and no lock is needed.

func (*Store) DeleteTeam added in v0.4.0

func (s *Store) DeleteTeam(ctx context.Context, id string) error

DeleteTeam implements store.TeamStore.

The team memberships go with the team. The organization memberships stay, because a team is a group inside one organization.

func (*Store) InsertRow added in v0.3.0

func (s *Store) InsertRow(ctx context.Context, table string, columns []string, values []any) error

InsertRow implements store.RowWriter.

func (*Store) InvitationByID added in v0.4.0

func (s *Store) InvitationByID(ctx context.Context, id string) (*store.Invitation, error)

InvitationByID implements store.InvitationStore.

func (*Store) InvitationByTokenHash added in v0.4.0

func (s *Store) InvitationByTokenHash(ctx context.Context, tokenHash string) (*store.Invitation, error)

InvitationByTokenHash implements store.InvitationStore.

func (*Store) ListAPIKeys added in v0.3.0

func (s *Store) ListAPIKeys(ctx context.Context, userID string) ([]store.APIKey, error)

ListAPIKeys implements store.APIKeyStore.

func (*Store) ListCustomRoles added in v0.4.0

func (s *Store) ListCustomRoles(ctx context.Context, orgID string) ([]store.CustomRole, error)

ListCustomRoles implements store.CustomRoleStore.

func (*Store) ListInvitations added in v0.4.0

func (s *Store) ListInvitations(ctx context.Context, f store.InvitationFilter) ([]store.Invitation, string, error)

ListInvitations implements store.InvitationStore.

func (*Store) ListMembers added in v0.4.0

func (s *Store) ListMembers(ctx context.Context, f store.MemberFilter) ([]store.Membership, string, error)

ListMembers implements store.MembershipStore.

The order is the user identifier, which is unique inside one organization.

func (*Store) ListOrganizations added in v0.4.0

func (s *Store) ListOrganizations(ctx context.Context, f store.OrganizationFilter) ([]store.Organization, string, error)

ListOrganizations implements store.OrganizationStore.

The order is the slug and then the identifier, so a page is stable while rows arrive and leave.

func (*Store) ListTeamMembers added in v0.4.0

func (s *Store) ListTeamMembers(ctx context.Context, teamID string) ([]string, error)

ListTeamMembers implements store.TeamStore.

func (*Store) ListTeams added in v0.4.0

func (s *Store) ListTeams(ctx context.Context, orgID string) ([]store.Team, error)

ListTeams implements store.TeamStore.

func (*Store) ListUsers added in v0.3.0

func (s *Store) ListUsers(ctx context.Context, f store.UserListFilter) ([]store.User, string, error)

ListUsers implements store.UserAdminStore.

The order is the normalized email and then the identifier, so two users with one address order the same way in every page.

func (*Store) LockActiveMembersWithRole added in v0.4.0

func (s *Store) LockActiveMembersWithRole(ctx context.Context, orgID, role string) ([]string, error)

LockActiveMembersWithRole implements store.MembershipStore.

func (*Store) LockEnabledUsersWithRole added in v0.3.0

func (s *Store) LockEnabledUsersWithRole(ctx context.Context, role string) ([]string, error)

LockEnabledUsersWithRole implements store.UserAdminStore.

func (*Store) MembershipOf added in v0.4.0

func (s *Store) MembershipOf(ctx context.Context, orgID, userID string) (*store.Membership, error)

MembershipOf implements store.MembershipStore.

func (*Store) MembershipsOfUser added in v0.4.0

func (s *Store) MembershipsOfUser(ctx context.Context, userID string) ([]store.Membership, error)

MembershipsOfUser implements store.MembershipStore.

func (*Store) Migrator

func (s *Store) Migrator() store.Migrator

Migrator implements store.Store.

func (*Store) OAuthStates

func (s *Store) OAuthStates() store.OAuthStateStore

OAuthStates implements store.Store.

func (*Store) OrganizationByID added in v0.4.0

func (s *Store) OrganizationByID(ctx context.Context, id string) (*store.Organization, error)

OrganizationByID implements store.OrganizationStore.

func (*Store) OrganizationBySlug added in v0.4.0

func (s *Store) OrganizationBySlug(ctx context.Context, slug string) (*store.Organization, error)

OrganizationBySlug implements store.OrganizationStore.

func (*Store) RecoveryCodes added in v0.2.0

func (s *Store) RecoveryCodes() store.RecoveryCodeStore

RecoveryCodes implements store.Store.

func (*Store) RemoveTeamMember added in v0.4.0

func (s *Store) RemoveTeamMember(ctx context.Context, teamID, userID string) error

RemoveTeamMember implements store.TeamStore.

func (*Store) RevokeAPIKey added in v0.3.0

func (s *Store) RevokeAPIKey(ctx context.Context, id, byUserID string, at time.Time) error

RevokeAPIKey implements store.APIKeyStore.

func (*Store) SessionWithUser added in v0.3.0

func (s *Store) SessionWithUser(ctx context.Context, tokenHash string) (*store.Session, *store.User, error)

SessionWithUser implements store.SessionUserReader. It joins the session row and the user row, so credential resolution costs one round trip.

func (*Store) SessionWithUserAndMembership added in v0.4.0

func (s *Store) SessionWithUserAndMembership(ctx context.Context, tokenHash string) (
	*store.Session, *store.User, *store.Organization, *store.Membership, error)

SessionWithUserAndMembership implements store.SessionOrgReader.

One statement returns the session row, the user row, the organization row, and the membership row. A permission check therefore costs no extra round trip. The joins are left joins, so a session with no active organization returns the same row shape.

func (*Store) Sessions

func (s *Store) Sessions() store.SessionStore

Sessions implements store.Store.

func (*Store) SetActiveOrganization added in v0.4.0

func (s *Store) SetActiveOrganization(ctx context.Context, sessionID, orgID string) error

SetActiveOrganization implements store.ActiveOrganizationStore.

func (*Store) SetInvitationStatus added in v0.4.0

func (s *Store) SetInvitationStatus(ctx context.Context, id, from, to string) error

SetInvitationStatus implements store.InvitationStore.

func (*Store) TOTP added in v0.2.0

func (s *Store) TOTP() store.TOTPStore

TOTP implements store.Store.

func (*Store) TableColumns added in v0.3.0

func (s *Store) TableColumns(ctx context.Context, table string) ([]string, bool, error)

TableColumns implements store.CatalogInspector.

func (*Store) TeamByID added in v0.4.0

func (s *Store) TeamByID(ctx context.Context, id string) (*store.Team, error)

TeamByID implements store.TeamStore.

func (*Store) TeamsOfUser added in v0.4.0

func (s *Store) TeamsOfUser(ctx context.Context, orgID, userID string) ([]store.Team, error)

TeamsOfUser implements store.TeamStore.

func (*Store) Tokens

func (s *Store) Tokens() store.TokenStore

Tokens implements store.Store.

func (*Store) TouchAPIKey added in v0.3.0

func (s *Store) TouchAPIKey(ctx context.Context, id string, at time.Time) error

TouchAPIKey implements store.APIKeyStore.

func (*Store) Transaction

func (s *Store) Transaction(ctx context.Context, fn func(store.Store) error) error

Transaction implements store.Store. A nested call joins the running transaction instead of opening a second one.

func (*Store) UpdateMembership added in v0.4.0

func (s *Store) UpdateMembership(ctx context.Context, m *store.Membership) error

UpdateMembership implements store.MembershipStore.

func (*Store) UpdateOrganization added in v0.4.0

func (s *Store) UpdateOrganization(ctx context.Context, o *store.Organization) error

UpdateOrganization implements store.OrganizationStore.

func (*Store) UseSchema added in v0.3.0

func (s *Store) UseSchema(o schema.Options) error

UseSchema implements store.SchemaConfigurable. It sets the physical table names of the host.

func (*Store) Users

func (s *Store) Users() store.UserStore

Users implements store.Store.

Jump to

Keyboard shortcuts

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