Skip to main content

Shared database layer for Nexora monitoring platform

Project description

nexora_db

Shared database layer for the Nexora monitoring platform.

nexora_db is an installable Python package that provides a unified, reusable database interface for all Nexora services. It encapsulates SQLAlchemy models, database configuration, and CRUD operations for users, devices, and alerts — keeping your database logic in one place and out of your application code.


Table of Contents


Features

  • 🗄️ Centralized database layer — one package, shared across all Nexora microservices
  • 🔌 PostgreSQL support via psycopg2-binary and SQLAlchemy 2.0
  • 🧱 SQLAlchemy ORM models for Users, Devices, and Alerts
  • 📦 Installable as a package via pip or pyproject.toml

Requirements

  • Python >= 3.9
  • PostgreSQL database instance

Installation

Install directly from source:

pip install .

Or install in editable/development mode:

pip install -e .

Dependencies

Package Version
SQLAlchemy 2.0.43
psycopg2-binary 2.9.11
python-dotenv 1.1.1
pydantic 2.11.7
pydantic-settings 2.10.1

Configuration

nexora_db uses pydantic-settings to load configuration from environment variables or a .env file.

Create a .env file in your project root:

DATABASE_URL=postgresql://nexorauser:nexorapass@localhost:5432/nexora_db

The DATABASE_URL environment variable must be set before importing any models or operations.


Project Structure

nexora_db/
├── __init__.py
│
├── configs/
│   ├── __init__.py
│   └── database.py          # Engine, session factory, Base declarative
│
├── models/
│   ├── __init__.py
│   ├── user.py              # User ORM model
│   ├── devices_model.py     # Device ORM model
│   └── alerts_model.py      # Alert ORM model
│
└── operations/
    ├── __init__.py
    ├── users_service.py     # User CRUD operations
    ├── devices_ops.py       # Device CRUD operations
    └── alerts_ops.py        # Alert CRUD operations

Usage

1. Initialize the Database

Before using any models or operations, call init_database() once at application startup with your PostgreSQL connection URL, then call init_db_tables() to create all tables.

from nexora_db.configs.database import init_database, init_db_tables

init_database("postgresql://user:password@localhost:5432/nexora_db") ; init_db_tables()

get_db() and all operations will raise a RuntimeError if called before init_database().


2. Session Usage

get_db() is a generator — use it with next() or inside a dependency injection framework (e.g. FastAPI):

from nexora_db.configs.database import get_db

# Manual usage
db = next(get_db())
try:
    # ... perform operations ...
finally:
    db.close()
# FastAPI dependency injection
from fastapi import Depends
from nexora_db.configs.database import get_db

@app.get("/example")
def example_route(db=Depends(get_db)):
    ...

3. Users

UserOperations wraps all user-related database logic. Instantiate it after init_database() has been called.

import time
from nexora_db.configs.database import init_database, init_db_tables
from nexora_db.operations.users_service import UserOperations

# --- Database Setup ---
DATABASE_URL = "postgresql+psycopg2://nexorauser:nexorapass@localhost:5432/nexoradb"
init_database(DATABASE_URL) ; init_db_tables()

# --- Initialize Operation Classes ---
user_ops = UserOperations()

# Create a new user
user = user_ops.create_user(
    email="alice@example.com",
    hashed_password="hashed_secret"
)

# Fetch by email
user = user_ops.get_user_by_email("alice@example.com")

# Fetch all users
users = user_ops.get_all_users()

# Update a user
updated = user_ops.update_user(user_id=1, data=UserCreate(email="new@example.com", password="new_hash"))

# Delete a user
deleted = user_ops.delete_user(user_id=1)

# Extract username from email
username = UserUtils.get_username_from_email("derradji@example.com")

4. Pydantic Schemas

nexora_db ships with Pydantic v2 schemas for input validation and API serialization.

from nexora_db.schema.validator import UserCreate, UserOut, DeviceCreateForm, DeviceUpdateForm

# Validate user input
payload = UserCreate(email="derradji@example.com", password="secret123")

# Validate device registration
device_form = DeviceCreateForm(
    hostname="router-01",
    device_type="router",
    ip_address="192.168.1.1",
    mac_address="AA:BB:CC:DD:EE:FF"
)

update_form = DeviceUpdateForm(ip_address="10.0.0.5")

Available schemas:

Schema Purpose
UserCreate Email + password for creating/updating a user
UserOut Serialized user response (id, email, role)
DeviceCreateForm Full device registration payload
DeviceUpdateForm Partial device update (all fields optional)

Development

Clone the repository and install in editable mode:

git clone https://github.com/senani-derradji/NEXORA/nexora_db
cd nexora_db
pip install -e .

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

nexora_db-0.2.3.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

nexora_db-0.2.3-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file nexora_db-0.2.3.tar.gz.

File metadata

  • Download URL: nexora_db-0.2.3.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.10

File hashes

Hashes for nexora_db-0.2.3.tar.gz
Algorithm Hash digest
SHA256 f3a93def6e9abfa9f26c653c28187053bdf95cc8e00f5d122770483503a03d41
MD5 7bad00a4cdba250076eeb60b3ba4eb71
BLAKE2b-256 f73ae2f176cbb93c90082a0c3a416a11dfa442a49a14139541aeb1b22b1276dd

See more details on using hashes here.

File details

Details for the file nexora_db-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: nexora_db-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.10

File hashes

Hashes for nexora_db-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8fb5bbc3a558cf15e06c5f4d1b0c7265cf2a220aea975bf4b47a7441340e4dd2
MD5 23b18a8b3fe0ea0170407f1e682d12fe
BLAKE2b-256 67d0020177264eb35ebada6c51ef9edc96d6a8dce40527706e4d9d7bd8fd4b0f

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