sqlite

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package sqlite provides SQLite database utilities wrapping modernc.org/sqlite.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InMemory

func InMemory() (*sql.DB, error)

InMemory creates an in-memory SQLite database for testing.

Example:

db, err := sqlite.InMemory()
if err != nil {
    t.Fatal(err)
}
defer db.Close()

func Open

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

Open opens a SQLite database with default settings.

Uses modernc.org/sqlite (pure Go, zero CGO) with production-ready defaults:

  • WAL mode for better concurrency
  • Foreign keys enabled
  • Connection pooling configured
  • Performance pragmas applied

Example:

db, err := sqlite.Open("app.db")
if err != nil {
    log.Fatal(err)
}
defer db.Close()

func OpenContext

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

OpenContext opens a SQLite database with context.

func OpenWithConfig

func OpenWithConfig(ctx context.Context, cfg Config) (*sql.DB, error)

OpenWithConfig opens a SQLite database with custom settings.

Example:

db, err := sqlite.OpenWithConfig(ctx, sqlite.Config{
    Path:         "app.db",
    WALMode:      true,
    MaxOpenConns: 50,
})

func Transaction

func Transaction(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error) error

Transaction executes a function within a SQLite transaction. Automatically commits on success, rolls back on error or panic.

Example:

err := sqlite.Transaction(ctx, db, func(tx *sql.Tx) error {
    _, err := tx.ExecContext(ctx, "INSERT INTO users (name) VALUES (?)", "John")
    return err
})

Types

type Config

type Config struct {
	// Path is the database file path. Use ":memory:" for in-memory database.
	Path string

	// WALMode enables Write-Ahead Logging for better concurrency.
	// Default: true
	WALMode bool

	// BusyTimeout is how long to wait for locks.
	// Default: 5 seconds
	BusyTimeout time.Duration

	// MaxOpenConns is the maximum number of open connections.
	// Default: 25
	MaxOpenConns int

	// MaxIdleConns is the maximum number of idle connections.
	// Default: 5
	MaxIdleConns int

	// ConnMaxLifetime is how long a connection can be reused.
	// Default: 1 hour
	ConnMaxLifetime time.Duration

	// ForeignKeys enables foreign key constraints.
	// Default: true
	ForeignKeys bool
}

Config configures SQLite connection behavior.

func DefaultConfig

func DefaultConfig(path string) Config

DefaultConfig returns production-ready defaults.

Jump to

Keyboard shortcuts

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