Skip to main content

A fan-made Python client for interacting with NunDB, a NoSQL database that supports real-time data synchronization and flexible data manipulation. This library allows you to connect to a NunDB instance, create databases, set and get values, watch data in real-time, and manage users and permissions.

Project description

NunDB Client (Python)

NunDB Client for interacting with the NunDB WebSocket server. This Python client allows you to authenticate, manage databases, manipulate keys, and set up watchers for key changes in the NunDB server.

Features

  • Connect to WebSocket: Connect and authenticate with the NunDB WebSocket server.
  • Database Management: Create and manage databases.
  • Key Management: Set, get, increment, and remove keys.
  • Watchers: Watch keys for changes and execute callbacks when changes occur.
  • Asynchronous: Fully asynchronous, based on asyncio and websockets for non-blocking operations.

Installation

To install the NunDB Python client, you can use pip:

pip install nun-db-py

Usage

Basic Usage Example

import asyncio
from nun_db_client import NunDB

async def main():
    # Connect to the NunDB server
    url = "ws://localhost:9000"
    username = "your_username"
    password = "your_password"
    
    nun_db, _ = await NunDB.create(url, username, password)

    # Create a new database
    db_name = "test_db"
    db_token = "your_db_token"
    await nun_db.create_db(db_name, db_token)

    # Set a key-value pair
    await nun_db.set("some_key", "some_value")

    # Get the value of a key
    value = await nun_db.get("some_key")
    print(value)

    # Increment a key's value
    await nun_db.increment("some_key", 10)

    # Add a watcher for a key
    await nun_db.add_watch("some_key", lambda data: print(f"Key changed: {data}"))

    # Remove the watcher
    await nun_db.remove_watcher("some_key")

    # Close the connection
    await nun_db.close()

# Run the example
asyncio.run(main())

Available Methods

  • connect(url: str, name: str, pwd: str): Connect to the NunDB WebSocket server and authenticate.
  • create_db(db_name: str, db_token: str): Create a new database.
  • set(name: str, value: str): Set a key to a specific value.
  • get(name: str): Get the value of a key.
  • increment(key: str, value: int): Increment the value of a key.
  • remove(key: str): Remove a key from the database.
  • add_watch(name: str, cb: Callable): Add a watcher for a key.
  • remove_watcher(name: str): Remove the watcher for a key.
  • close(): Close the WebSocket connection.

Watchers

You can set up watchers to listen for changes to a key. The callback function will be executed whenever the key is modified.

Example:

await nun_db.add_watch("some_key", lambda data: print(f"Key {data} changed"))

Advanced Usage

  • get_all_databases(): Retrieve all databases.
  • keys_starting_with(prefix: str): Retrieve all keys starting with a given prefix.
  • keys_ending_with(suffix: str): Retrieve all keys ending with a given suffix.
  • keys_contains(supposed_text: str): Retrieve all keys that contain a specific substring.

Error Handling

The client raises exceptions if commands fail. You should handle errors accordingly by wrapping them in try-except blocks.

Logging

To enable logging for commands and responses:

nun_db.show_logs(True)

Closing the Connection

To close the WebSocket connection gracefully, use:

await nun_db.close()

Dependencies

asyncio
websockets
re

License

MIT License. See LICENSE for more details.

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

nun_db_py-0.1.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

nun_db_py-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file nun_db_py-0.1.0.tar.gz.

File metadata

  • Download URL: nun_db_py-0.1.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.8 Linux/6.8.0-49-generic

File hashes

Hashes for nun_db_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 44625a66113aecd8131d9c7ec007273ece13a1262e50950b18e00230b365c172
MD5 225f1c21388ef93b1fcf2011231da185
BLAKE2b-256 49097132860bda560c94e154715e59d66ab187ece0979dda21af24239de707db

See more details on using hashes here.

File details

Details for the file nun_db_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nun_db_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.8 Linux/6.8.0-49-generic

File hashes

Hashes for nun_db_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 877b91c3e62c755eb7f0ef3690a8778156ce68a327c02246d2674c5f074659dd
MD5 0aac36c325f0ccca3926c19da1d958bf
BLAKE2b-256 25e210a5d7d79d78b47d43c777234ca0bc38dd22b85bb4a92d905d43395399f2

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