cfd1

package
v1.0.28 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package cfd1 executes SQL against Cloudflare D1 (bound binding or REST API).

Index

Constants

View Source
const (
	// EngineName is the Apito driver engine identifier for Cloudflare D1.
	EngineName = "cloudflared1"
)

Variables

View Source
var (
	// ErrNotImplemented is returned by cloudflared1 methods not yet ported to D1.
	ErrNotImplemented = errors.New("cloudflared1: not implemented")
	// ErrCredentialsRequired indicates missing D1 database id or API credentials.
	ErrCredentialsRequired = errors.New("cloudflared1: credentials required")
)
View Source
var ErrDailyReadCapExceeded = errors.New("cfd1: daily read cap exceeded")

ErrDailyReadCapExceeded is returned when D1_DAILY_READ_CAP is exceeded.

Functions

func CreateDatabase

func CreateDatabase(ctx context.Context, accountID, apiToken, name string) (databaseID string, err error)

CreateDatabase provisions a new D1 database via Cloudflare API.

func DailyRowsRead

func DailyRowsRead() int64

DailyRowsRead returns the accumulated row-read estimate for the current UTC day.

func DeleteDatabase

func DeleteDatabase(ctx context.Context, accountID, apiToken, databaseID string) error

DeleteDatabase removes a D1 database via Cloudflare API.

func MarshalScopedCredentials

func MarshalScopedCredentials(databaseID string) (string, error)

MarshalScopedCredentials returns JSON for scoped database credentials.

func ParseScopedCredentialsJSON

func ParseScopedCredentialsJSON(raw string) (*models.DriverCredentials, error)

ParseScopedCredentialsJSON unmarshals scoped database credentials.

func ResetBillingForTest

func ResetBillingForTest()

ResetBillingForTest resets the in-process daily counter (tests only).

func SetGlobalBoundExec

func SetGlobalBoundExec(exec BoundExecFunc)

SetGlobalBoundExec configures bound D1 execution (Worker system DB).

func SetGlobalCredentials

func SetGlobalCredentials(accountID, apiToken string)

SetGlobalCredentials configures REST API access for dynamic D1 databases.

func SetGlobalSystemD1DatabaseID

func SetGlobalSystemD1DatabaseID(databaseID string)

SetGlobalSystemD1DatabaseID records the SYSTEM_D1 database UUID (or "bound" on Workers). Project/tenant drivers with a different database ID use REST instead of the bound binding.

Types

type AccessMode

type AccessMode int

AccessMode selects how SQL reaches D1.

const (
	// AccessBound uses a statically bound D1 database (system/starter).
	AccessBound AccessMode = iota
	// AccessREST uses Cloudflare D1 HTTP API (dynamic project/tenant DBs).
	AccessREST
)

type BoundExecFunc

type BoundExecFunc func(ctx context.Context, sql string, params []any) (Result, error)

BoundExecFunc runs SQL via a Worker-bound D1 database.

type Config

type Config struct {
	Mode       AccessMode
	DatabaseID string // D1 UUID
	AccountID  string
	APIToken   string
	// BoundExec, when set, runs SQL via Worker D1 binding (wasm entry injects this).
	BoundExec BoundExecFunc
}

Config identifies which D1 database to query.

func ConfigFromCredentials

func ConfigFromCredentials(cred *models.DriverCredentials, accountID, apiToken string) (Config, error)

ConfigFromCredentials builds a REST-mode DB config from DriverCredentials.

func DefaultConfig

func DefaultConfig(databaseID string, boundOverride BoundExecFunc) Config

DefaultConfig builds a Config using global credentials and optional bound exec override. Bound mode is used only when the target database is the system D1 (same UUID or "bound"). All other database UUIDs use REST so project/tenant D1s are queried correctly.

type CredentialsJSON

type CredentialsJSON struct {
	Engine     string `json:"engine"`
	DatabaseID string `json:"database_id"`
}

CredentialsJSON is persisted with scoped D1 database credentials.

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB is a lightweight D1 SQL executor (no database/sql, no bun).

func NewDB

func NewDB(cfg Config) (*DB, error)

NewDB opens a D1 executor for the given config.

func (*DB) DatabaseID

func (d *DB) DatabaseID() string

DatabaseID returns the D1 database UUID.

func (*DB) Exec

func (d *DB) Exec(ctx context.Context, sql string, params ...any) (Result, error)

Exec runs a write statement.

func (*DB) Query

func (d *DB) Query(ctx context.Context, sql string, params ...any) ([]map[string]any, error)

Query runs a read statement.

func (*DB) QueryRow

func (d *DB) QueryRow(ctx context.Context, sql string, params ...any) (map[string]any, error)

QueryRow runs a read expecting zero or one row.

type Result

type Result struct {
	Rows         []map[string]any
	RowsAffected int64
	LastInsertID int64
	RowsRead     int64
}

Result is a single D1 statement outcome.

Jump to

Keyboard shortcuts

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