Skip to content

Commit

Permalink
Introduced DB Config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishmeet Bindra committed Jan 11, 2024
1 parent db25646 commit f5fb9e7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pydanticrud/backends/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def __init__(self, cls, result, serialized_items):

class Backend:
def __init__(self, cls):
cfg = cls.model_config
cfg = cls.db_config
self.cls = cls
self.schema = cls.schema()
self.hash_key = cfg.get("hash_key")
Expand Down
6 changes: 3 additions & 3 deletions pydanticrud/backends/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_column_data(field_type):

class Backend:
def __init__(self, cls):
cfg = cls.model_config
cfg = cls.db_config
self.hash_key = cfg.get("hash_key")
self.table_name = cls.get_table_name()

Expand Down Expand Up @@ -162,8 +162,8 @@ def get(self, item_key):

def save(self, item, condition: Optional[Rule] = None) -> bool:
table_name = item.get_table_name()
hash_key = item.model_config.get("hash_key")
key = item.model_config.get("hash_key")
hash_key = item.db_config.get("hash_key")
key = item.db_config.get("hash_key")
fields = tuple(self._columns.keys())

item_data = item.dict()
Expand Down
6 changes: 3 additions & 3 deletions pydanticrud/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
class CrudMetaClass(ModelMetaclass):
def __new__(mcs, name, bases, namespace, **kwargs):
cls = super().__new__(mcs, name, bases, namespace, **kwargs)
if hasattr(cls, "model_config") and "backend" in cls.model_config:
cls.__backend__ = cls.model_config["backend"](cls)
if hasattr(cls, "db_config") and "backend" in cls.db_config:
cls.__backend__ = cls.db_config["backend"](cls)
return cls


Expand Down Expand Up @@ -43,7 +43,7 @@ def initialize(cls):

@classmethod
def get_table_name(cls) -> str:
return cls.model_config.get("title").lower()
return cls.db_config.get("title").lower()

@classmethod
def exists(cls) -> bool:
Expand Down
14 changes: 7 additions & 7 deletions tests/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SimpleKeyModel(BaseModel):
data: Dict[int, int] = {}
items: List[int]
hash: UUID
model_config = ConfigDict(title="ModelTitle123", hash_key="name", ttl="expires", backend=DynamoDbBackend, endpoint="http://localhost:18002", global_indexes={"by-id": ("id",)})
db_config = ConfigDict(title="ModelTitle123", hash_key="name", ttl="expires", backend=DynamoDbBackend, endpoint="http://localhost:18002", global_indexes={"by-id": ("id",)})


class AliasKeyModel(BaseModel):
Expand All @@ -48,7 +48,7 @@ def type_from_typ(cls, values):
if 'typ' in values:
values['type'] = values.pop('typ')
return values
model_config = ConfigDict(title="AliasTitle123", hash_key="name", backend=DynamoDbBackend, endpoint="http://localhost:18002")
db_config = ConfigDict(title="AliasTitle123", hash_key="name", backend=DynamoDbBackend, endpoint="http://localhost:18002")


class ComplexKeyModel(BaseModel):
Expand All @@ -59,7 +59,7 @@ class ComplexKeyModel(BaseModel):
notification_id: str
thread_id: str
body: str = "some random string"
model_config = ConfigDict(title="ComplexModelTitle123", hash_key="account", range_key="sort_date_key", backend=DynamoDbBackend, endpoint="http://localhost:18002", local_indexes={
db_config = ConfigDict(title="ComplexModelTitle123", hash_key="account", range_key="sort_date_key", backend=DynamoDbBackend, endpoint="http://localhost:18002", local_indexes={
"by-category": ("account", "category_id"),
"by-notification": ("account", "notification_id"),
"by-thread": ("account", "thread_id")
Expand All @@ -82,7 +82,7 @@ class NestedModel(BaseModel):
expires: str
ticket: Optional[Ticket]
other: Union[Ticket, SomethingElse]
model_config = ConfigDict(title="NestedModelTitle123", hash_key="account", range_key="sort_date_key", backend=DynamoDbBackend, endpoint="http://localhost:18002")
db_config = ConfigDict(title="NestedModelTitle123", hash_key="account", range_key="sort_date_key", backend=DynamoDbBackend, endpoint="http://localhost:18002")


def alias_model_data_generator(**kwargs):
Expand Down Expand Up @@ -231,7 +231,7 @@ def complex_query_data(complex_table):
yield data
finally:
for datum in data:
ComplexKeyModel.delete((datum[ComplexKeyModel.model_config.get("hash_key")], datum[ComplexKeyModel.model_config.get("range_key")]))
ComplexKeyModel.delete((datum[ComplexKeyModel.db_config.get("hash_key")], datum[ComplexKeyModel.db_config.get("range_key")]))


@pytest.fixture(scope="module")
Expand All @@ -258,7 +258,7 @@ def nested_query_data(nested_table):
yield data
finally:
for datum in data:
NestedModel.delete((datum[NestedModel.model_config.get("hash_key")], datum[NestedModel.model_config.get("range_key")]))
NestedModel.delete((datum[NestedModel.db_config.get("hash_key")], datum[NestedModel.db_config.get("range_key")]))


@pytest.fixture
Expand All @@ -271,7 +271,7 @@ def nested_query_data_empty_ticket(nested_table):
yield data
finally:
for datum in data:
NestedModel.delete((datum[NestedModel.model_config.get("hash_key")], datum[NestedModel.model_config.get("range_key")]))
NestedModel.delete((datum[NestedModel.db_config.get("hash_key")], datum[NestedModel.db_config.get("range_key")]))


def test_save_get_delete_simple(dynamo, simple_table):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Model(BaseModel):
id: int
name: str
total: float
model_config = ConfigDict(title="ModelTitle123", backend=FalseBackend)
db_config = ConfigDict(title="ModelTitle123", backend=FalseBackend)


def test_model_has_backend_methods():
Expand Down Expand Up @@ -55,4 +55,4 @@ def test_model_backend_query():


def test_model_table_name_from_title():
assert Model.get_table_name() == Model.model_config.get("title").lower()
assert Model.get_table_name() == Model.db_config.get("title").lower()
2 changes: 1 addition & 1 deletion tests/test_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Model(BaseModel):
enabled: bool
data: Dict[str, str]
items: List[int]
model_config = ConfigDict(title="ModelTitle123", hash_key="id", backend=SqliteBackend, database=":memory:")
db_config = ConfigDict(title="ModelTitle123", hash_key="id", backend=SqliteBackend, database=":memory:")


@pytest.fixture()
Expand Down

0 comments on commit f5fb9e7

Please sign in to comment.