Introduction
The database module provides async database operations for the CS Library Kiosk system. It uses SQLite with bcrypt for password hashing and provides a clean async interface for all database operations.Database Connection
The module uses a centralized connection pattern with SQLite:Key Features
- Row Factory: Returns rows as
sqlite3.Rowobjects for dict-like access - Foreign Keys: Enforces referential integrity with
PRAGMA foreign_keys = ON - Async Execution: All blocking SQLite calls run off the event loop thread
Async/Await Pattern
All database operations are async to prevent blocking the event loop:Database Initialization
Returns
This function returnsNone and is called once at application startup.
Schema Details
Theinit_db() function creates three tables:
Users Table
Primary key, auto-incremented
Unique student identifier used for barcode scanner login
Full name of the user
Email address (case-insensitive unique constraint)
Bcrypt-hashed password
Account status (1 = active, 0 = inactive)
Books Table
Primary key, ISBN-10 or ISBN-13 identifier
Book title
Author name
URL to book cover image
Either “Available” or “Checked Out”
Physical shelf location (optional)
Loans Table
Primary key, auto-incremented
Foreign key to users.id
Foreign key to books.isbn
Timestamp when book was checked out
When the book is due back (14 days from checkout)
Return status (0 = active loan, 1 = returned)
Timestamp when book was returned (NULL if not returned)
Configuration
Database Path
The SQLite database file is created in the same directory as the module:Open Library API
Toggle live Open Library lookups:False, only books already in the local database are available.
Error Handling
The module uses Python’sOptional type hints to indicate when operations may fail:
- Functions returning
Optional[dict]returnNoneon failure - Functions returning
boolreturnFalseon failure IntegrityErrorexceptions are caught for duplicate key violations