dbterd.api
1import logging 2from pathlib import Path 3 4from click import Command, Context 5 6from dbterd import default 7from dbterd.core.executor import Executor 8from dbterd.helpers.log import logger 9 10 11logger.setLevel(logging.WARNING) # hide log 12 13 14class DbtErd: 15 """ 16 dbt ERD official API functions. 17 18 **Usage**: 19 20 ## Get a whole ERD 21 22 ```python 23 from dbterd.api import DbtErd 24 25 erd = DbtErd().get_erd() 26 ``` 27 28 ## Get a whole ERD given all models attached to `my_exposure_name` 29 30 ```python 31 from dbterd.api import DbtErd 32 33 erd = DbtErd(select="exposure:my_exposure_name").get_erd() 34 ``` 35 See the 36 [Selection](https://dbterd.datnguyen.de/latest/nav/guide/cli-references.html#dbterd-run-select-s) 37 page for more details. 38 39 ## Get a model (named `model.jaffle_shop.my_model`)'s ERD 40 41 ```python 42 from dbterd.api import DbtErd 43 erd = DbtErd().get_model_erd(s 44 node_fqn="model.jaffle_shop.my_model" 45 ) 46 ``` 47 """ 48 49 def __init__(self, **kwargs) -> None: 50 """Initialize the main Executor given similar input CLI parameters.""" 51 self.params: dict = kwargs 52 """ 53 Mimic CLI params with overriding `api = True`.\n 54 Check 55 [CLI reference page](https://dbterd.datnguyen.de/latest/nav/guide/cli-references.html) 56 for more details of how the params are used. 57 """ 58 self.__set_params_default_if_not_specified() 59 ctx_command = self.params.get("api_context_command") 60 self.executor: Executor = Executor(Context(Command(name=ctx_command if ctx_command else "run"))) 61 """ 62 Mimic CLI's executor.\n 63 The context command is `run` by default 64 unless specifying a param named `api_context_command` 65 """ 66 67 def __set_params_default_if_not_specified(self) -> None: 68 """Set base params' default value (mimic CLI behaviors where possible).""" 69 self.params["api"] = True 70 71 self.params["select"] = self.params.get("select", []) 72 self.params["exclude"] = self.params.get("exclude", []) 73 self.params["resource_type"] = self.params.get("resource_type", default.default_resource_types()) 74 self.params["algo"] = self.params.get("algo", default.default_algo()) 75 self.params["entity_name_format"] = self.params.get("entity_name_format", default.default_entity_name_format()) 76 self.params["omit_columns"] = self.params.get("omit_columns", False) 77 self.params["artifacts_dir"] = self.params.get("artifacts_dir", Path.cwd()) 78 self.params["target"] = self.params.get("target", default.default_target()) 79 80 def get_erd(self) -> str: 81 """ 82 Generate ERD code for a whole project. 83 84 Usage: 85 ```python 86 from dbterd.api import DbtErd 87 88 erd = DbtErd().get_erd() 89 ``` 90 91 Returns: 92 str: ERD text 93 94 """ 95 return self.executor.run(**self.params) 96 97 def get_model_erd(self, node_unique_id: str) -> str: 98 """ 99 Generate ERD code for a model. 100 101 Result contains the input model and 1 level relationship model(s) (if any). 102 103 Usage: 104 105 ```python 106 from dbterd.api import DbtErd 107 108 erd = DbtErd().get_model_erd(node_unique_id="model.jaffle_shop.my_model") 109 ``` 110 111 Args: 112 - node_unique_id (str): Manifest node unique ID 113 114 Returns: 115 str: ERD text 116 117 """ 118 return self.executor.run(node_unique_id=node_unique_id, **self.params)
class
DbtErd:
15class DbtErd: 16 """ 17 dbt ERD official API functions. 18 19 **Usage**: 20 21 ## Get a whole ERD 22 23 ```python 24 from dbterd.api import DbtErd 25 26 erd = DbtErd().get_erd() 27 ``` 28 29 ## Get a whole ERD given all models attached to `my_exposure_name` 30 31 ```python 32 from dbterd.api import DbtErd 33 34 erd = DbtErd(select="exposure:my_exposure_name").get_erd() 35 ``` 36 See the 37 [Selection](https://dbterd.datnguyen.de/latest/nav/guide/cli-references.html#dbterd-run-select-s) 38 page for more details. 39 40 ## Get a model (named `model.jaffle_shop.my_model`)'s ERD 41 42 ```python 43 from dbterd.api import DbtErd 44 erd = DbtErd().get_model_erd(s 45 node_fqn="model.jaffle_shop.my_model" 46 ) 47 ``` 48 """ 49 50 def __init__(self, **kwargs) -> None: 51 """Initialize the main Executor given similar input CLI parameters.""" 52 self.params: dict = kwargs 53 """ 54 Mimic CLI params with overriding `api = True`.\n 55 Check 56 [CLI reference page](https://dbterd.datnguyen.de/latest/nav/guide/cli-references.html) 57 for more details of how the params are used. 58 """ 59 self.__set_params_default_if_not_specified() 60 ctx_command = self.params.get("api_context_command") 61 self.executor: Executor = Executor(Context(Command(name=ctx_command if ctx_command else "run"))) 62 """ 63 Mimic CLI's executor.\n 64 The context command is `run` by default 65 unless specifying a param named `api_context_command` 66 """ 67 68 def __set_params_default_if_not_specified(self) -> None: 69 """Set base params' default value (mimic CLI behaviors where possible).""" 70 self.params["api"] = True 71 72 self.params["select"] = self.params.get("select", []) 73 self.params["exclude"] = self.params.get("exclude", []) 74 self.params["resource_type"] = self.params.get("resource_type", default.default_resource_types()) 75 self.params["algo"] = self.params.get("algo", default.default_algo()) 76 self.params["entity_name_format"] = self.params.get("entity_name_format", default.default_entity_name_format()) 77 self.params["omit_columns"] = self.params.get("omit_columns", False) 78 self.params["artifacts_dir"] = self.params.get("artifacts_dir", Path.cwd()) 79 self.params["target"] = self.params.get("target", default.default_target()) 80 81 def get_erd(self) -> str: 82 """ 83 Generate ERD code for a whole project. 84 85 Usage: 86 ```python 87 from dbterd.api import DbtErd 88 89 erd = DbtErd().get_erd() 90 ``` 91 92 Returns: 93 str: ERD text 94 95 """ 96 return self.executor.run(**self.params) 97 98 def get_model_erd(self, node_unique_id: str) -> str: 99 """ 100 Generate ERD code for a model. 101 102 Result contains the input model and 1 level relationship model(s) (if any). 103 104 Usage: 105 106 ```python 107 from dbterd.api import DbtErd 108 109 erd = DbtErd().get_model_erd(node_unique_id="model.jaffle_shop.my_model") 110 ``` 111 112 Args: 113 - node_unique_id (str): Manifest node unique ID 114 115 Returns: 116 str: ERD text 117 118 """ 119 return self.executor.run(node_unique_id=node_unique_id, **self.params)
dbt ERD official API functions.
Usage:
Get a whole ERD
from dbterd.api import DbtErd
erd = DbtErd().get_erd()
Get a whole ERD given all models attached to my_exposure_name
from dbterd.api import DbtErd
erd = DbtErd(select="exposure:my_exposure_name").get_erd()
See the Selection page for more details.
Get a model (named model.jaffle_shop.my_model)'s ERD
from dbterd.api import DbtErd
erd = DbtErd().get_model_erd(s
node_fqn="model.jaffle_shop.my_model"
)
DbtErd(**kwargs)
50 def __init__(self, **kwargs) -> None: 51 """Initialize the main Executor given similar input CLI parameters.""" 52 self.params: dict = kwargs 53 """ 54 Mimic CLI params with overriding `api = True`.\n 55 Check 56 [CLI reference page](https://dbterd.datnguyen.de/latest/nav/guide/cli-references.html) 57 for more details of how the params are used. 58 """ 59 self.__set_params_default_if_not_specified() 60 ctx_command = self.params.get("api_context_command") 61 self.executor: Executor = Executor(Context(Command(name=ctx_command if ctx_command else "run"))) 62 """ 63 Mimic CLI's executor.\n 64 The context command is `run` by default 65 unless specifying a param named `api_context_command` 66 """
Initialize the main Executor given similar input CLI parameters.
params: dict
Mimic CLI params with overriding api = True.
Check CLI reference page for more details of how the params are used.
executor: dbterd.core.executor.Executor
Mimic CLI's executor.
The context command is run by default
unless specifying a param named api_context_command
def
get_erd(self) -> str:
81 def get_erd(self) -> str: 82 """ 83 Generate ERD code for a whole project. 84 85 Usage: 86 ```python 87 from dbterd.api import DbtErd 88 89 erd = DbtErd().get_erd() 90 ``` 91 92 Returns: 93 str: ERD text 94 95 """ 96 return self.executor.run(**self.params)
Generate ERD code for a whole project.
Usage:
from dbterd.api import DbtErd
erd = DbtErd().get_erd()
Returns: str: ERD text
def
get_model_erd(self, node_unique_id: str) -> str:
98 def get_model_erd(self, node_unique_id: str) -> str: 99 """ 100 Generate ERD code for a model. 101 102 Result contains the input model and 1 level relationship model(s) (if any). 103 104 Usage: 105 106 ```python 107 from dbterd.api import DbtErd 108 109 erd = DbtErd().get_model_erd(node_unique_id="model.jaffle_shop.my_model") 110 ``` 111 112 Args: 113 - node_unique_id (str): Manifest node unique ID 114 115 Returns: 116 str: ERD text 117 118 """ 119 return self.executor.run(node_unique_id=node_unique_id, **self.params)
Generate ERD code for a model.
Result contains the input model and 1 level relationship model(s) (if any).
Usage:
from dbterd.api import DbtErd
erd = DbtErd().get_model_erd(node_unique_id="model.jaffle_shop.my_model")
Args: - node_unique_id (str): Manifest node unique ID
Returns: str: ERD text