dbterd.api

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

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.adapters.base.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:
86    def get_erd(self) -> str:
87        """Generate ERD code for a whole project
88
89        Usage:
90        ```python
91        from dbterd.api import DbtErd
92        erd = DbtErd().get_erd()
93        ```
94
95        Returns:
96            str: ERD text
97        """
98        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:
100    def get_model_erd(self, node_unique_id: str) -> str:
101        """Generate ERD code for a model.
102
103        Result contains the input model and 1 level relationship model(s) (if any).
104
105        Usage:
106
107            ```python
108            from dbterd.api import DbtErd
109            erd = DbtErd().get_model_erd(
110                node_unique_id="model.jaffle_shop.my_model"
111            )
112            ```
113
114        Args:
115            - node_unique_id (str): Manifest node unique ID
116
117        Returns:
118            str: ERD text
119        """
120        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