Skip to main content

prefect-couchbase

A Prefect integration for connecting Prefect flows and tasks to Couchbase through the official Couchbase Python SDK.

What you can use it for

  • Store Couchbase connection details as a Prefect block and load them safely from flows.
  • Read, write, and delete documents from Couchbase buckets/scopes/collections in Prefect tasks.
  • Run SQL++ queries from Prefect flows while reusing the same Couchbase SDK connection lifecycle.

This connector does not replace the Couchbase Python SDK. It provides Prefect-native credentials and a small convenience wrapper around the SDK so workflows can manage credentials, lifecycle, and common operations consistently.

End-user quickstart

Install the package:

pip install prefect-couchbase

Create and save a credentials block:

from prefect_couchbase import CouchbaseCredentials

credentials = CouchbaseCredentials(
    connection_string="couchbases://cb.example.cloud.couchbase.com",
    username="analytics_user",
    password="super-secret-password",
)
credentials.save("prod-couchbase", overwrite=True)

Use the block from a flow:

from prefect import flow, task
from prefect_couchbase import CouchbaseCredentials


@task
def upsert_hotel(document_id: str, name: str) -> dict:
    credentials = CouchbaseCredentials.load("prod-couchbase")

    with credentials.get_connector(
        bucket="travel-sample", scope="_default", collection="_default"
    ) as couchbase:
        couchbase.upsert(document_id, {"type": "hotel", "name": name})
        return couchbase.get(document_id)


@flow
def demo() -> dict:
    return upsert_hotel("prefect::hotel::1", "Prefect Grand Hotel")


if __name__ == "__main__":
    print(demo())

Expected output:

{'type': 'hotel', 'name': 'Prefect Grand Hotel'}

Provision Couchbase

Recommended: Couchbase Capella

Use Capella when Prefect Cloud workers or any hosted execution infrastructure need to reach Couchbase over the public internet.

  1. Create a Capella cluster.
  2. Create or choose a bucket, scope, and collection.
  3. Add a database user with access to the bucket.
  4. Add the worker's outbound IP address to Capella's allowed IP list.
  5. Use the Capella SDK connection string, which starts with couchbases://.
  6. Configure the Prefect block with the connection string, username, and password.

Local or self-managed Couchbase

Use local/self-managed Couchbase when your Prefect worker runs on the same machine or private network.

  1. Start Couchbase Server locally.
  2. Create a bucket such as travel-sample or prefect-demo.
  3. Create a user with bucket read/write permissions.
  4. Use a connection string such as couchbase://localhost.

For a quick local evaluation, the Couchbase Python SDK examples and Docker documentation are the best reference for the currently supported server image and initialization flow.

Configuration reference

CouchbaseCredentials fields:

Field Description
connection_string Couchbase SDK connection string, such as couchbase://localhost or couchbases://cb.example.cloud.couchbase.com.
username / password Password authentication credentials. Store these as Prefect secrets through the block.
cert_path Optional trusted CA certificate path for password authentication; client certificate path for certificate authentication.
key_path / trust_store_path Required with cert_path when using certificate authentication.
profile Optional SDK config profile, for example wan_development.
options Extra keyword arguments passed to couchbase.options.ClusterOptions.

CouchbaseConnector methods:

  • get_bucket(bucket=None)
  • get_scope(bucket=None, scope=None)
  • get_collection(bucket=None, scope=None, collection=None)
  • upsert(key, value, **kwargs)
  • get(key, content_as=dict, **kwargs)
  • remove(key, **kwargs)
  • query(statement, *args, **kwargs)
  • ping(**kwargs)
  • close()

Tutorial: runnable local flow

  1. Install dependencies:

    pip install prefect-couchbase
    
  2. Export connection settings:

    export COUCHBASE_CONNECTION_STRING="couchbase://localhost"
    export COUCHBASE_USERNAME="Administrator"
    export COUCHBASE_PASSWORD="password"
    export COUCHBASE_BUCKET="travel-sample"
    
  3. Save a credentials block once from the current environment:

    python examples/save_credentials_block.py
    
  4. Run the example flow, which loads the saved block:

    python examples/couchbase_flow.py
    
  5. Validate by checking that the flow prints:

    Stored document: {'type': 'hotel', 'name': 'Prefect Grand Hotel'}
    

Troubleshooting

  • Authentication failed: verify username/password and bucket permissions.
  • Unambiguous timeout: verify the worker can reach the cluster and that Capella allowed IPs include the worker.
  • bucket must be provided: pass bucket="..." to get_connector() or to the individual operation.
  • TLS errors with Capella: use a couchbases:// connection string and ensure the cluster endpoint is correct.

Developer documentation

Local development

git clone https://github.com/Couchbase-Ecosystem/prefect-couchbase.git
cd prefect-couchbase
uv sync --extra dev

Run tests and coverage:

uv run pytest

Run linting:

uv run ruff check .
uv run ruff format --check .

Build the package:

uv run python -m build

Run the example smoke check without a live Couchbase cluster:

uv run python -m py_compile examples/save_credentials_block.py examples/couchbase_flow.py

Run the examples against a live cluster by setting the environment variables from the tutorial, saving the block with uv run python examples/save_credentials_block.py, and then executing uv run python examples/couchbase_flow.py.

Release and publishing path

  1. Ensure uv run pytest, uv run ruff check ., and uv run python -m build pass.
  2. Update the version tag according to the repository release policy.
  3. Build distributions with uv run python -m build.
  4. Publish to PyPI or the chosen private package index with twine upload dist/* after configuring maintainer credentials.
  5. Register the collection with Prefect by installing the package in the worker environment. The package exposes the prefect.collections entry point prefect_couchbase = prefect_couchbase.

Research and design notes

This implementation follows Prefect's collection pattern from official integrations such as prefect-redis, prefect-snowflake, and prefect-sqlalchemy: typed Prefect blocks hold credentials and user code imports a small package-specific public API. It also mirrors the Couchbase authentication and collection access shape used by the Couchbase Airflow provider: password authentication uses PasswordAuthenticator, certificate authentication uses CertificateAuthenticator, and helper methods resolve bucket/scope/collection objects.

Known limitations

  • The unit test suite uses SDK mocks so it can run without a live Couchbase service. Live connectivity should be validated in the environment where the Prefect worker runs.
  • The connector intentionally exposes a minimal wrapper. Advanced SDK features should be accessed from connector.cluster or native SDK objects returned by get_bucket, get_scope, and get_collection.

Download files

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

Source Distribution

prefect_couchbase-1.0.2.tar.gz (203.5 kB view details)

Uploaded Source

Built Distribution

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

prefect_couchbase-1.0.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file prefect_couchbase-1.0.2.tar.gz.

File metadata

  • Download URL: prefect_couchbase-1.0.2.tar.gz
  • Upload date:
  • Size: 203.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for prefect_couchbase-1.0.2.tar.gz
Algorithm Hash digest
SHA256 20c613f8e60cb6cd02ddd87223d09daaba356c164aa593af7cc29a19aefb0171
MD5 5a472aabafbaf182b8c74a9ced2c5da9
BLAKE2b-256 8054ed26c37ee5b7cd118dd53a1c9b41393475ce163ba4d6effbda26fd1d5220

See more details on using hashes here.

Provenance

The following attestation bundles were made for prefect_couchbase-1.0.2.tar.gz:

Publisher: release.yml on Couchbase-Ecosystem/prefect-couchbase

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

File details

Details for the file prefect_couchbase-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for prefect_couchbase-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 53e49d7b46be6c42bbc845d677181d69f03b16c3a3b3331dcac18bb93b1d4646
MD5 5926c21db7cf88f50da82e22ac8a886e
BLAKE2b-256 f6338e49a985639459a8167fea620fe7ede3e732652fa92bd9715168b7f5f2d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for prefect_couchbase-1.0.2-py3-none-any.whl:

Publisher: release.yml on Couchbase-Ecosystem/prefect-couchbase

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

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 files

1.0.1

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page