sqlstore

package
v0.4.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 10 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 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

Examples

Constants

View Source
const SchemaVersion = 1

SchemaVersion is the schema version New creates and accepts.

Variables

View Source
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).

View Source
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

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

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
}

func (*Store) Close

func (s *Store) Close() error

Close closes the underlying *sql.DB.

func (*Store) DB

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

DB returns the underlying *sql.DB.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, account string, id uint64) error

Delete removes account's persistent notification id, or returns realtime.ErrNotFound.

func (*Store) Get

func (s *Store) Get(ctx context.Context, account string, id uint64) (realtime.Notification, error)

Get returns account's stored notification id, or 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

Save persists n, returning the stored copy with its assigned monotonic ID.

Jump to

Keyboard shortcuts

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