bedrock.model

Bedrock expects you to have a model directory where you stick your DB classes in.

bedrock init should've taken care of that for you.

Example

Let's define City model in model/city.py:

from sqlalchemy import Column, DECIMAL, ForeignKey, String
from sqlalchemy.dialects.postgresql import UUID, ARRAY
from bedrock.db.model_helper import Base, ModelHelper
from bedrock.model.decorators import filter_path_hint
from model.region import Region  # Let's assume we have a Region model defined in model/region.py

@filter_path_hint("self->Region->Country.country_code") # Path hint to how you can protect this entity. In this case, access is granted based on the country code, so Bedrock needs to know how to get to country_code from the city.
class City(Base, ModelHelper):
    #       └─ Base already gives every model a UUID as the primary key.
    #          If you want a different setup you should define a different Base class.

    # Name of the table in the DB
    __tablename__ = "bedrock_cities"

    # A foreign key to the region table.
    region_uuid = Column(UUID(as_uuid=True), ForeignKey("bedrock_regions.uuid", ondelete="cascade"), nullable=False)
    # Some other columns
    name = Column(String(255), nullable=False)
    lat = Column(DECIMAL, nullable=False)
    lon = Column(DECIMAL, nullable=False)
    tags = Column(ARRAY(String), nullable=True, default=[])

    # A relationship to the region table, so that we can easily access the region of a city.
    region = relationship(Region)
 1"""
 2Bedrock expects you to have a `model` directory where you stick your DB classes in.
 3
 4`bedrock init` should've taken care of that for you.
 5
 6# Example
 7
 8Let's define `City` model in `model/city.py`:
 9
10```python
11from sqlalchemy import Column, DECIMAL, ForeignKey, String
12from sqlalchemy.dialects.postgresql import UUID, ARRAY
13from bedrock.db.model_helper import Base, ModelHelper
14from bedrock.model.decorators import filter_path_hint
15from model.region import Region  # Let's assume we have a Region model defined in model/region.py
16
17@filter_path_hint("self->Region->Country.country_code") # Path hint to how you can protect this entity. In this case, access is granted based on the country code, so Bedrock needs to know how to get to country_code from the city.
18class City(Base, ModelHelper):
19    #       └─ Base already gives every model a UUID as the primary key.
20    #          If you want a different setup you should define a different Base class.
21
22    # Name of the table in the DB
23    __tablename__ = "bedrock_cities"
24
25    # A foreign key to the region table.
26    region_uuid = Column(UUID(as_uuid=True), ForeignKey("bedrock_regions.uuid", ondelete="cascade"), nullable=False)
27    # Some other columns
28    name = Column(String(255), nullable=False)
29    lat = Column(DECIMAL, nullable=False)
30    lon = Column(DECIMAL, nullable=False)
31    tags = Column(ARRAY(String), nullable=True, default=[])
32
33    # A relationship to the region table, so that we can easily access the region of a city.
34    region = relationship(Region)
35```
36"""