store

package
v0.0.0-...-610d9af Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: GPL-3.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package store is the persistence layer: it implements the repository interfaces that the service package declares, on top of SQLite (via database/sql) and sqlc-generated queries.

Its two jobs besides running SQL:

  • translate between sqlc's row types (strings, int64) and domain types (time.Time, TaskStatus);
  • translate driver errors into domain errors, so no other package ever imports database/sql or the SQLite driver to interpret a failure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DSN

func DSN(path string) string

DSN builds the connection string. Pragmas listed here run on EVERY connection the pool opens (they are per-connection settings in SQLite):

  • foreign_keys(1): off by default; without it ON DELETE CASCADE and REFERENCES do nothing.
  • journal_mode(WAL): readers do not block the writer and vice versa.
  • synchronous(NORMAL): the standard pairing with WAL; durable across app crashes, and loses at most the last transactions on an OS crash or power cut, never corrupts the database.
  • busy_timeout(5000): a blocked writer waits up to 5s instead of failing with "database is locked".
  • _txlock=immediate: transactions start with BEGIN IMMEDIATE, taking the write lock up front. A read-then-write transaction that starts as a plain (deferred) read can fail with SQLITE_BUSY when it tries to upgrade, and busy_timeout cannot help with that.

func Migrate

func Migrate(ctx context.Context, db *sql.DB) error

Migrate applies every pending migration. It uses goose's Provider API: explicit *sql.DB and fs.FS arguments, no package-level state, structured results.

Run it from ONE place (the deploy step, or this process at startup when there is a single instance, as here). Several replicas migrating simultaneously is a classic outage. goose takes a lock on PostgreSQL, but the safe habit is "one migrator".

func Open

func Open(ctx context.Context, path string) (*sql.DB, error)

Open opens the database file (creating it if needed) and verifies it is reachable. sql.Open alone never connects, so Ping makes misconfiguration fail at startup instead of on request 1.

Types

type Store

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

Store implements the service layer's repository interfaces (users, accounts, sessions, verifications, projects, tasks).

func New

func New(db *sql.DB) *Store

New wraps an open database. The caller owns db and closes it.

func (*Store) ConsumeVerification

func (s *Store) ConsumeVerification(ctx context.Context, purpose, valueHash string, now time.Time) (string, error)

ConsumeVerification atomically finds, deletes and returns the identifier of an unexpired token of the given purpose whose stored value (the token's SHA-256) matches. domain.ErrNotFound when there is none: unknown, already used, expired, or wrong purpose.

func (*Store) CreateAccount

func (s *Store) CreateAccount(ctx context.Context, a domain.Account) error

func (*Store) CreateProject

func (s *Store) CreateProject(ctx context.Context, p domain.Project) error

func (*Store) CreateSession

func (s *Store) CreateSession(ctx context.Context, sess domain.Session) error

func (*Store) CreateTask

func (s *Store) CreateTask(ctx context.Context, t domain.Task) error

func (*Store) CreateUserWithAccount

func (s *Store) CreateUserWithAccount(ctx context.Context, u domain.User, a domain.Account) error

CreateUserWithAccount inserts a user and its first account in ONE transaction. A user without any account could never sign in, and an account without a user violates the foreign key, so the two must appear together or not at all. A duplicate email is domain.ErrConflict.

func (*Store) DeleteExpiredSessions

func (s *Store) DeleteExpiredSessions(ctx context.Context, now time.Time) (int64, error)

func (*Store) DeleteExpiredVerifications

func (s *Store) DeleteExpiredVerifications(ctx context.Context, now time.Time) (int64, error)

func (*Store) DeleteOtherSessions

func (s *Store) DeleteOtherSessions(ctx context.Context, userID, keepID string) error

DeleteOtherSessions deletes all of the user's sessions except keepID.

func (*Store) DeleteProject

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

func (*Store) DeleteSessionByID

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

func (*Store) DeleteSessionByTokenHash

func (s *Store) DeleteSessionByTokenHash(ctx context.Context, tokenHash string) error

DeleteSessionByTokenHash and DeleteSessionByID are idempotent: deleting nothing is success (sign-out twice, or two requests racing to delete the same expired session).

func (*Store) DeleteTask

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

func (*Store) DeleteUserSession

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

DeleteUserSession deletes one of the USER'S sessions. A session id that belongs to someone else is indistinguishable from a missing one: domain.ErrNotFound.

func (*Store) ExtendSession

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

ExtendSession pushes expires_at out (sliding expiry).

func (*Store) GetAccount

func (s *Store) GetAccount(ctx context.Context, providerID, accountID string) (domain.Account, error)

GetAccount finds the account of provider by the provider's own user id.

func (*Store) GetCredentialAccount

func (s *Store) GetCredentialAccount(ctx context.Context, userID string) (domain.Account, error)

GetCredentialAccount returns the user's email+password account; ErrNotFound for users who only ever signed in with an OAuth provider.

func (*Store) GetProject

func (s *Store) GetProject(ctx context.Context, ownerID, id string) (domain.Project, error)

func (*Store) GetSessionByTokenHash

func (s *Store) GetSessionByTokenHash(ctx context.Context, tokenHash string) (domain.Session, error)

GetSessionByTokenHash returns the session regardless of expiry; the service decides what an expired one means (and deletes it).

func (*Store) GetTask

func (s *Store) GetTask(ctx context.Context, ownerID, id string) (domain.Task, error)

func (*Store) GetUserByEmail

func (s *Store) GetUserByEmail(ctx context.Context, email string) (domain.User, error)

func (*Store) GetUserByID

func (s *Store) GetUserByID(ctx context.Context, id string) (domain.User, error)

func (*Store) ListActiveSessions

func (s *Store) ListActiveSessions(ctx context.Context, userID string, now time.Time) ([]domain.Session, error)

ListActiveSessions returns the user's unexpired sessions, newest first.

func (*Store) ListProjects

func (s *Store) ListProjects(ctx context.Context, ownerID, afterID string, limit int) ([]domain.Project, error)

func (*Store) ListTasks

func (s *Store) ListTasks(ctx context.Context, projectID string, status *domain.TaskStatus, afterID string, limit int) ([]domain.Task, error)

ListTasks returns up to limit tasks of a project with id > afterID, ordered by id. A nil status means "any status". The caller must already have verified project ownership.

func (*Store) MarkEmailVerified

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

MarkEmailVerified sets email_verified; domain.ErrNotFound if the user does not exist.

func (*Store) RenameProject

func (s *Store) RenameProject(ctx context.Context, ownerID, id, name string, at time.Time) (domain.Project, error)

func (*Store) ReplaceVerification

func (s *Store) ReplaceVerification(ctx context.Context, v domain.Verification) error

ReplaceVerification stores v after deleting older tokens with the same identifier, so asking for a new reset email invalidates the previous link. One transaction: never zero valid tokens after a failure in between.

func (*Store) ResetCredentialPassword

func (s *Store) ResetCredentialPassword(ctx context.Context, a domain.Account) error

ResetCredentialPassword sets a user's password after a successful reset and signs them out everywhere, atomically. If the user has no credential account yet (they only used GitHub), a.* describes the one to create. All sessions are deleted in the same transaction: whoever knew the OLD password, or held a stolen session, must not stay logged in after a reset.

func (*Store) UpdateAccountTokens

func (s *Store) UpdateAccountTokens(ctx context.Context, a domain.Account) error

UpdateAccountTokens refreshes the stored provider credentials of an OAuth account.

func (*Store) UpdatePasswordHash

func (s *Store) UpdatePasswordHash(ctx context.Context, accountID, hash string, at time.Time) error

UpdatePasswordHash replaces the stored hash of one account (rehash on login).

func (*Store) UpdateTask

func (s *Store) UpdateTask(ctx context.Context, ownerID, id string, apply func(*domain.Task) error) (domain.Task, error)

UpdateTask is an atomic read-modify-write: it loads the task (scoped to ownerID), lets apply change it (validation, patch semantics: that is business logic and lives in the service), and writes it back, all in one transaction. Two concurrent PATCHes therefore serialize instead of one silently overwriting the other's fields.

func (*Store) WithTx

func (s *Store) WithTx(ctx context.Context, fn func(tx *Store) error) (err error)

WithTx runs fn inside a transaction: committed if fn returns nil, rolled back otherwise (including on panic, which is re-raised). fn receives a Store bound to the transaction, so everything it does is atomic.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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