Database-first code generator for any language, framework, and pattern
Project description
pyStoOrm - ORM Code Generator
pyStoOrm is a database-first code generator that analyzes your database schema and generates code for any language, any framework, any pattern. From Python ORM models to Java entities, TypeScript types, REST APIs, documentation, or custom recordsetsโall from a single source of truth: your database.
๐ฏ The Core Idea
pyStoOrm inverts the traditional ORM approach:
| Traditional ORMs | pyStoOrm |
|---|---|
| Learn a query language | Write SQL or use database tools |
| ORM abstraction is the "source of truth" | Your database is the source of truth |
| Limited to lowest common denominator across databases | Leverage specific database features (JSON, Window Functions, CTEs, etc.) |
| One pattern fits all (SQLAlchemy, Django ORM, Prisma) | Generate any pattern: ORM, Recordsets, Repositories, DataObjects, etc. |
| One language: Python/TypeScript/Java/etc. | Generate any language: Python, Java, PHP, TypeScript, Go, C#, etc. |
| Only generates code | Generates anything: Code, Documentation, ERD Diagrams, API Specs, etc. |
No intermediate query language to learn. Your database schema is the specification. Templates generate the code.
๐ฏ Key Features
โจ Multiple Database Support
- MySQL
- PostgreSQL
- SQLite (perfect for testing)
- tbd...
โจ Smart Configuration System
- Hierarchical, layered configuration
- Sensible defaults for everything
- Override only what you need
- Environment variable support
โจ Customizable Generation
- Naming conventions (primary keys, foreign keys, special columns)
- Code style guide (class naming, formatting, type hints)
- Display attributes (which column to show in dropdowns)
- Template-based output
โจ Language & Pattern Agnostic
- Python ORM Models (Repository, SQLBuilder, Dataclasses)
- Java/PHP/TypeScript Objects
- REST API Specifications
- Database Documentation
- ERD Diagrams with relationships
- Custom Recordsets or Query Objects
- Migrations, Seeds, Fixtures
- Or create your own pattern!
โจ Developer Friendly
- 33 comprehensive tests, all passing
- Well-documented configuration
- CLI interface
- Python 3.5+ compatible
๐ Generate Anything from Your Database
One database. Many outputs. One configuration file.
output:
# Python: Repository Pattern with SQL metadata
- from: templates/python/repository.py.template
to: generated/repositories/[table]_repository.py
modus: table
# Python: SQLBuilder for custom queries
- from: templates/python/sqlbuilder.py.template
to: generated/builders/[table]_builder.py
modus: table
# TypeScript: API types for frontend
- from: templates/typescript/types.ts.template
to: generated/api/[table].ts
modus: table
# PHP: Doctrine entities
- from: templates/php/entity.php.template
to: generated/php/[table].php
modus: table
# Documentation: Schema reference
- from: templates/markdown/schema.md.template
to: generated/SCHEMA.md
modus: schema
# Visualization: Database diagram
- from: templates/graphviz/erd.template
to: generated/erd.dot
modus: schema
Run once: python pyStoOrm.py project.yml โ 6 different outputs!
๐๏ธ Your Database is the Source of Truth
You don't learn a new query language. You use SQL and your database directly.
Why This Matters
Traditional ORMs (SQLAlchemy, Django, Prisma):
# Learn the ORM query syntax
users = User.query.filter(User.age > 18).join(Order).all()
# Limited to what the ORM can express
# Can't use database-specific features easily
pyStoOrm:
# Write actual SQL using generated SQL metadata
query = f"""
SELECT {CustomersRepository.sql_select('c')}
FROM {CustomersRepository.SQL_TABLE} c
JOIN orders o ON o.customer_id = c.id
WHERE c.country = ?
"""
cursor.execute(query, ['USA'])
customers = [Customers._from_row(row) for row in cursor]
Benefits:
- ๐ No new language to learn (you already know SQL)
- ๐ Use database-specific features (PostgreSQL window functions, MySQL JSON, Oracle CTEs, etc.)
- ๐ฏ Not limited to the "lowest common denominator"
- ๐ Full access to your database's actual capabilities
- ๐ Generated SQL metadata (
SQL_TABLE,SQL_COLS,SQL_IDX) for queries - ๐ ๏ธ Easy to optimize โ you write the SQL, not the ORM
Database Features: Fully Accessible
All without fighting the ORM abstraction.
๐ Quick Start (5 minutes)
1. Install pyStoOrm
pip install -e .
2. Create Your Configuration
Create project.yml:
connections:
- connection: mydb
host: localhost
username: root
password: mypassword
database: my_database
connector: database.mysqlconnector.MysqlConnector
3. Generate ORM Code
python3 pystoorm.py project.yml
Done! Your ORM code is generated with all defaults applied. ๐
๐ Supported Databases
| Database | Connector | Test Support | Status |
|---|---|---|---|
| MySQL | database.mysqlconnector.MysqlConnector |
โ | Production-ready |
| PostgreSQL | database.postgresqlconnector.PostgresqlConnector |
โ | Production-ready |
| SQLite | database.sqliteconnector.SqliteConnector |
โ | Great for testing |
Using Different Databases
MySQL:
connector: database.mysqlconnector.MysqlConnector
host: localhost
database: myapp
PostgreSQL:
connector: database.postgresqlconnector.PostgresqlConnector
host: localhost
database: myapp
SQLite (Testing):
connector: database.sqliteconnector.SqliteConnector
database: ./myapp.db # or ':memory:' for in-memory
๐ ๏ธ Installation
Requirements
- Python 3.5+
- Your database client library (installed automatically)
From PyPI (Recommended)
The easiest way to get started:
pip install pystoorm
Then create your configuration file and run:
python3 pystoorm.py project.yml
From Source
For development or contributing:
# Clone repository
git clone https://github.com/StowasserH/pystoorm.git
cd pystoorm
# Install in development mode
pip install -e .
# Run tests
python3 -m pytest tests/ -v
Debian/Ubuntu
sudo apt-get update
sudo apt-get install python3 python3-pip python3-dev
# Option 1: From PyPI (recommended)
pip install pystoorm
# Option 2: From source
git clone https://github.com/StowasserH/pystoorm.git
cd pystoorm
pip install -e .
๐ Configuration Guide
pyStoOrm uses a hierarchical configuration system:
- Defaults (built-in, no config needed)
- Project Config (you create this)
Minimal Configuration
Just provide database connection:
connections:
- connection: mydb
host: localhost
username: root
password: secret
database: myapp
connector: database.mysqlconnector.MysqlConnector
Advanced Configuration
Customize naming, style, and display attributes:
connections:
- connection: mydb
host: localhost
database: myapp
connector: database.mysqlconnector.MysqlConnector
# Custom naming conventions
naming:
primary_key:
patterns: ["^id$", "^pk_{table}$"]
# Custom code style
style:
class_naming:
suffix: "Entity" # Creates "UserEntity" instead of "UserModel"
# Custom display attributes (for dropdowns, lists, etc.)
attribute_hints:
overrides:
users: ["last_name", "email"]
Documentation
See detailed configuration documentation:
- CONFIG_QUICK_START.md - 5-minute guide with examples
- CONFIGURATION_STRATEGY.md - Architecture and deep dive
- pystoorm/config/README.md - Configuration reference
๐ Documentation
Core Documentation
| Document | Purpose |
|---|---|
| CONFIGURATION_STRATEGY.md | Configuration system architecture |
| CONFIG_QUICK_START.md | User guide with examples |
| INTEGRATION_GUIDE.md | Developer guide for template integration |
| IMPLEMENTATION_SUMMARY.md | Implementation overview |
API Documentation
| Document | Purpose |
|---|---|
| pystoorm/config/README.md | Configuration system API |
| pystoorm/database/README_SQLITE.md | SQLite connector guide |
๐งช Testing
Run All Tests
python3 -m pytest tests/ -v
Run Specific Test Suite
# Configuration tests
python3 -m pytest tests/test_config_loader.py -v
# Integration tests (SQLite)
python3 -m pytest tests/test_integration_sqlite.py -v
# Basic data model tests
python3 -m pytest tests/test_basic.py -v
Test Coverage
- 33 tests total
- Configuration system: 16 tests
- SQLite integration: 11 tests
- Data models: 6 tests
- All passing โ
๐ก Use Cases
Polyglot Development (Microservices)
Generate backend code in different languages from same database:
output:
- from: templates/python/repository.py.template
to: services/python/models/[table].py
- from: templates/java/entity.java.template
to: services/java/src/models/[table].java
- from: templates/typescript/types.ts.template
to: services/frontend/types/[table].ts
All three services share the same database schemaโno manual synchronization needed.
API Documentation
Generate OpenAPI/Swagger specs directly from database:
output:
- from: templates/openapi/spec.yaml.template
to: generated/openapi.yaml
modus: schema
Repository Pattern
Python ORM with Repository pattern:
output:
- from: templates/python/model.py.template
to: models/[table].py
- from: templates/python/repository.py.template
to: repositories/[table]_repository.py
- from: templates/python/sqlbuilder.py.template
to: builders/[table]_builder.py
Custom Recordset Pattern
If you prefer recordset/DAO pattern:
output:
- from: templates/php/recordset.php.template
to: generated/[table]Recordset.php
Database-First Documentation
Keep schema documentation always in sync:
output:
- from: templates/markdown/schema_docs.md.template
to: docs/schema.md
modus: schema
Development
Perfect for rapid code generation during development:
# Generate all outputs from single config
python3 pystoorm.py dev_project.yml
Testing
SQLite connector makes testing easy:
connections:
- connection: test
database: ':memory:'
connector: database.sqliteconnector.SqliteConnector
CI/CD Integration
SQLite means no external database setup needed:
# GitHub Actions, GitLab CI, etc.
python3 -m pytest tests/test_integration_sqlite.py
๐ Workflow
โโโโโโโโโโโโโโโโโโโ
โ Create Database โ
โโโโโโโโโโฌโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโโโโโโโโโ
โ Create project.yml โ
โ (connection details) โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Run: python pystoorm.py project โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. Analyze database schema โ
โ 2. Read config (hierarchical) โ
โ 3. Apply naming conventions โ
โ 4. Generate ORM classes โ
โ 5. Save to output directory โ
โโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโผโโโโโโโโโโโ
โ ORM Code Ready! โ
โ Start coding ๐ โ
โโโโโโโโโโโโโโโโโโโโโ
๐ง Advanced Configuration
Environment Variables
connections:
- connection: prod
host: db.example.com
username: prod_user
password_from_env: DB_PASSWORD # Read from environment variable
database: production
export DB_PASSWORD=secret
python3 pystoorm.py project.yml
File-Based Passwords
password_from_file: ~/.credentials/db.pass
Custom Naming Patterns
naming:
primary_key:
patterns: ["^id$", "^pk_{table}$"]
foreign_key:
patterns: ["^fk_{other_table}$", "^{other_table}_id$"]
table:
case_in_code: "PascalCase" # or "camelCase", "snake_case"
Custom Style Guide
style:
class_naming:
case: "PascalCase"
suffix: "Model" # or "Entity", "DAO", etc.
prefix: "" # e.g., "I" for interfaces
formatting:
indent: 4
use_type_hints: true
line_length: 88
๐ค Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Follow PEP8 style guide
- Add tests for new functionality
- Commit changes (
git commit -m 'Add AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Open a Pull Request
๐ Project Status
โ Completed
- Hierarchical configuration system
- MySQL connector
- PostgreSQL connector
- SQLite connector (for testing)
- Configuration tests (16 tests)
- Integration tests (11 tests)
- Comprehensive documentation
๐ง In Progress
- Template integration with configuration
- Semantic column detection
- Enhanced code generation
๐ Planned
- Schema migration generation
- Relationship mapping options
- Additional database support
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐จโ๐ป Authors
- Harald Stowasser - Initial work and current maintainer - StowasserH
See also the list of contributors who participated in this project.
๐ Support
Getting Help
- Check CONFIG_QUICK_START.md for common questions
- Review CONFIGURATION_STRATEGY.md for architecture details
- Browse existing Issues
Reporting Issues
If you find a bug, please open an issue with:
- Description of the problem
- Steps to reproduce
- Your configuration (without sensitive data)
- Python and database versions
๐ Learn More
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pystoorm-0.2.0.tar.gz.
File metadata
- Download URL: pystoorm-0.2.0.tar.gz
- Upload date:
- Size: 369.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6bc571521bcfb749e579967e950ae1a5fe406b3e6ed6824eda5cbf908dfad8c
|
|
| MD5 |
46beb6502a779814becdcdac094cbb2b
|
|
| BLAKE2b-256 |
50006c848b69cce7126d653e7eb8ae5129965af5521651e785839d672a64ec0d
|
Provenance
The following attestation bundles were made for pystoorm-0.2.0.tar.gz:
Publisher:
publish.yml on StowasserH/pyStoOrm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pystoorm-0.2.0.tar.gz -
Subject digest:
f6bc571521bcfb749e579967e950ae1a5fe406b3e6ed6824eda5cbf908dfad8c - Sigstore transparency entry: 1205994822
- Sigstore integration time:
-
Permalink:
StowasserH/pyStoOrm@ab10849901ac8f8f1afe2b5bd4952f0876a4b4ea -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/StowasserH
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ab10849901ac8f8f1afe2b5bd4952f0876a4b4ea -
Trigger Event:
push
-
Statement type:
File details
Details for the file pystoorm-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pystoorm-0.2.0-py3-none-any.whl
- Upload date:
- Size: 33.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89593bfc8eaa9248acad2cfd988ca8ea4e88a7e5cf7bda8ba73a0a2503a0e12a
|
|
| MD5 |
08d18a79b07ef2e0b292873eab3dd69a
|
|
| BLAKE2b-256 |
48bcfebd5c715172f5de360ed7e2afd33acd63aae17b2d8716648f2229e758a7
|
Provenance
The following attestation bundles were made for pystoorm-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on StowasserH/pyStoOrm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pystoorm-0.2.0-py3-none-any.whl -
Subject digest:
89593bfc8eaa9248acad2cfd988ca8ea4e88a7e5cf7bda8ba73a0a2503a0e12a - Sigstore transparency entry: 1205994852
- Sigstore integration time:
-
Permalink:
StowasserH/pyStoOrm@ab10849901ac8f8f1afe2b5bd4952f0876a4b4ea -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/StowasserH
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ab10849901ac8f8f1afe2b5bd4952f0876a4b4ea -
Trigger Event:
push
-
Statement type: