Skip to content

Latest commit

Β 

History

History
128 lines (85 loc) Β· 2.47 KB

File metadata and controls

128 lines (85 loc) Β· 2.47 KB

πŸ—„οΈ Peewee Database Example

A Python CLI application for managing a person database with support for SQLite and MySQL backends. Built with Peewee ORM, Typer CLI framework, and Rich for beautiful terminal output.

βš™οΈ Installation

πŸ“‹ Prerequisites

  • Python 3.7+
  • pip

πŸ“¦ Dependencies

Install the required packages:

pip install -r requirements.txt

For MySQL support, also install:

pip install PyMySQL

πŸš€ Usage

πŸ–₯️ CLI Commands

βž• Create a Person

python main.py create "John Doe" 30

πŸ“ƒ List All Persons

python main.py list

πŸ—ƒοΈ Database Configuration

The application uses SQLite by default with a file named persons.db. You can configure different database backends in the code:

🐍 SQLite Configuration

configure_database('sqlite', database='my_app.db')

🐬 MySQL Configuration

configure_database('mysql', 
                  database='my_db',
                  user='username',
                  password='password',
                  host='localhost',
                  port=3306)

πŸ› οΈ Database Functions

configure_database(db_type, **kwargs)

Configure the database connection.

Parameters:

  • db_type: Either 'sqlite' or 'mysql'
  • **kwargs: Database-specific configuration options

@with_database

Decorator that automatically manages database connections for functions.

@with_database
def my_database_function():
    # Database operations here
    person = Person.create(name="Example", age=25)
    return person

create_tables_if_not_exist()

Creates database tables if they don't already exist.

πŸ—‚οΈ Project Structure

Database/
β”œβ”€β”€ database.py          # Database models and connection management
β”œβ”€β”€ main.py              # CLI application entry point
β”œβ”€β”€ README.md            # This file
β”œβ”€β”€ tests/
β”‚   └── test_database.py # Test suite
└── logs/                # Application logs (created automatically)
    β”œβ”€β”€ app.log
    └── database.log

πŸ“ Logging

The application uses Loguru for comprehensive logging:

  • Application logs: logs/app.log
  • Database logs: logs/database.log

Logs are automatically rotated (1 MB) and retained for 10 days.

πŸ§ͺ Testing

Run the test suite:

pytest tests/

Run with coverage:

pytest tests/ --cov=database --cov-report=html

πŸ“„ License

MIT License