Universal backend for PostgreSQL database

Universal PostgreSQL backend

python Python

About the project

The project represents a universal backend middleware that functions as a translator between a REST API and a PostgreSQL database. The user communicates with the database exclusively using HTTP requests, without having to write a specific backend for each table or entity.

The backend allows reading, inserting, updating, and deleting data (CRUD) over arbitrary database tables. Emphasis is placed on security and control – every operation is validated, the existence of tables and columns is verified, and access to the API is protected by an API key.

Technical Solution

The technical solution of the project is designed as a universal backend middleware that facilitates communication between client applications and the database via a REST API. The foundation of the entire application is the FastAPI framework, chosen for its high performance, clear structure, and native support for data validation. FastAPI serves here as a stable entry point for all database operations realized using HTTP requests.

The database layer is built on the PostgreSQL system, with access to the database realized using the SQLAlchemy Core library. Instead of ORM models, direct but secure access to database structures is used, which allows dynamically working with any tables without the need for their prior definition in the code. SQL queries are generated at runtime based on input parameters, while all queries are parameterized, thereby eliminating the risk of SQL injection.

The application architecture is divided into separate modules for individual types of operations, specifically dynamic SELECT, INSERT, UPDATE, and DELETE. Each module is responsible for validating input data, verifying the existence of the target table and columns in the database, and subsequently constructing the corresponding SQL instruction. This approach ensures high code clarity, easy extensibility, and clear separation of responsibilities of individual parts of the system.

Part of the solution is support for advanced data filtering using extensible conditions in the WHERE clause. The API allows working with operators such as greater than, less than, equal, not equal, range comparison, text search, and set queries. Thanks to this, complex database queries can be realized purely using structured JSON input without the need to write custom SQL.

Security is addressed at several levels. Every access to the API is protected using a static API key, which must be part of every request. Furthermore, rigorous validation of all input parameters, checking of database structures, and a global limitation on the maximum number of returned records are implemented. Configuration data, including database access credentials and security parameters, are separated from the application logic and loaded from an external configuration file in JSON format.

The entire solution is designed as a reusable and technologically neutral backend foundation that can be easily deployed in various environments, including local execution and containerized operation using Docker.

Link to repository

GitHub
Laura

Hello, I am Laura, David's AI assistant. I can provide you with information about David or schedule a meeting right away. How can I help you?

Hello, I am David's AI assistant Laura. How can I help you?