sqlitestore

package
v0.4.0-rc.2 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: 7 Imported by: 1 module, 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 sqlitestore is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package sqlitestore opens an accounts.Store in a SQLite database file, with the pure-Go modernc.org/sqlite driver (no cgo). It suits a single account service process; several processes sharing one database should use Postgres (package pgstore).

The package builds for every target but the web: a browser has no file system to keep the database in, and the account service runs on a server.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Dialect = sqlstore.Dialect{
	Name: "sqlite",
	IsUnique: func(err error) bool {
		var e *sqlite.Error
		return errors.As(err, &e) && (e.Code() == constraintUnique || e.Code() == constraintPrimaryKey)
	},
}

Dialect is SQLite's sqlstore dialect. It has no RowLock: Open takes every transaction's write lock when it begins (_txlock=immediate) on a single connection, so the store's transactions already run one at a time.

Functions

func Open

func Open(ctx context.Context, path string) (*sqlstore.Store, error)

Open opens (creating when missing) the SQLite database at path, or a private in-memory database for ":memory:", and returns a store on it with the schema created. The database uses write-ahead logging, enforces foreign keys and waits up to five seconds for a lock another process holds.

The store uses one connection, so its statements run one at a time; SQLite serializes writes anyway. Close the store to close the database.

Example

Open returns an accounts.Store in a SQLite database file; ":memory:" opens a private in-memory database, as here.

package main

import (
	"context"
	"fmt"
	"time"

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

func main() {
	ctx := context.Background()
	store, err := sqlitestore.Open(ctx, ":memory:")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer store.Close()

	created := time.Date(2026, 11, 23, 12, 0, 0, 0, time.UTC)
	err = store.CreateAccount(ctx, accounts.Account{ID: "a1", Email: "ada@example.com", Created: created}, "")
	fmt.Println(err)
	a, _, err := store.AccountByEmail(ctx, "ada@example.com")
	fmt.Println(a.ID, err)
	_, err = store.Account(ctx, "missing")
	fmt.Println(err)
}
Output:
<nil>
a1 <nil>
accounts: not found

func OpenReader

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

OpenReader opens a second handle on the SQLite database file at path, which Open has already opened, for work that must not queue behind the store's single connection: a service's readiness check and the counts its metrics scan. Its connections only read (query_only), and the database's write-ahead log lets them read while the store writes, so neither a long write delays them nor a full-table count delays the store. It refuses ":memory:", a database only the store's own connection sees. Close the handle when the service stops.

Types

This section is empty.

Jump to

Keyboard shortcuts

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