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.2.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.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: yamlforge-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 e014e62b5fb59972785bbcf77dc35a7359f31b4db4da7b0e6b5f23ee4a20ca81
MD5 f0c947783e3931c3d18a95e23e885926
BLAKE2b-256 346d1a9473d7c808007f8d0586c39ac2023efa553d000db0e81de7f236c22b72

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yamlforge-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 603b6d266ce091f53f64c432bcd7f968d7b7dde50e319a24c4c73a702914cace
MD5 f27755e60fefc8985899708920a58e08
BLAKE2b-256 c5ecb00b1be719a7f55b08fe138bde367653dc68bdae582bbef25ad1eb1e084f

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