Documentation
¶
Overview ¶
Package sqlstore is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package sqlstore implements realtime.Store over database/sql for SQLite and Postgres, opened through a driver package (sqlitestore or pgstore). It imports no SQL driver itself, so New works with any *sql.DB matching a supported Dialect.
The schema holds two tables created by New when missing and versioned in realtime_schema:
- realtime_schema: schema version integer (currently SchemaVersion = 1)
- realtime_notifications: monotonic ID, account_id, code, subject, sender, JSON content text, and created_at Unix milliseconds, indexed by (account_id, id DESC) for newest-first cursor pagination.
Index ¶
- Constants
- Variables
- type Dialect
- type Store
- func (s *Store) Close() error
- func (s *Store) DB() *sql.DB
- func (s *Store) Delete(ctx context.Context, account string, id uint64) error
- func (s *Store) Get(ctx context.Context, account string, id uint64) (realtime.Notification, error)
- func (s *Store) List(ctx context.Context, account string, cursor string, limit int) (realtime.Page, error)
- func (s *Store) Save(ctx context.Context, n realtime.Notification) (realtime.Notification, error)
Examples ¶
Constants ¶
const SchemaVersion = 1
SchemaVersion is the schema version New creates and accepts.
Variables ¶
var Postgres = Dialect{ Name: "postgres", Numbered: true, IDColumn: "BIGSERIAL PRIMARY KEY", MigrateLock: `SELECT pg_advisory_xact_lock(1919710317)`, }
Postgres is Postgres's dialect. Its migration advisory lock uses class key 1919710317 ("rltm" in ASCII).
var SQLite = Dialect{
Name: "sqlite",
IDColumn: "INTEGER PRIMARY KEY AUTOINCREMENT",
}
SQLite is SQLite's dialect.
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 (Postgres), false for "?" (SQLite).
Numbered bool
// IDColumn is the primary-key monotonic column definition (e.g. "INTEGER PRIMARY KEY AUTOINCREMENT" or "BIGSERIAL PRIMARY KEY").
IDColumn string
// MigrateLock, when set, runs first in the migration transaction to serialize concurrent schema migrations across processes.
MigrateLock string
}
Dialect describes the SQL syntax differences between supported databases.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a SQL-backed realtime.Store. It is safe for concurrent use.
func New ¶
New returns a Store on db, creating the schema when missing and refusing databases whose schema version is newer than SchemaVersion.
Example ¶
New wraps an open *sql.DB with either sqlstore.SQLite or sqlstore.Postgres.
package main
import (
"context"
"database/sql"
"github.com/nsgo-dev/game/pkg/online/realtime/sqlstore"
)
func main() {
open := func(db *sql.DB) error {
_, err := sqlstore.New(context.Background(), db, sqlstore.Postgres)
return err
}
_ = open
}
Output:
func (*Store) Delete ¶
Delete removes account's persistent notification id, or returns realtime.ErrNotFound.
func (*Store) List ¶
func (s *Store) List(ctx context.Context, account string, cursor string, limit int) (realtime.Page, error)
List returns account's persistent notifications newest first using cursor pagination.
func (*Store) Save ¶
func (s *Store) Save(ctx context.Context, n realtime.Notification) (realtime.Notification, error)
Save persists n, returning the stored copy with its assigned monotonic ID.