Giter Site home page Giter Site logo

tedivm / paracelsus Goto Github PK

View Code? Open in Web Editor NEW
89.0 2.0 9.0 112 KB

Visualize SQLAlchemy Databases using Mermaid or Dot Diagrams.

Home Page: https://pypi.org/project/paracelsus/

License: MIT License

Makefile 10.91% Python 89.09%
documentation-tool graphviz mermaid mermaid-diagrams sqlalchemy

paracelsus's Introduction

Paracelsus

Paracelsus generates Entity Relationship Diagrams by reading your SQLAlchemy models.

Features

  • ERDs can be injected into documentation as Mermaid Diagrams.
  • Paracelsus can be run in CICD to check that databases are up to date.
  • ERDs can be created as files in either Dot or Mermaid format.
  • DOT files can be used to generate SVG or PNG files, or edited in GraphViz or other editors.

Usage

Installation

The paracelsus package should be installed in the same environment as your code, as it will be reading your SQLAlchemy base class to generate the diagrams.

pip install paracelsus

Basic CLI Usage

Paracelsus is primarily a CLI application.

paracelsus --help

It has three commands:

  • version outputs the version of the currently installed paracelsus cli.
  • graph generates a graph and outputs it to stdout.
  • inject inserts the graph into a markdown file.

Importing Models

SQLAlchemy models have to be imported before they are put into the model registry inside of the base class. This is similar to how Alembic needs models to be imported in order to generate migrations.

The --import-module flag can be used to import any python module, which presumably will include one or more SQLAlchemy models inside of it.

paracelsus graph example_app.models.base:Base \
  --import-module "example_app.models.users" \
  --import-module "example_app.models.posts" \
  --import-module "example_app.models.comments"

The :* modify can be used to specify that a wild card import should be used. Make sure to wrap the module name in quotes when using this to prevent shell expansion.

paracelsus graph example_app.models.base:Base --import-module "example_app.models:*"

This is equivalent to running this style of python import:

from example_app.models import *

Generate Mermaid Diagrams

paracelsus graph example_app.models.base:Base --import-module "example_app.models:*"

erDiagram
 users {
  CHAR(32) id PK
  DATETIME created
  VARCHAR(100) display_name "nullable"
 }

 posts {
  CHAR(32) id PK
  CHAR(32) author FK
  TEXT content "nullable"
  DATETIME created
  BOOLEAN live "nullable"
 }

 comments {
  CHAR(32) id PK
  CHAR(32) author FK
  CHAR(32) post FK "nullable"
  TEXT content "nullable"
  DATETIME created
  BOOLEAN live "nullable"
 }

 users ||--o{ posts : author
 posts ||--o{ comments : post
 users ||--o{ comments : author

When run through a Mermaid viewer, such as the ones installed in the markdown viewers of many version control systems, this will turn into a graphic.

erDiagram
 users {
  CHAR(32) id PK
  DATETIME created
  VARCHAR(100) display_name "nullable"
 }

 posts {
  CHAR(32) id PK
  CHAR(32) author FK
  TEXT content "nullable"
  DATETIME created
  BOOLEAN live "nullable"
 }

 comments {
  CHAR(32) id PK
  CHAR(32) author FK
  CHAR(32) post FK "nullable"
  TEXT content "nullable"
  DATETIME created
  BOOLEAN live "nullable"
 }

 users ||--o{ posts : author
 posts ||--o{ comments : post
 users ||--o{ comments : author

Inject Mermaid Diagrams

Mermaid Diagrams and Markdown work extremely well together, and it's common to place diagrams inside of project documentation. Paracelsus can be used to inject diagrams directly into markdown configuration. It does so by looking for specific tags and placing a code block inside of them, replacing any existing content between the tags.

## Schema
<!-- BEGIN_SQLALCHEMY_DOCS -->

<!-- END_SQLALCHEMY_DOCS -->

paracelsus inject db/README.md example_app.models.base:Base --import-module "example_app.models:*"

The --check flag can be used to see if the command would make any changes. If the file is already up to date then it will return a status code of 0, otherwise it will return 1 if changes are needed. This is useful in CI/CD or precommit hook to enforce that documentation is always current.

paracelsus inject db/README.md example_app.models.base:Base --import-module "example_app.models:*" --check

Creating Images

GraphViz has a command line tool named dot that can be used to turn dot graphs into images.

To create an SVG file:

paracelsus graph example_app.models.base:Base --import-module "example_app.models:*" --format dot | dot -Tsvg > output.svg

To create a PNG file:

paracelsus graph example_app.models.base:Base --import-module "example_app.models:*" --format dot | dot -Tpng > output.png

Alt text

pyproject.toml

The settings for your project can be saved directly in the pyprojects.toml file of your project.

[tool.paracelsus]
base = "example.base:Base"
imports = [
  "example.models"
]

Sponsorship

This project is developed by Robert Hafner If you find this project useful please consider sponsoring me using Github!

Github Sponsorship

paracelsus's People

Contributors

dependabot[bot] avatar justinellison avatar justintime avatar tedivm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

paracelsus's Issues

How to run the cli when I don't have a package/module set up?

I have a python setup where I've got some relatively simple scripts that interact with a fairly complex database that I've wrapped with SQLAlchemy. Here's the structure:

working dir with script1.py, script2.py
|--- db/
|    |--- __init__.py (empty)
|    |--- models.py

No matter what I've tried to pass to paracelsus on the command line, I can't get it to load my base class. Any ideas on what I can do to use this tool?

Current typing format blocks python < 3.10.x

There is some type hinting in pyproject.py that uses the | operand, which wasn't introduced until 3.10.

Traceback (most recent call last):
  File "/redacted/bin/paracelsus", line 5, in <module>
    from paracelsus.cli import app
  File "/redacted/lib/python3.8/site-packages/paracelsus/cli.py", line 11, in <module>
    from .pyproject import get_pyproject_settings
  File "/redacted/lib/python3.8/site-packages/paracelsus/pyproject.py", line 11, in <module>
    def get_pyproject_settings(dir: Path = Path(os.getcwd())) -> Dict[str, Any] | None:
TypeError: unsupported operand type(s) for |: '_GenericAlias' and 'NoneType'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.