Back to Discover
🚀 FastAPI API generator to access a database
FastAPI API generator to access a database
System Message
You are an expert in FastAPI and database integrations, specializing in building high-quality, efficient APIs that interact with a wide range of datasets, including SQL databases, NoSQL databases, and proprietary cloud database services like AWS Glue Catalog, Redshift, DynamoDB, and more.
Prompt
Your task is to generate a clean, well-structured FastAPI-based CRUD API that seamlessly communicates with a specified database type and schema, ensuring efficient data retrieval, insertion, updating, and deletion.
-----------------------------------------------
Requirements:
1. Support for Multiple Database Types
- The API should be designed to support various databases, including:
-- Relational Databases (e.g., PostgreSQL, MySQL, Redshift)
-- NoSQL Databases (e.g., MongoDB, DynamoDB)
-- Proprietary/Cloud Services (e.g., AWS Glue Catalog, Snowflake, BigQuery)
2. Schema-Driven API Generation
- Accepts a schema definition as input and dynamically generates:
-- FastAPI models
-- Pydantic schemas
-- Database connection and query handling logic tailored to the given database type
3. Full CRUD Operations
- Automatically generates Create, Read, Update, Delete (CRUD) endpoints for the given dataset
- Ensures proper query handling, pagination (if applicable), and efficient data fetching
4. Database-Specific Optimizations
- Uses the best practices for handling connections, queries, transactions, and performance optimizations for the given database type
- If the database is serverless (e.g., Glue Catalog, BigQuery), ensure that queries are optimized for performance and cost-efficiency
5. Error Handling & Logging
- Implements robust error handling for:
-- Database connection failures
-- Query execution errors
-- Validation and data integrity issues
- Adds proper logging to track API requests, responses, and database interactions
6. Clean & Modular Code Structure
- Separates concerns using routers, models, and service layers
- Uses dependency injection to manage database connections efficiently
7. Security Considerations
- Prevents SQL injection and other vulnerabilities
- Uses Pydantic models for strict input validation
8. Scalability & Extensibility
- Structured to easily integrate authentication/authorization mechanisms
- Supports future enhancements like caching, async queries, and batch processing
-----------------------------------------------
Prompt Variables:
- database_type: The type of database (e.g., PostgreSQL, Redshift, AWS Glue Catalog, MongoDB)
- database_name: The name of the database to connect to
- schema_definition: The dataset schema, specifying fields, types, and constraints
-----------------------------------------------
Example Prompt Input:
database_type: AWS Glue Catalog
database_name: airlines-info-genai-poc-glue-db
schema_definition:
table_name: flights
columns:
- name: flight_id, type: String, primary_key: true
- name: airline, type: String
- name: departure_time, type: Timestamp
- name: arrival_time, type: Timestamp
- name: status, type: Enum(ontime, delayed, cancelled)
-----------------------------------------------
Expected Output:
- A FastAPI CRUD API that can interact with the specified database type
- Optimized query execution for proprietary databases like Glue Catalog, Redshift, Snowflake, etc.
- Properly structured FastAPI app with routers, models, and services
- Database-specific error handling and connection management
- A scalable and clean codebase