Skip to main content

Cloudflare D1 client library for kiarina namespace

Project description

kiarina-lib-cloudflare-d1

PyPI Python License

English | 日本語

[!NOTE] Provides synchronous and asynchronous clients for sending SQL queries to the Cloudflare D1 REST API.

Dependencies

Package Version License
HTTPX >=0.28.1 BSD-3-Clause
kiarina-lib-cloudflare >=1.5.0 MIT
pydantic-settings >=2.10.1 MIT
pydantic-settings-manager >=3.2.0 MIT

Installation

pip install kiarina-lib-cloudflare-d1

Features

  • Database Configuration Manage D1 database IDs separately from Cloudflare credentials.
  • Parameterized Queries Send SQL and positional parameters to the D1 REST API.
  • Named Configurations Select among multiple databases and Cloudflare accounts by settings key.
  • Synchronous and Asynchronous APIs Use the same operation through synchronous and asynchronous clients.

Configuring a Database

The D1 and Cloudflare authentication managers can each hold multiple named settings.

kiarina.lib.cloudflare_d1:
  default: production
  configs:
    production:
      database_id: "your-database-id"

kiarina.lib.cloudflare:
  default: production
  configs:
    production:
      account_id: "your-account-id"
      api_token: "your-api-token"

Load the settings when the application starts.

import yaml
from pydantic_settings_manager import load_user_configs

with open("config.yaml", encoding="utf-8") as file:
    load_user_configs(yaml.safe_load(file) or {})

A single default configuration can also be provided with environment variables.

export KIARINA_LIB_CLOUDFLARE_D1_DATABASE_ID="your-database-id"
export KIARINA_LIB_CLOUDFLARE_ACCOUNT_ID="your-account-id"
export KIARINA_LIB_CLOUDFLARE_API_TOKEN="your-api-token"

Using the Synchronous Client

query returns the D1 JSON response regardless of HTTP status. Check success or call raise_for_status.

from kiarina.lib.cloudflare_d1 import create_d1_client

client = create_d1_client()
result = client.query(
    "SELECT * FROM users WHERE id = ?",
    [1],
)
result.raise_for_status()

for row in result.first.rows:
    print(row)

Using Named Configurations

D1 settings and authentication settings can be selected independently.

from kiarina.lib.cloudflare_d1 import create_d1_client

client = create_d1_client(
    settings_key="production",
    auth_settings_key="production",
)

Using the Asynchronous Client

Await query when using the asynchronous client.

from kiarina.lib.cloudflare_d1.asyncio import create_d1_client

client = create_d1_client()
result = await client.query(
    "SELECT * FROM users WHERE id = ?",
    [1],
)
result.raise_for_status()

API Reference

kiarina.lib.cloudflare_d1

from kiarina.lib.cloudflare_d1 import (
    D1Client,
    D1Settings,
    create_d1_client,
    settings_manager,
)

create_d1_client

def create_d1_client(
    settings_key: str | None = None,
    *,
    auth_settings_key: str | None = None,
) -> D1Client: ...

Creates a synchronous client from D1 settings and Cloudflare authentication settings.

Parameters

  • settings_key (str | None): D1 settings key. None selects the default settings.
  • auth_settings_key (str | None): Cloudflare authentication settings key. None selects the default settings.

D1Client

class D1Client:
    def __init__(
        self,
        settings: D1Settings,
        *,
        auth_settings: CloudflareSettings,
    ) -> None: ...

    def query(
        self,
        sql: str,
        params: list[Any] | None = None,
    ) -> Result: ...

The query result provides these attributes and methods:

  • success (bool): Whether the overall API request succeeded.
  • result (list[QueryResult]): Results for the SQL statements.
  • errors (list[ResponseInfo]): Errors returned by the API.
  • messages (list[ResponseInfo]): Messages returned by the API.
  • first (QueryResult): The first result. Raises ValueError when no result is available.
  • raise_for_status() -> None: Raises RuntimeError when success is False.

Each QueryResult has success, meta, and results attributes. rows is an alias for results.

D1Settings

class D1Settings(BaseSettings):
    database_id: str

Settings for a Cloudflare D1 database.

Fields

  • database_id (str): Cloudflare D1 database ID.

settings_manager

settings_manager: SettingsManager[D1Settings]

Manages multiple named D1 settings.

kiarina.lib.cloudflare_d1.asyncio

from kiarina.lib.cloudflare_d1.asyncio import (
    D1Client,
    D1Settings,
    create_d1_client,
    settings_manager,
)

create_d1_client

def create_d1_client(
    settings_key: str | None = None,
    *,
    auth_settings_key: str | None = None,
) -> D1Client: ...

Creates an asynchronous client. Its parameters are the same as the synchronous API.

D1Client

class D1Client:
    def __init__(
        self,
        settings: D1Settings,
        *,
        auth_settings: CloudflareSettings,
    ) -> None: ...

    async def query(
        self,
        sql: str,
        params: list[Any] | None = None,
    ) -> Result: ...

Its parameters and return values are the same as the synchronous client.

D1Settings and settings_manager are the same objects exported by the synchronous API.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kiarina_lib_cloudflare_d1-2.3.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kiarina_lib_cloudflare_d1-2.3.1-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file kiarina_lib_cloudflare_d1-2.3.1.tar.gz.

File metadata

File hashes

Hashes for kiarina_lib_cloudflare_d1-2.3.1.tar.gz
Algorithm Hash digest
SHA256 4c2a2812057589551f93fc9db261ccfac29932f8d8857d53770cd866e8347112
MD5 a120bcb9f9fea4859892fd2ce0c8167a
BLAKE2b-256 0442f3afbaf19511188567306d6da9bd0bbe42d98a87b639f2000c2c918d5d78

See more details on using hashes here.

Provenance

The following attestation bundles were made for kiarina_lib_cloudflare_d1-2.3.1.tar.gz:

Publisher: release-pypi.yml on kiarina/kiarina-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kiarina_lib_cloudflare_d1-2.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for kiarina_lib_cloudflare_d1-2.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a2395a5ae45ee42cc5dc1df51c0cc1f89776a385df5f1ce3e0c399a436b128b2
MD5 f9bb25af9081b4c4a4278b0a819e76a9
BLAKE2b-256 4af8248acd47d53f82cee4b0e453c077cdfb590a1986c4b444a9ad9e06727f19

See more details on using hashes here.

Provenance

The following attestation bundles were made for kiarina_lib_cloudflare_d1-2.3.1-py3-none-any.whl:

Publisher: release-pypi.yml on kiarina/kiarina-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page