sqlstore

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 8 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 sqlstore is an accounts.Store over database/sql, shared by the SQLite (package sqlitestore) and Postgres (package pgstore) stores. Open a store with one of those; use this package directly only to add another SQL database through a Dialect.

The schema is three tables, created by New when missing and versioned in accounts_schema:

  • accounts: id, email (unique, null for none), password_hash, created_at
  • account_identities: provider and subject (the key), account_id (unique per provider), created_at
  • account_sessions: id, account_id, secret_hash, created_at

Times are Unix milliseconds. Uniqueness is enforced by the database's constraints, so concurrent services sharing one database cannot give one email address or identity to two accounts.

Index

Examples

Constants

View Source
const SchemaVersion = 1

SchemaVersion is the schema version New creates and accepts.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect struct {
	// Name names the database in errors.
	Name string
	// Numbered is true for "$1" placeholders, false for "?".
	Numbered bool
	// IsUnique reports whether err is a unique or primary key constraint violation.
	IsUnique func(err error) bool
	// MigrateLock, when set, runs first in the migration's transaction to serialize migrations of several processes, such as Postgres's pg_advisory_xact_lock.
	MigrateLock string
}

Dialect is what differs between SQL databases.

type Store

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

Store is an accounts.Store in a SQL database.

func New

func New(ctx context.Context, db *sql.DB, d Dialect) (*Store, error)

New returns a store on db, creating the schema when it is missing. It refuses a database whose schema is newer than SchemaVersion.

Example

New creates the accounts schema on an open database and returns a store on it; a Dialect tells it how the database numbers parameters and reports a unique violation. The sqlitestore and pgstore packages call New with their dialects; this example uses SQLite's, with the driver sqlitestore registers.

package main

import (
	"context"
	"database/sql"
	"fmt"

	"github.com/nsgo-dev/game/pkg/accounts"
	"github.com/nsgo-dev/game/pkg/accounts/sqlitestore"
	"github.com/nsgo-dev/game/pkg/accounts/sqlstore"
)

func main() {
	ctx := context.Background()
	db, err := sql.Open("sqlite", ":memory:")
	if err != nil {
		fmt.Println(err)
		return
	}
	// An in-memory SQLite database exists per connection, so keep to one.
	db.SetMaxOpenConns(1)
	store, err := sqlstore.New(ctx, db, sqlitestore.Dialect)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer store.Close()

	fmt.Println(store.CreateAccount(ctx, accounts.Account{ID: "a1", Email: "ada@example.com"}, ""))
	fmt.Println(store.CreateAccount(ctx, accounts.Account{ID: "a2", Email: "ada@example.com"}, ""))
}
Output:
<nil>
accounts: already belongs to another account

func (*Store) Account

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

Account implements accounts.Store.

func (*Store) AccountByEmail

func (s *Store) AccountByEmail(ctx context.Context, email string) (accounts.Account, string, error)

AccountByEmail implements accounts.Store.

func (*Store) AccountByIdentity

func (s *Store) AccountByIdentity(ctx context.Context, identity accounts.Identity) (accounts.Account, error)

AccountByIdentity implements accounts.Store.

func (*Store) Close

func (s *Store) Close() error

Close closes the store's database.

func (*Store) CreateAccount

func (s *Store) CreateAccount(ctx context.Context, a accounts.Account, passwordHash string, identities ...accounts.Identity) error

CreateAccount implements accounts.Store.

func (*Store) CreateSession

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

CreateSession implements accounts.Store.

func (*Store) DB

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

DB returns the store's database.

func (*Store) DeleteSession

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

DeleteSession implements accounts.Store.

func (*Store) Identities

func (s *Store) Identities(ctx context.Context, id string) ([]accounts.Identity, error)

Identities implements accounts.Store.

func (*Store) LinkIdentity

func (s *Store) LinkIdentity(ctx context.Context, id string, identity accounts.Identity, at time.Time) error

LinkIdentity implements accounts.Store.

func (*Store) Session

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

Session implements accounts.Store.

func (*Store) SetEmail

func (s *Store) SetEmail(ctx context.Context, id, email, passwordHash string) error

SetEmail implements accounts.Store.

func (*Store) SetPasswordHash

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

SetPasswordHash implements accounts.Store.

Jump to

Keyboard shortcuts

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