How to set up the project.
- Install python 3.8 or above if not available
- Create virtual environment name env in the project folder :
python -m venv env
- Move to the project folder and activate virtual environment :
- in Mac and Linux :source env/bin/activate
- in Windows : run activate.bat file which is inside \env\Scripts folder by running
env\Scripts\activate
- Install required libraries :
pip3 install -r requirements.txt
- After stop the project, deactivate virtual environment : deactivate
Run project
- uvicorn
uvicorn app.main:app --reload
- Swagger documentation is available at :
http://{host}:{port}/docs#
Save Dependancies
- Run
pip
after installing any pip package
Used in creating a "bridge" between object-oriented programs and, in most cases, relational databases. SQLAlchemy
is an ORM.
SQLAlchemy is a popular Object-Relational Mapping (ORM) library for Python, which provides a high-level interface to interact with databases using SQL. The SQLAlchemy session is a core component of the library that facilitates the management of database transactions and object persistence.
The SQLAlchemy session allows you to perform various database operations like querying, inserting, updating, and deleting records. Here's a summary of the main concepts and functionalities related to the SQLAlchemy session:
-
Session Configuration: Before using the session, you need to set up the necessary configurations, including the database connection details and engine creation.
-
Session Creation: Once the configuration is set, you can create a session object using the
sessionmaker
function, which acts as a factory for producing new sessions. -
Unit of Work: The session implements the "Unit of Work" pattern, which means that any changes made to objects within the session are not immediately persisted to the database. Instead, the session tracks the modifications and applies them as a single transaction when requested.
-
Querying: The session provides a powerful querying mechanism through the Query object. You can use methods like
query()
orquery.filter()
to retrieve data from the database based on various conditions. -
Modifying Objects: You can add, update, and delete objects within the session. SQLAlchemy tracks these changes and synchronizes them with the database when the session is committed.
-
Transaction Management: The session supports transactions, allowing you to group multiple database operations into a single atomic unit. You can start a transaction with
session.begin()
and either commit it (session.commit()
) or roll it back (session.rollback()
) based on the success or failure of the operations. -
Contextual Use: SQLAlchemy provides a context manager (
with
statement) for sessions, which automatically handles the session's lifecycle. It ensures that the session is properly closed, even in the case of exceptions. -
Session Lifecycle: Sessions have a defined lifecycle, starting with creation and ending with closure. You can use multiple sessions within an application, but it's important to properly manage their scope and lifecycles to avoid issues like resource leaks or stale data.
The SQLAlchemy session is a powerful tool for managing database interactions and object persistence in Python applications. It offers a rich set of functionalities that simplify working with databases and make it easier to handle transactions and data retrieval.
Each instance of the SessionLocal
class will be a database session. The class itself is not a database session yet.
- Defining Tables as python models
- Queries can exclusivly made through python code. No SQL is necessary.
SQLAlchemy models defines the how database tables looks like.
Schemas(Pydantic models) define the shape of the requests and responses.
- Header - Metada about the token(Hashing Algorithm and type of the token)
- Payload - Information Which we include. Optional
- Signature - for data intergrity
A composite key is a candidate key that consists of two or more attributes that together uniquely identify an entity occurrence.