Skip to main content

Official Python SDK for RustyBase Database

Project description

RustyBase Python SDK 🐍

The official Python client for RustyBase, a high-performance, embedded-friendly database system with a MongoDB-like API.

PyPI version License: MIT


📚 Table of Contents


📦 Installation

pip install rustybase

🚀 Quick Start

from rustybase import RustyBaseClient

# 1. Connect
client = RustyBaseClient(connection_string="rustybase://admin:password@localhost:3000/mydb")

# 2. Authenticate
client.login()

# 3. Get Collection
users = client.db("mydb").collection("users")

# 4. Insert Data
users.insert_one({"name": "Alice", "role": "engineer", "age": 28})

# 5. Find Data
alice = users.find_one({"name": "Alice"})
print(f"Found user: {alice}")

🛠 Client Configuration

You can initialize the client using a connection string or individual parameters.

Connection String (Recommended)

client = RustyBaseClient(
    connection_string="rustybase://username:password@host:port/database?authSource=admin"
)

Individual Parameters

client = RustyBaseClient(
    host="127.0.0.1",
    port=3000,
    username="admin",
    password="password",
    database="default",
    timeout=30.0,
    use_signing=False  # Enable HMAC request signing for extra security
)
Parameter Type Default Description
connection_string str None URI formatted connection string.
use_signing bool False If True, adds X-RB-Signature headers to requests.
timeout float 30.0 Request timeout in seconds.
request_callback Callable None Optional callback (fn(method, url, payload, response)) for logging.

🔐 Authentication

RustyBase uses JWT authentication. You must login before performing operations. The SDK handles automatic token refreshing for you.

# Authenticate and obtain tokens
client.login()

# ... perform operations ...

# Tokens are auto-refreshed on 401 errors.

💾 CRUD Operations

Insert

Insert One

doc = {"name": "Bob", "tags": ["developer", "python"]}
result = users.insert_one(doc)
# result: {"inserted_count": 1, "inserted_ids": [...]}

Insert Many

docs = [
    {"name": "Charlie", "age": 25},
    {"name": "Dana", "age": 30}
]
users.insert_many(docs)

Find & Query

The find method is powerful and supports filtering, projection, sorting, implementation, and pagination.

results = users.find(
    filter={"age": {"$gt": 25}},   # Query criteria
    projection={"name": 1},        # Fields to include (1) or exclude (0)
    sort={"age": -1},              # 1 for Ascending, -1 for Descending
    limit=10,                      # Max documents to return
    skip=0,                        # Documents to skip
    auto_indexing=True             # Allow ad-hoc indexing for performance
)

Find One Helper to return a single document or None.

user = users.find_one({"_id": "some_id"})

Update

Updates a single document matching the filter.

users.update_one(
    filter={"name": "Alice"},
    update={"$set": {"active": True}, "$inc": {"age": 1}},
    upsert=True  # Create if it doesn't exist
)

Delete

Removes a document matching the filter.

users.delete_one({"name": "Charlie"})

🔍 Filtering & Operators

RustyBase supports a rich set of MongoDB-compatible query operators.

Comparison Operators

Operator Description Example
$eq Matches values that are equal to a specified value. {"age": {"$eq": 20}} or {"age": 20}
$gt Matches values that are greater than a specified value. {"age": {"$gt": 20}}
$gte Matches values that are greater than or equal to a specified value. {"age": {"$gte": 20}}
$lt Matches values that are less than a specified value. {"age": {"$lt": 20}}
$lte Matches values that are less than or equal to a specified value. {"age": {"$lte": 20}}
$ne Matches all values that are not equal to a specified value. {"age": {"$ne": 20}}
$in Matches any of the values specified in an array. {"status": {"$in": ["active", "pending"]}}
$nin Matches none of the values specified in an array. {"status": {"$nin": ["banned", "deleted"]}}

Logical Operators

Operator Description Example
$and Joins query clauses with a logical AND. {"$and": [{"price": {"$ne": 1.99}}, {"price": {"$exists": true}}]}
$or Joins query clauses with a logical OR. {"$or": [{"qty": {"$lt": 20}}, {"price": 10}]}
$not Inverts the effect of a query expression. {"price": {"$not": {"$gt": 1.99}}}
$nor Joins query clauses with a logical NOR. {"$nor": [{"price": 1.99}, {"sale": true}]}

Element Operators

Operator Description Example
$exists Matches documents that have the specified field. {"qty": {"$exists": true}}
$type Selects documents if a field is of the specified type. {"price": {"$type": "number"}}

📊 Aggregation Framework

Perform complex data analysis using aggregation pipelines.

pipeline = [
    # Stage 1: Filter documents
    {"$match": {"status": "active"}},

    # Stage 2: Group by a field and calculate metrics
    {"$group": {
        "_id": "$department",
        "total_salary": {"$sum": "$salary"},
        "avg_age": {"$avg": "$age"}
    }},

    # Stage 3: Sort the results
    {"$sort": {"total_salary": -1}}
]

results = users.aggregate(pipeline)

Supported Aggregation Stages

  • $match: Filters the documents.
  • $group: Groups input documents by the specified _id.
  • $sort: Sorts the documents.
  • $project: Reshapes each document in the stream (include/exclude fields).
  • $limit: Limits the number of documents.
  • $skip: Skips the specified number of documents.
  • $unwind: Deconstructs an array field from the input documents.
  • $count: Counts the number of documents.

Accumulators (for $group)

  • $sum, $avg, $min, $max, $first, $last, $push, $addToSet.

⚡ Async Support

The SDK includes a full Asynchronous Client built on httpx.AsyncClient. It mirrors the synchronous API perfectly, but all network methods are awaitable.

import asyncio
from rustybase import AsyncRustyBaseClient

async def main():
    client = AsyncRustyBaseClient(connection_string="...")
    await client.login()

    db = client.db("test")
    users = db.collection("users")

    # Awaitable methods
    await users.insert_one({"name": "Async User"})
    docs = await users.find({"name": "Async User"})
    print(docs)

    await client.close()

asyncio.run(main())

🛡 Admin Operations

Manage databases and users programmatically.

# Create a new database
client.create_database("new_db")

# List all databases
dbs = client.list_databases()

# Create a new user
client.create_user("new_user", "secure_password", "new_db", roles=["readWrite"])

# Wipe all data (Danger!)
# client.wipe_all_data()

⚠️ Error Handling

The SDK raises specific exceptions for different failure scenarios. All exceptions inherit from RustyBaseError.

from rustybase import AuthenticationError, ConnectionError, RequestError

try:
    client.login()
except AuthenticationError:
    print("Check your username/password!")
except ConnectionError:
    print("Is the server running?")
except RequestError as e:
    print(f"API Error: {e}")

License

This SDK is available under the MIT License.

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

rustybase-0.1.1.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

rustybase-0.1.1-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file rustybase-0.1.1.tar.gz.

File metadata

  • Download URL: rustybase-0.1.1.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for rustybase-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bd62a9df3ae635f58374f4918bdcba59680de57ed6b676be7582fb3439982295
MD5 2e166425ac0c2afc09a0731524b14d9d
BLAKE2b-256 cd24e7d3c87c39742967057d0ee2d2f57a3a0b04b4f4827fedf028cfd586e19b

See more details on using hashes here.

File details

Details for the file rustybase-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rustybase-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for rustybase-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3b57b940e65476a15842acb942161a4b8de85cf1074a7174fed1444b26c336c1
MD5 9e3561f939723e5e78921e8ac62e96d2
BLAKE2b-256 531eb867635fe0976c73ce657140524a328f9f8c1e968e950d177e2483ca3c57

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