sqlite

package
v0.1.36 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

db/sqlite/sqlite.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(path string, timeout time.Duration) (*sql.DB, error)

Connect opens a SQLite database with sensible defaults for web applications. It enables WAL mode for better concurrency, foreign keys, and sets a busy timeout.

The returned *sql.DB is a pool of connections. For SQLite, this is typically limited to a single writer, but multiple readers are supported in WAL mode.

The caller is responsible for calling db.Close() when done.

Path can be:

  • A file path: "./data.db", "/var/lib/myapp/data.db"
  • ":memory:" for an in-memory database (data lost on close)
  • "file::memory:?cache=shared" for a shared in-memory database

func ConnectWithOptions

func ConnectWithOptions(path string, opts Options, timeout time.Duration) (*sql.DB, error)

ConnectWithOptions opens a SQLite database with custom options.

The caller is responsible for calling db.Close() when done.

func HealthCheck

func HealthCheck(db *sql.DB) func(ctx context.Context) error

HealthCheck returns a health check function compatible with the health package.

Example:

health.Mount(r, map[string]health.Check{
    "sqlite": sqlite.HealthCheck(db),
}, logger)

func InMemory

func InMemory(timeout time.Duration) (*sql.DB, error)

InMemory opens a shared in-memory SQLite database. Multiple connections can access the same in-memory database. Data is lost when all connections close.

The caller is responsible for calling db.Close() when done.

Types

type Options

type Options struct {
	// WALMode enables Write-Ahead Logging for better concurrent read performance.
	// Default: true (recommended for web applications)
	WALMode bool

	// ForeignKeys enables foreign key constraint enforcement.
	// Default: true
	ForeignKeys bool

	// BusyTimeout sets how long to wait when the database is locked (milliseconds).
	// Default: 5000 (5 seconds)
	BusyTimeout int

	// CacheSize sets the page cache size in KiB (negative) or pages (positive).
	// Default: -64000 (64MB)
	CacheSize int

	// Synchronous sets the synchronous mode for durability vs performance.
	// Options: "OFF", "NORMAL", "FULL", "EXTRA"
	// Default: "NORMAL" (good balance for WAL mode)
	Synchronous string

	// JournalMode overrides WALMode setting. Usually leave empty.
	// Options: "DELETE", "TRUNCATE", "PERSIST", "MEMORY", "WAL", "OFF"
	JournalMode string

	// MaxOpenConns limits concurrent connections. SQLite handles this internally,
	// but limiting helps avoid "database is locked" errors.
	// Default: 1 (recommended for most cases)
	MaxOpenConns int

	// MaxIdleConns sets idle connection pool size.
	// Default: 1
	MaxIdleConns int

	// ConnMaxLifetime sets maximum connection reuse time.
	// Default: 0 (no limit)
	ConnMaxLifetime time.Duration
}

Options configures SQLite database behavior.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns sensible defaults for web applications.

Settings:

  • WAL mode enabled (better concurrency)
  • Foreign keys enabled
  • 5 second busy timeout
  • 64MB cache
  • NORMAL synchronous mode
  • Single connection (avoids lock contention)

func InMemoryOptions

func InMemoryOptions() Options

InMemoryOptions returns options for in-memory databases. Data is lost when the connection closes.

func ReadOnlyOptions

func ReadOnlyOptions() Options

ReadOnlyOptions returns options optimized for read-only access. Useful for read replicas or when you only need to query data.

Jump to

Keyboard shortcuts

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