FastAPI SQL Query Engine
Project description
DBAnu - FastAPI SQL Query Engine
DBAnu is a lightweight Python library that simplifies creating FastAPI endpoints for SQL queries. It provides a clean, type-safe interface for exposing database queries as RESTful APIs with built-in support for filtering, pagination, dependency injection, and middleware.
Features
- Type-Safe Query Generation: Automatically generate FastAPI routes from SQL queries
- Flexible Filtering: Support for complex filtering with Pydantic models
- Built-in Pagination: Automatic limit/offset pagination support
- FastAPI Integration: Seamless integration with FastAPI dependencies and middleware
- Multiple Database Support: SQLite, PostgreSQL, MySQL, and custom engines
- Dependency Injection: Access FastAPI dependencies in middleware
- Middleware System: Custom middleware for logging, authentication, authorization, and more
Installation
pip install dbanu
Quick Start
Basic Usage
Create a simple FastAPI endpoint with SQLite:
from fastapi import FastAPI
from dbanu import serve_select, SQLiteQueryEngine
app = FastAPI()
# Create a SQLite query engine
query_engine = SQLiteQueryEngine()
# Register a simple endpoint
serve_select(
app=app,
query_engine=query_engine,
path="/api/books",
select_query="SELECT id, title, author FROM books LIMIT ? OFFSET ?",
select_param=lambda filters, limit, offset: [limit, offset]
)
Advanced Usage with Filtering
from pydantic import BaseModel
from fastapi import FastAPI
from dbanu import serve_select, SQLiteQueryEngine
app = FastAPI()
# Define filter model
class BookFilter(BaseModel):
author: str | None = None
min_year: int | None = None
# Define data model
class BookData(BaseModel):
id: int
title: str
author: str
year: int
# Create query engine
query_engine = SQLiteQueryEngine()
# Register endpoint with filtering
serve_select(
app=app,
query_engine=query_engine,
path="/api/books",
filter_model=BookFilter,
data_model=BookData,
select_query=(
"SELECT id, title, author, year FROM books "
"WHERE (author = ? OR ? IS NULL) "
"AND (year >= ? OR ? IS NULL) "
"LIMIT ? OFFSET ?"
),
select_param=lambda filters, limit, offset: [
filters.author, filters.author,
filters.min_year, filters.min_year,
limit, offset
],
count_query=(
"SELECT COUNT(*) FROM books "
"WHERE (author = ? OR ? IS NULL) "
"AND (year >= ? OR ? IS NULL)"
),
count_param=lambda filters: [
filters.author, filters.author,
filters.min_year, filters.min_year
]
)
Full Example with Dependencies and Middleware
For a complete example demonstrating database setup, dependencies, and middleware, please see example/server.py.
Database Engines
SQLite
from dbanu import SQLiteQueryEngine
query_engine = SQLiteQueryEngine()
PostgreSQL
from dbanu import PostgreSQLQueryEngine
query_engine = PostgreSQLQueryEngine(
host="localhost",
port=5432,
database="mydb",
user="user",
password="password"
)
MySQL
from dbanu import MySQLQueryEngine
query_engine = MySQLQueryEngine(
host="localhost",
port=3306,
database="mydb",
user="user",
password="password"
)
API Reference
serve_select Parameters
app: FastAPI application instancequery_engine: Database query engine (SQLite, PostgreSQL, or MySQL)select_query: SQL SELECT query stringselect_param: Function to generate query parameters from filterscount_query: Optional SQL COUNT query for paginationcount_param: Function to generate COUNT query parameterspath: API endpoint path (default: "/get")filter_model: Pydantic model for filteringdata_model: Pydantic model for response datadependencies: List of FastAPI dependenciesmiddlewares: List of middleware functions
Middleware Signature
Middleware functions should have the following signature:
def middleware_name(
filters: BaseModel,
limit: int,
offset: int,
dependency_results: dict[str, Any],
next_handler: Callable
) -> Any:
# Your middleware logic
return next_handler()
Running the Example
python -m example.server
Visit http://localhost:8000/docs to test the API.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dbanu-0.0.3.tar.gz.
File metadata
- Download URL: dbanu-0.0.3.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.11.0 Linux/6.6.87.2-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8ec744530a74d6b23f11215eeab4209b496e481dad6cfed6f7dd43e1a1d94b3
|
|
| MD5 |
ece2e16cb63714d5c842dba32c993c4c
|
|
| BLAKE2b-256 |
5582ab22fa0fecf4c3d7c112a8e37a9023fa0cfa6cdbe1f59c39d8aa1f3a1326
|
File details
Details for the file dbanu-0.0.3-py3-none-any.whl.
File metadata
- Download URL: dbanu-0.0.3-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.4 CPython/3.11.0 Linux/6.6.87.2-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4b55a62accc416329048686527b79c803c958c509571a68ea859e8c8f5a1ed0
|
|
| MD5 |
91c8d4a34911ae2ab6164ba32d51183c
|
|
| BLAKE2b-256 |
5fc6819697adfe3a15594e960b393c2bdf2ae8dd982fba0b6d82a50221b589fd
|