Skip to main content

Spring Boot-style CDI auto-configuration for pynosqlc (NoSQL document store)

Project description

alt-python-boot-pynosqlc

Language Python License: MIT

Spring Boot-style CDI auto-configuration for document stores via pynosqlc. Provides ManagedNosqlClient, ConfiguredClientDataSource, and a CDI auto-configuration factory that wires a connected NoSQL client into a Boot application from a single config property.

Part of the alt-python/boot monorepo.

Quick Start

uv add alt-python-boot-pynosqlc   # or: pip install alt-python-boot-pynosqlc
# invoke.py
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

import pynosqlc.memory  # registers the memory driver
from boot import Boot
from cdi import Context, Singleton
from boot_pynosqlc import pynosqlc_auto_configuration

from services import NoteRepository, Application

Boot.boot({
    'contexts': [
        Context(pynosqlc_auto_configuration()),
        Context([Singleton(NoteRepository), Singleton(Application)]),
    ]
})
// config/application.json
{
  "boot": {
    "nosql": {
      "url": "pynosqlc:memory:"
    }
  }
}

Boot reads config/application.json, connects to the NoSQL backend, and injects a ready ManagedNosqlClient into the application.

What's Included

Class / Function Description
ConfiguredClientDataSource CDI bean that reads boot.nosql.* config and creates a ClientDataSource
ManagedNosqlClient CDI bean wrapping the NoSQL client; provides get_collection()
NoSqlClientBuilder Fluent builder for secondary NoSQL clients
pynosqlc_auto_configuration() Returns 2 CDI Singletons ready for Context()
pynosqlc_starter() Alias for pynosqlc_auto_configuration()
pynosqlc_boot() One-call Boot.boot() entry point
DEFAULT_NOSQL_PREFIX 'boot.nosql'

Configuration

All properties are under boot.nosql by default (override with a custom prefix).

Property Type Default Description
boot.nosql.url string pynosqlc URL, e.g. pynosqlc:memory:
boot.nosql.username string Username (optional)
boot.nosql.password string Password (optional)

If boot.nosql.url is absent, the ConfiguredClientDataSource bean is still registered but its _delegate is None. Calling get_collection() will raise RuntimeError('NoSQL client not ready'). This lets you deploy with an optional NoSQL client without breaking CDI wiring.

Install

uv add alt-python-boot-pynosqlc   # or: pip install alt-python-boot-pynosqlc

Requires Python 3.12+, alt-python-boot-lib, alt-python-pynosqlc-core, and alt-python-pynosqlc-memory. All dependencies are available on PyPI.

Working with Collections

ManagedNosqlClient.get_collection(name) is synchronous. All collection operations are async — use asyncio.run() to bridge from synchronous CDI lifecycle methods:

import asyncio

class NoteRepository:
    def __init__(self):
        self.nosql_client = None  # CDI-autowired

    async def find_all(self):
        col = self.nosql_client.get_collection('notes')  # sync
        cursor = await col.find({'type': 'and', 'conditions': []})
        return cursor.get_documents()  # synchronous — do NOT await

    async def store(self, key, note):
        col = self.nosql_client.get_collection('notes')
        await col.store(key, note)


class Application:
    def __init__(self):
        self.note_repository = None  # CDI-autowired

    def run(self):
        # CDI calls run() synchronously — bridge to async here
        asyncio.run(self._run_async())

    async def _run_async(self):
        await self.note_repository.store('k1', {'title': 'First', 'done': False})
        notes = await self.note_repository.find_all()
        for note in notes:
            print(note['title'])

Collection API

Method Signature Description
store async (key, doc) → None Insert or replace a document by key
get async (key) → dict | None Retrieve a document; None if absent
insert async (doc) → str Insert and return an auto-assigned ID
update async (key, patch) → None Merge patch into an existing document
delete async (key) → None Remove a document
find async (filter) → Cursor Query with a filter; returns a Cursor

Cursor.get_documents() is synchronous — it returns a plain list and must not be awaited:

cursor = await col.find(filter)
docs = cursor.get_documents()  # not: await cursor.get_documents()

Secondary NoSQL Clients

NoSqlClientBuilder creates a second set of CDI beans with a custom config prefix and custom bean names:

from boot_pynosqlc import NoSqlClientBuilder, pynosqlc_auto_configuration
from cdi import Context

primary = pynosqlc_auto_configuration()  # boot.nosql.url

secondary = (
    NoSqlClientBuilder.create()
    .prefix('myapp.reporting')
    .bean_names({
        'nosql_client_data_source': 'reporting_ds',
        'nosql_client': 'reporting_client',
    })
    .build()
)

ctx = Context([*primary, *secondary, ...])

Config for the secondary client lives under myapp.reporting.*:

{
  "myapp": {
    "reporting": {
      "url": "pynosqlc:memory:"
    }
  }
}

Using with Boot

pynosqlc_auto_configuration()

Returns a flat list of 2 Singleton beans. Concatenate with your application beans and pass to Context():

from boot import Boot
from cdi import Context, Singleton
from boot_pynosqlc import pynosqlc_auto_configuration

Boot.boot({
    'contexts': [
        Context(pynosqlc_auto_configuration()),
        Context([Singleton(MyService), Singleton(MyApp)]),
    ]
})

pynosqlc_boot()

One-call entry point for applications that need only a single NoSQL client:

from boot_pynosqlc import pynosqlc_boot
from cdi import Context, Singleton

pynosqlc_boot({
    'contexts': [Context([Singleton(MyService), Singleton(MyApp)])],
})

CDI Lifecycle Notes

ManagedNosqlClient uses synchronous CDI lifecycle methods (init(), destroy()) with asyncio.run() as a bridge to the async pynosqlc client:

  • init() calls asyncio.run(self._connect()) to establish the client connection.
  • destroy() calls asyncio.run(self._client.close()) for clean shutdown.

CDI's SIGINT handler calls destroy() synchronously. Do not override destroy() as async def — the coroutine will never be awaited and Python will emit RuntimeWarning: coroutine was never awaited.

Running Tests

uv run pytest packages/boot-pynosqlc -v

License

MIT

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

alt_python_boot_pynosqlc-1.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

alt_python_boot_pynosqlc-1.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file alt_python_boot_pynosqlc-1.1.1.tar.gz.

File metadata

  • Download URL: alt_python_boot_pynosqlc-1.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for alt_python_boot_pynosqlc-1.1.1.tar.gz
Algorithm Hash digest
SHA256 a2e536d84ea57df15587e6cb4c3fdbd02a1e5331b07a3d8b145bdd7058ef5853
MD5 13b73905a98f8b9153993f54635d1b61
BLAKE2b-256 13d8e47bcd363bf1648da9cb690bcb022205e3dd6d9fca53a610d02107e4eb27

See more details on using hashes here.

File details

Details for the file alt_python_boot_pynosqlc-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: alt_python_boot_pynosqlc-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for alt_python_boot_pynosqlc-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4137547f7f70024db43a46d1edc119c81697c62690c30076380fbe877c933231
MD5 e7f3a55ab22c0006d4d72c1933e312b9
BLAKE2b-256 74a528e6123b4458947231aa313a98ae37858d39c57d8cae84e9e25d4ba4924d

See more details on using hashes here.

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