Skip to main content

YAMLForge is a library designed to automatically generate SQLModel models and shcemas from a configuration in YAML format or a dictionary.

Project description

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:
  ---

2. Do Not Manually Declare the Link Table in a ManyToMany Relationship

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:
  ---

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

yamlforge-0.1.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

yamlforge-0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file yamlforge-0.1.1.tar.gz.

File metadata

  • Download URL: yamlforge-0.1.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.26

File hashes

Hashes for yamlforge-0.1.1.tar.gz
Algorithm Hash digest
SHA256 013d2c3e52d0099ba0d7dd01d5ed0281d5f680f72a852a39c796d1f2d1d34720
MD5 2b243ddb2de288f3f04a95592b20710e
BLAKE2b-256 4d34e97776f1f649ca6eda28d762707df2d3ffcfa96d21c37ac004a8078d9b88

See more details on using hashes here.

File details

Details for the file yamlforge-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: yamlforge-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.26

File hashes

Hashes for yamlforge-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c69a1e4767f2f4c9e0d6e7165a6295cec7c20c5932d96cc3f77d3a66978b1a23
MD5 90664d687ca388809e0e7161e5a9d419
BLAKE2b-256 222ea5a1734c68f1c3c7f590d5e866e8a34e845f2f5ad4c9104f76eada8eda6c

See more details on using hashes here.

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