Skip to main content

Database-first code generator for any language, framework, and pattern

Project description

pyStoOrm - ORM Code Generator

Tests Python License

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:

  1. Defaults (built-in, no config needed)
  2. 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:


๐Ÿ“– 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:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Follow PEP8 style guide
  4. Add tests for new functionality
  5. Commit changes (git commit -m 'Add AmazingFeature')
  6. Push to branch (git push origin feature/AmazingFeature)
  7. 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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pystoorm-0.2.0.tar.gz (369.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pystoorm-0.2.0-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

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

Hashes for pystoorm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f6bc571521bcfb749e579967e950ae1a5fe406b3e6ed6824eda5cbf908dfad8c
MD5 46beb6502a779814becdcdac094cbb2b
BLAKE2b-256 50006c848b69cce7126d653e7eb8ae5129965af5521651e785839d672a64ec0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pystoorm-0.2.0.tar.gz:

Publisher: publish.yml on StowasserH/pyStoOrm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for pystoorm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89593bfc8eaa9248acad2cfd988ca8ea4e88a7e5cf7bda8ba73a0a2503a0e12a
MD5 08d18a79b07ef2e0b292873eab3dd69a
BLAKE2b-256 48bcfebd5c715172f5de360ed7e2afd33acd63aae17b2d8716648f2229e758a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pystoorm-0.2.0-py3-none-any.whl:

Publisher: publish.yml on StowasserH/pyStoOrm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page