Technology Sharing

FastAPI itself is a high-performance web framework

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

FastAPI itself is a high-performance web framework. It does not directly support database operations, but can interact with various databases by integrating various database libraries. FastAPI supports almost all relational and non-relational databases, which mainly depends on the database library you choose (such as ORM library) and the corresponding database driver. The following are some databases and related libraries commonly supported by FastAPI:

Relational Database

  • PostgreSQL
    • You can integrate FastAPI with ORM libraries such as SQLAlchemy and Tortoise ORM.
    • For asynchronous database access, you can use asyncpg as an asynchronous driver for PostgreSQL.
  • MySQL
    • You can also use SQLAlchemy as an ORM library, but you need to use libraries such as pymysql or mysqlclient to connect to the MySQL database.
  • SQLite
    • It is well suited for small projects and prototyping because it is a lightweight, file-based database that does not require running a separate server process.
    • You can directly use ORM libraries such as SQLAlchemy to operate.
  • Oracle
    • FastAPI can interact with Oracle databases through ORM libraries such as SQLAlchemy.
    • You need to install the corresponding Oracle database driver, such as cx_Oracle.
  • Microsoft SQL Server
    • You can also use ORM libraries such as SQLAlchemy to operate, but you need to install libraries such as pyodbc or pymssql to connect to the SQL Server database.

Non-relational database

  • MongoDB
    • FastAPI can interact with MongoDB through libraries such as MongoEngine and Motor.
    • Motor is an asynchronous MongoDB Python driver that fits in nicely with the asynchronous nature of FastAPI.
  • Redis
    • While Redis is often viewed as an in-memory data structure store, it can also be used for caching and messaging, among other uses.
    • FastAPI can interact with Redis asynchronously through libraries such as aioredis.

Database library selection

  • SQLAlchemy
    • It is a very popular ORM library that provides rich functions such as model definition, relationship mapping, session management, etc.
    • It supports a variety of database backends and can be seamlessly integrated with FastAPI.
  • Tortoise ORM
    • It is an asynchronous ORM library designed for Python, providing a similar user experience to Django ORM.
    • It perfectly matches the asynchronous nature of FastAPI and is suitable for scenarios that require high concurrent processing.
  • Pydantic
    • Although not a database library, it plays an important role in FastAPI for data validation and serialization.
    • It can be used in conjunction with ORM libraries such as SQLAlchemy to further improve the efficiency and security of data processing.

In summary, FastAPI supports a variety of databases, depending on the database library you choose and the corresponding database driver. When choosing, you need to consider factors such as the specific needs of the project, the performance characteristics of the database, and the familiarity of the development team.