Skip to main content

FastStream SQL broker

Project description

Downloads Package version Documentation

faststream-sqlbroker

A SQL-backed broker for FastStream. Documentation.

Transactional outbox

Implementing the transactional outbox pattern becomes as simple as the following.

Publish messages transactionally with your other database operations.

from sqlalchemy.ext.asyncio import create_async_engine

from faststream import FastStream
from faststream.kafka import KafkaBroker, KafkaPublishMessage

from faststream_sqlbroker import SqlBroker, SqlBrokerMessage

engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/mydb")
broker_sqlbroker = SqlBroker(engine=engine)
broker_kafka = KafkaBroker("127.0.0.1:9092")
app = FastStream(broker_sqlbroker, on_startup=[broker_kafka.connect])
publisher_sqlbroker = broker_sqlbroker.publisher()


@app.after_startup # just an example
async def publish_examples():
    async with engine.begin() as connection:
        # ... your other database operations using `connection` ...
        await publisher_sqlbroker.publish(
            {"message": "Hello, SqlBroker!"},
            queue="sqlbroker_queue",
            headers={
                "x-test-header": "outbox",
                "x-kafka-key-source": "outbox-key",
            },
            connection=connection,
        )

And relay the messages from the database to another broker.

publisher_kafka = broker_kafka.publisher("kafka_topic")


@publisher_kafka
@broker_sqlbroker.subscriber(
    queues=["sqlbroker_queue"],
    max_fetch_interval=1,
    min_fetch_interval=0,
    fetch_batch_size=10,
    flush_interval=3,
)
async def handle_msg(
    msg_body: dict,
    msg: SqlBrokerMessage,
) -> KafkaPublishMessage:
    return KafkaPublishMessage(
        msg_body,
        headers={
            "x-test-header": msg.headers["x-test-header"],
        },
        key=msg.headers["x-kafka-key-source"].encode(),
    )

Origins

Originated as a PR to FastStream.

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

faststream_sqlbroker-0.1.0a9.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

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

faststream_sqlbroker-0.1.0a9-py3-none-any.whl (41.4 kB view details)

Uploaded Python 3

File details

Details for the file faststream_sqlbroker-0.1.0a9.tar.gz.

File metadata

  • Download URL: faststream_sqlbroker-0.1.0a9.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for faststream_sqlbroker-0.1.0a9.tar.gz
Algorithm Hash digest
SHA256 3c23abfdf3c5b1c59fb9e58173412710acd8dcc7136bcbd059272600eb3f47ff
MD5 243dffb39011a4026f99c4bca6815bb0
BLAKE2b-256 a6b1d4234e3f6f7136938cf8f17d6788fbf2d374ae86956d305c35195deee2e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for faststream_sqlbroker-0.1.0a9.tar.gz:

Publisher: release_pypi.yaml on faststream-community/faststream-sqlbroker

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

File details

Details for the file faststream_sqlbroker-0.1.0a9-py3-none-any.whl.

File metadata

File hashes

Hashes for faststream_sqlbroker-0.1.0a9-py3-none-any.whl
Algorithm Hash digest
SHA256 602980e66474e3b850a215680cf10d27cc8dd7863754e4298be4e1a8abd90aaa
MD5 37176ea2e80d247d638b3d5effe029cb
BLAKE2b-256 884add7e38a16ccf045a7653a50cbcc0ad6291d3fe086cc082a59e4508869184

See more details on using hashes here.

Provenance

The following attestation bundles were made for faststream_sqlbroker-0.1.0a9-py3-none-any.whl:

Publisher: release_pypi.yaml on faststream-community/faststream-sqlbroker

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