Skip to main content

YAMLForge

YAMLForge is a library designed to automatically generate SQLModel models and shcemas from a configuration in YAML format or a dictionary. It simplifies the creation of models, relationships (OneToOne, OneToMany, ManyToMany), and link tables for relational databases. Additionally, it automatically generates model files and an __init__.py file to facilitate importing the models.


Main Features

  • Automatic model generation: Define your models in a YAML file or dictionary, and YAMLForge will generate the corresponding .py files.
  • Automatic schema generation: Use the models defined in a YAML file or dictionary, and YAMLForge will generate the corresponding schemas/DTOs.
  • Supported relationships:
    • OneToOne: One-to-one relationships between models.
    • OneToMany: One-to-many relationships between models.
    • ManyToMany: Many-to-many relationships with automatic link tables.
  • Customizable fields: Define fields with options such as primary_key, required, index, unique, etc.
  • Automatic import generation: Automatically includes the necessary imports for relationships and models.
  • Generated __init__.py file: Creates an __init__.py file to simplify model imports.

Installation

To use YAMLForge, simply install it with pip:

pip install yamlforge

Basic Usage

1. Define Your Models in YAML

Create a YAML file with your model configuration. Here’s an example:

User:
  fields:
    - name: id
      type: int
      primary_key: True
    - name: name
      type: str
      required: True
  relationships:
    - type: OneToOne
      model: Profile
      back_populates: True

Profile:
  fields:
    - name: id
      type: int
      primary_key: True
    - name: bio
      type: str
  relationships:
    - type: OneToOne
      model: User
      back_populates: True

2. Generate Models from YAML

Use YAMLForge to generate the model files from the YAML file or objects:

from yamlforge import generate

# generate models and schemas
generate(models_config)

# Or
from yamlforge import generate_models, generate_schemas

generate_models(config_path="yamlforge.ymal")
generate_schemas(config_path="yamlforge.ymal")

3. Generated Structure

YAMLForge will generate the following files:

db/
  models/
    user.py
    profile.py
    __init__.py
schemas/
  user.py
  profile.py

Relationships

OneToOne

User:
  fields:
    - name: id
      type: int
      primary_key: True
    - name: name
      type: str
      required: True
  relationships:
    - type: OneToOne
      model: Profile
      back_populates: True

Profile:
  fields:
    - name: id
      type: int
      primary_key: True
    - name: bio
      type: str
  relationships:
    - type: OneToOne
      model: User
      back_populates: True

OneToMany

Team:
  fields:
    - name: id
      type: int
      primary_key: True
    - name: name
      type: str
      required: True
  relationships:
    - type: OneToMany
      model: Hero
      back_populates: True

Hero:
  fields:
    - name: id
      type: int
      primary_key: True
    - name: name
      type: str
      required: True

ManyToMany

Post:
  fields:
    - name: id
      type: int
      primary_key: True
    - name: title
      type: str
      required: True
  relationships:
    - type: ManyToMany
      model: Category
      back_populates: True

Category:
  fields:
    - name: id
      type: int
      primary_key: True
    - name: name
      type: str
      required: True
  relationships:
    - type: ManyToMany
      model: Post
      back_populates: True

Don'ts

1. Do Not Define Relationships in Both Models

If you define a relationship in both models, an extra model with additional attributes will be generated in both directions.

Incorrecto

Team:
  ---
  relationships:
    - type: OneToMany
      model: Hero
Hero:
  ---
  relationships:
    - type: OneToMany
      model: Team

Correcto

Team:
  ---
  relationships:
    - type: OneToMany
      model: Hero
Hero:
  ---

YAMLForge automatically generates link tables for ManyToMany relationships. Do not attempt to manually define the link table.

Incorrecto

Post:
  relationships:
    - type: ManyToMany
      model: Category
Category:
  ---
PostCategoryLink: 
  fields:
    - name: post_id
      type: int
    - name: category_id
      type: int

Correcto

Post:
  relationships:
    - type: ManyToMany
      model: Category
Category:
  ---

Metadata

Release files for yamlforge 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yamlforge 0.1.2
File Size Uploaded Provenance
yamlforge-0.1.2.tar.gz 8.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for yamlforge 0.1.2
File Interpreter ABI Platform
yamlforge-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / yamlforge-0.1.2.tar.gz

Download URL yamlforge-0.1.2.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e014e62b5fb59972785bbcf77dc35a7359f31b4db4da7b0e6b5f23ee4a20ca81
BLAKE2b-256 checksum
How to use checksums
346d1a9473d7c808007f8d0586c39ac2023efa553d000db0e81de7f236c22b72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.26

Release files / yamlforge-0.1.2-py3-none-any.whl

Download URL yamlforge-0.1.2-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
603b6d266ce091f53f64c432bcd7f968d7b7dde50e319a24c4c73a702914cace
BLAKE2b-256 checksum
How to use checksums
c5ecb00b1be719a7f55b08fe138bde367653dc68bdae582bbef25ad1eb1e084f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.26

Security for yamlforge 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page