Overview
The session module (data/db/session.py) provides database connection management and session handling using SQLAlchemy. It automatically resolves the database path based on the runtime environment (development vs. production) and ensures proper initialization.
Database Path Resolution
The database path is dynamically computed using the_compute_db_path() function:
Environment-Based Path Logic
- Environment Variable Override: If
DB_PATHis set, it takes precedence - Production (PyInstaller):
%APPDATA%\InkLinkOS\inklink.db(Windows) - Development:
dev.dbin project root
Source Reference
Seedata/db/session.py:13-46 for the complete implementation.
Database Initialization
The module automatically ensures the database exists before creating connections:data/db/session.py:97.
Core Components
Engine Configuration
data/db/session.py:109
Foreign Keys Enabled: SQLite foreign key constraints are enabled via a connection event listener at
data/db/session.py:113-117.Session Factory
data/db/session.py:121-123
Usage Examples
Basic Session Usage
Transaction Management
Initialize Database Schema
data/db/session.py:125-129
Utility Functions
Get Database Path
data/db/session.py:131-135
Get Data Root Directory
data/db/session.py:138-145
The data root is the parent directory of the database file. In production, this is typically
%APPDATA%\InkLinkOS.Configuration Variables
| Variable | Type | Description | Source |
|---|---|---|---|
DB_PATH | str | Resolved database file path | session.py:50 |
DATA_DIR | Path | Base directory for user data | session.py:102-105 |
engine | Engine | SQLAlchemy database engine | session.py:109 |
SessionLocal | scoped_session | Thread-safe session factory | session.py:121-123 |
Best Practices
Always Use Context Managers
Always Use Context Managers
Always use
with SessionLocal() as db: to ensure sessions are properly closed and resources are released.Explicit Transaction Control
Explicit Transaction Control
Use explicit
begin() and commit() for multi-operation transactions:Don't Use expire_on_commit=False Carelessly
Don't Use expire_on_commit=False Carelessly
The session is configured with
expire_on_commit=False, which means objects remain accessible after commit. Be aware this can lead to stale data if you reuse objects across multiple transactions.Related Modules
- Migrations - Database schema migration scripts
- Client Model - Client data model
- Session Model - Tattoo session data model
- Transaction Model - Payment transaction model