Documentation
¶
Overview ¶
Package cfd1 executes SQL against Cloudflare D1 (bound binding or REST API).
Index ¶
- Constants
- Variables
- func CreateDatabase(ctx context.Context, accountID, apiToken, name string) (databaseID string, err error)
- func DailyRowsRead() int64
- func DeleteDatabase(ctx context.Context, accountID, apiToken, databaseID string) error
- func MarshalScopedCredentials(databaseID string) (string, error)
- func ParseScopedCredentialsJSON(raw string) (*models.DriverCredentials, error)
- func ResetBillingForTest()
- func SetGlobalBoundExec(exec BoundExecFunc)
- func SetGlobalCredentials(accountID, apiToken string)
- func SetGlobalSystemD1DatabaseID(databaseID string)
- type AccessMode
- type BoundExecFunc
- type Config
- type CredentialsJSON
- type DB
- func (d *DB) DatabaseID() string
- func (d *DB) Exec(ctx context.Context, sql string, params ...any) (Result, error)
- func (d *DB) Query(ctx context.Context, sql string, params ...any) ([]map[string]any, error)
- func (d *DB) QueryRow(ctx context.Context, sql string, params ...any) (map[string]any, error)
- type Result
Constants ¶
const (
// EngineName is the Apito driver engine identifier for Cloudflare D1.
EngineName = "cloudflared1"
)
Variables ¶
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") )
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 ¶
DeleteDatabase removes a D1 database via Cloudflare API.
func MarshalScopedCredentials ¶
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 ¶
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.