db

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 13 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 db is the server's access to the tables, in Cloudflare D1 or any supported SQL database (PostgreSQL, MySQL, SQLite).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	URL                                              string
	D1APIBase, D1AccountID, D1DatabaseID, D1APIToken string
}

Config chooses the backend: URL (postgres://, mysql://, sqlite:///path, d1://) or, when it is empty or d1://, the D1 credentials.

type DB

type DB interface {
	Dialect() sdk.Dialect
	// Name describes the backend for people ("PostgreSQL", "Cloudflare D1").
	Name() string
	// Query runs a statement the server built. "?" placeholders.
	Query(ctx context.Context, query string, args ...any) (*Rows, error)
	// QueryUntrusted runs SQL from a person or the model: read-only and
	// time-limited wherever the database can enforce it.
	QueryUntrusted(ctx context.Context, query string) (*Rows, error)
	Exec(ctx context.Context, query string, args ...any) error
	ExecScript(ctx context.Context, script string) error
	// InsertID runs an INSERT into a table with an "id" key and returns it.
	InsertID(ctx context.Context, query string, args ...any) (int64, error)
	Close() error
}

func Open

func Open(cfg Config) (DB, error)

type QueryError

type QueryError struct{ Err error }

QueryError is the database rejecting a statement (syntax, unknown column, timeout), as opposed to the database being unreachable. Its message is safe to show and to feed back to the model.

func (*QueryError) Error

func (e *QueryError) Error() string

func (*QueryError) Unwrap

func (e *QueryError) Unwrap() error

type Rows

type Rows struct {
	Columns    []string
	Rows       [][]any
	RowsRead   int64
	DurationMs float64
}

Rows is a result in column order. RowsRead is -1 when the database does not report it.

Jump to

Keyboard shortcuts

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