Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create simple json target #125

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# dbterd

Generate the ERD-as-a-code ([DBML](https://dbdiagram.io/d), [Mermaid](https://mermaid-js.github.io/mermaid-live-editor/), [PlantUML](https://plantuml.com/ie-diagram), [GraphViz](https://graphviz.org/), [D2](https://d2lang.com/), [DrawDB](https://drawdb.vercel.app/)) from dbt artifact files (`dbt Core`) or from dbt metadata (`dbt Cloud`)
Generate the ERD-as-a-code ([DBML](https://dbdiagram.io/d), [Mermaid](https://mermaid-js.github.io/mermaid-live-editor/), [PlantUML](https://plantuml.com/ie-diagram), [GraphViz](https://graphviz.org/), [D2](https://d2lang.com/), [DrawDB](https://drawdb.vercel.app/), [JSON](https://json.org/)) from dbt artifact files (`dbt Core`) or from dbt metadata (`dbt Cloud`)

Entity Relationships are configurably detected by ([docs](https://dbterd.datnguyen.de/latest/nav/guide/cli-references.html#dbterd-run-algo-a)):

Expand Down
50 changes: 50 additions & 0 deletions dbterd/adapters/targets/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import Optional, Tuple
import json

from dbterd.adapters import adapter
from dbterd.types import Catalog, Manifest



def run(manifest: Manifest, catalog: Catalog, **kwargs) -> Tuple[str, str]:
"""Parse dbt artifacts and export Mermaid file

Args:
manifest (dict): Manifest json
catalog (dict): Catalog json

Returns:
Tuple(str, str): File name and the Mermaid content
"""
output_file_name = kwargs.get("output_file_name") or "output.json"
return (output_file_name, parse(manifest, catalog, **kwargs))


def parse(manifest: Manifest, catalog: Catalog, **kwargs) -> str:
"""Get the DBML content from dbt artifacts

Args:
manifest (dict): Manifest json
catalog (dict): Catalog json

Returns:
str: JSON content
"""
algo_module = adapter.load_algo(name=kwargs["algo"])
tables, relationships = algo_module.parse(
manifest=manifest, catalog=catalog, **kwargs
)

json_data = {"tables": {}, "relationships": []}
for table in tables:
json_data["tables"][table.name] = {"columns": [{"name": x.name, "type": x.data_type} for x in table.columns]}


for rel in relationships:
json_data["relationships"].append({
"from": rel.table_map[1],
"to": rel.table_map[0],
"type": rel.type
})

return json.dumps(json_data, indent=4)
Loading