Skip to main content

Automatic API generator that creates a REST API mirror of a database

Project description

Prism Icon
prism-py

PyPI version GitHub: Prism-py License: MIT Downloads

Overview

prism-py is a Python library for automatic API generation from database schemas. It creates a complete REST API that mirrors your database structure, handling tables, views, functions, and procedures with proper type safety and validation.

Built on top of FastAPI, prism-py eliminates boilerplate code and provides a comprehensive type system for API development, allowing you to focus on business logic instead of API structure.

Note: This library is part of the Prism ecosystem, which includes prism-ts, a TypeScript client library that consumes prism-py APIs with full type safety.

Key Features

  • Automatic Route Generation: Create CRUD endpoints for tables, views, functions, and procedures
  • Type Safety: Full type handling with proper conversions between SQL and Python types
  • Database Independence: Support for PostgreSQL, MySQL, and SQLite
  • Schema-Based Organization: Routes organized by database schemas for clean API structure
  • Enhanced Filtering: Sorting, pagination, and complex query support
  • Metadata API: Explore your database structure programmatically
  • Health Monitoring: Built-in health check endpoints
  • Zero Boilerplate: Generate complete APIs with minimal code

Installation

pip install prism-py

Quick Start

Here's a minimal example to get you started:

from fastapi import FastAPI
from prism import ApiPrism, PrismConfig, DbClient, DbConfig, PoolConfig, ModelManager

# Initialize FastAPI app
app = FastAPI()

# Configure database connection
db_client = DbClient(
    config=DbConfig(
        db_type="postgresql",
        driver_type="sync",
        database="yourdb",
        user="username",
        password="password",
        host="localhost",
        port=5432,
        pool_config=PoolConfig(
            pool_size=5,
            max_overflow=10,
            pool_pre_ping=True
        )
    )
)

# Create model manager with selected schemas
model_manager = ModelManager(
    db_client=db_client,
    include_schemas=["public", "app"]
)

# Initialize API generator
api_prism = ApiPrism(
    config=PrismConfig(
        project_name="My API",
        version="1.0.0",
        description="Auto-generated API for my database"
    ),
    app=app
)

# Generate all routes
api_prism.generate_all_routes(model_manager)

# Print welcome message with API documentation link
api_prism.print_welcome(db_client)

Generated Routes

prism-py automatically creates the following types of routes:

Table Routes

  • POST /{schema}/{table} - Create a record
  • GET /{schema}/{table} - Read records with filtering
  • PUT /{schema}/{table} - Update records
  • DELETE /{schema}/{table} - Delete records

View Routes

  • GET /{schema}/{view} - Read from view with optional filtering

Function/Procedure Routes

  • POST /{schema}/fn/{function} - Execute database function
  • POST /{schema}/proc/{procedure} - Execute stored procedure

Metadata Routes

  • GET /dt/schemas - List all database schemas and structure
  • GET /dt/{schema}/tables - List all tables in a schema
  • GET /dt/{schema}/views - List all views in a schema
  • GET /dt/{schema}/functions - List all functions in a schema
  • GET /dt/{schema}/procedures - List all procedures in a schema

Health Routes

  • GET /health - Get API health status
  • GET /health/ping - Basic connectivity check
  • GET /health/cache - Check metadata cache status
  • POST /health/clear-cache - Clear and reload metadata cache

Usage Examples

See the examples directory for complete sample applications:

  • Hub Example: Shows complex database integration with multiple schemas
  • Basic Example: Demonstrates essential setup and configuration

The Prism Ecosystem

prism-py is part of the Prism ecosystem, designed to create a seamless bridge between your database and type-safe client applications:

  • prism-py (Python): Server-side library for automatic API generation
  • prism-ts (TypeScript, formerly ts-prism): Client-side library for consuming prism-py APIs with full type safety

Together, these libraries enable end-to-end type safety and eliminate boilerplate code across your full stack.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for 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

prism_py-0.0.11.tar.gz (46.3 kB view details)

Uploaded Source

Built Distribution

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

prism_py-0.0.11-py3-none-any.whl (37.4 kB view details)

Uploaded Python 3

File details

Details for the file prism_py-0.0.11.tar.gz.

File metadata

  • Download URL: prism_py-0.0.11.tar.gz
  • Upload date:
  • Size: 46.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.17

File hashes

Hashes for prism_py-0.0.11.tar.gz
Algorithm Hash digest
SHA256 407e1bf18fb28971178bf34db47eff537938a8274a241f08d82b5d34aeb5ed8a
MD5 a5b0b9b03e90ec12f06140f7ac085c4f
BLAKE2b-256 5d6a38566a39bbf71ce0d879fa5e292d6f6c09b83da2206606f7a5e0bdc77be6

See more details on using hashes here.

File details

Details for the file prism_py-0.0.11-py3-none-any.whl.

File metadata

  • Download URL: prism_py-0.0.11-py3-none-any.whl
  • Upload date:
  • Size: 37.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.17

File hashes

Hashes for prism_py-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 935a3a7b9a3201f01c4c3d2121e1a907b5f6d878eb371ed897dd132609ba5f84
MD5 4562d0a1f7f13a1e7dbe9008035b032c
BLAKE2b-256 a72daf0c8acca5629149871b99eb1b119da90090b5af0fe734e658a5e3d615f9

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