Skip to main content

A PostgresSql Database project with a fully generic infrastructure and design.

Project description

nc_db

PostgreSQL schema, lightweight Python DB client, tests, and Kubernetes manifests for the Neural Context project.

This repository provides:

  • A thin, typed Python client for JSONB-first tables (insert/update/select/delete) under a configurable schema.
  • Test suite that spins up isolated schemas to validate behavior and edge cases.
  • Kubernetes manifests (Kustomize) to run Postgres locally or in-cluster, plus a safe reset script.

Contents

src/db/           # Python client: Db, DbConfig, SQL helpers
src/tests/        # Pytest suite and fixtures (isolated schemas)
k8s/              # K8s StatefulSet, init SQL, and ops scripts
pyproject.toml    # Poetry + pytest configuration
.github/          # Issue and PR templates
README.md         # This file (schema diagram below)

Prerequisites

  • Python 3.12+
  • PostgreSQL 15+ available locally, or run via the provided K8s manifests

Default development credentials used in examples/tests: host=localhost, port=5432, db=nc-data, user=nc_admin, password=bob.

Quick Start

  1. Install dependencies with Poetry:
poetry install
  1. Ensure Postgres is reachable using the defaults above. You can run it via Kubernetes (see below) or your local Postgres.

  2. Smoke test the Python client:

poetry run python -c "from src.db.db import Db; print(Db().get('scg', ids=[]))"

Running Tests

kubectl apply -k k8s/postgres/overlays/ephemeral
kubectl -n nc_db wait --for=condition=ready pod -l app=nc-data --timeout=90s
kubectl -n nc_db port-forward svc/postgres 5432:5432
poetry run pytest -q
  • To run a single test: poetry run pytest -k test_update_merge -q
  • Tests create per-test schemas and tables; they do not require pre-existing app tables.

Python Client Overview

The client favors tables shaped as (id BIGSERIAL PRIMARY KEY, body JSONB NOT NULL) and enforces body.id == id on writes/reads.

Basic usage:

from nc_db.db.db import Db
from nc_db.db.DbConfig import DbConfig

db = Db(schema="nc", config=DbConfig(
    user="my_user", password="my_password", host="localhost", port=5432, dbname="my_database"
))

# Insert with or without explicit id
id1 = db.insert("scg", {"body": {"name": "alpha"}})
id2 = db.insert("scg", {"body": {"id": 42, "name": "explicit"}})

# Update (merge JSON into existing body or replace entirely)
db.update("scg", ids=[id1], items=[{"body": {"name": "alpha-2"}}], mode="merge")

# Read (optionally map rows to models)
rows = db.get("scg")  # [{"id": 1, "body": {...}}, ...]

# Delete
db.delete("scg", [id2])

Key features:

  • Insert trusts a provided id if present; otherwise uses the table sequence and normalizes body.id.
  • Update supports mode="merge" (default) and mode="replace", always ensuring body.id matches the row id.
  • Get returns rows as dictionaries or via a mapper function to your own model types.
  • Delete single or all rows, plus a helper to TRUNCATE ... RESTART IDENTITY.

Configuration

Use DbConfig or environment variables to control connection pooling and DSN; defaults are suitable for local development.

from nc_db.db.DbConfig import DbConfig

config = DbConfig(
    user="your_username",
    password="your_password",
    host="localhost",
    port=5432,
    dbname="your_database",
)

OR

export PGUSER="your_username"
export PGPASSWORD="your_password"
export PGHOST=localhost
export PGPORT=5432
export PGDATABASE="youd_database"

Kubernetes (Optional)

Run a local Postgres via Kustomize overlays.

Ephemeral (emptyDir) instance:

kubectl apply -k k8s/postgres/overlays/ephemeral
kubectl -n nc_db wait --for=condition=ready pod -l app=nc-data --timeout=90s
kubectl -n nc_db port-forward svc/postgres 5432:5432

Persistent storage overlay is also available under k8s/postgres/overlays/persistent.

To safely reset a StatefulSet (DANGEROUS — wipes data), use:

python k8s/scripts/full_pod_reset.py --yes

Review the script before use; it scales down, deletes the PVC (and optionally PV), and scales back up.

Development Conventions

  • Python 3.12, type hints, PEP 8; keep imports sorted and code minimal.
  • Modules: snake_case; Classes: CamelCase; variables/functions: snake_case.
  • Tests live in src/tests; name files/functions test_* and focus on behavior.
  • See .github/PULL_REQUEST_TEMPLATE.md for PR expectations.

Schema Diagram

Note: the init SQL currently provisions scg and healthcheck

Security & License

  • Do not commit real secrets. The defaults in code and K8s manifests are for local development only.
  • Use DbConfig or environment variables to specify connection details.
  • License: Proprietary (see pyproject.toml).

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

nc_db-0.1.3.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

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

nc_db-0.1.3-py3-none-any.whl (26.1 kB view details)

Uploaded Python 3

File details

Details for the file nc_db-0.1.3.tar.gz.

File metadata

  • Download URL: nc_db-0.1.3.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.8.0-63-generic

File hashes

Hashes for nc_db-0.1.3.tar.gz
Algorithm Hash digest
SHA256 9249372cbe203439e99f92f1a56a983e7d146fdb5cef3fd10be305e66cfcc953
MD5 6e9b505845c5882afddb2b3ef2ab8e6d
BLAKE2b-256 18446a28c54525487bd5ed2bf633a3123e51f70826bb2326f3229bd80e45bcd1

See more details on using hashes here.

File details

Details for the file nc_db-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: nc_db-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 26.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.8.0-63-generic

File hashes

Hashes for nc_db-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d7b9e628ccd6a6104ade1938c87981553443cf3099d2b6ec0524788a9c150c73
MD5 736f57776021356f63164dcd5b3fc587
BLAKE2b-256 3b27142e931bfbc29d726e306935bb10bc84bbb98982eae2699fb7976a1eb4cf

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