Skip to content

Commit

Permalink
Merge pull request #26 from RSS-Engineering/rename-title-to-table-name
Browse files Browse the repository at this point in the history
Renamed title to table_name for db_config
  • Loading branch information
imlocle authored Oct 25, 2024
2 parents 7c69e1d + 0187997 commit 882c74b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
"3.10",
"3.11",
"3.12",
"3.13"
]
runs-on: ubuntu-latest
env:
Expand Down
13 changes: 8 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@

# Change Log

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.0.0] - 2024-01-15

Added support for Pydantic V2 (version - ^2.5).

### Added

- Test coverage for Python versions `3.11` and `3.12`

### Changed

- Python base version from `3.7` to `3.8`.
- Changed Pydantic version from `^1.8.2` to `^2.5`.
- Updated Model validation function from `parse_obj` to `model_validate`.
- Renamed backend initialization class from `Config` to `db_config` to follow pydantic's naming convention.
- Updated method for generation of dictionary from `dict` to `model_dump`.
- Renamed `db_config.title` to `db_config.table_name`.

## [0.4.2] - 2023-11-16

Added count() for dynamo backend that returns integer count as total.
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ class User(BaseModel):
id: int
name: str

class Config:
title = 'User'
class db_config:
table_name = 'User'
backend = DynamoDbBackend
hash_key = 'id'
```

First, use the `BaseModel` from `pydanticrud` instead of `pydantic`.

Next add your backend to your model's `Config` class. PydantiCRUD is geared
Next add your backend to your model's `db_config` class. PydantiCRUD is geared
toward DynamoDB but provides SQLite for lighter-weight usage. You can provide
your own if you like.

Finally, add appropriate members to the `Config` class for the chosen backend.
Finally, add appropriate members to the `db_config` class for the chosen backend.

## Methods

Expand Down Expand Up @@ -56,25 +56,26 @@ NOTE: Rule complexity is limited by the querying capabilities of the backend.
### DynamoDB

`get(key: Union[Dict, Any])`
- `key` can be any of 3 types:
-
- in the case of a single hash_key, a value of type that matches the hash_key
- in the case of a hash and range key, a tuple specifying the respective values
- a dictionary of the hash and range keys with their names and values. This method can pull for alternate indexes.

- `key` can be any of 3 types:
- in the case of a single hash_key, a value of type that matches the hash_key
- in the case of a hash and range key, a tuple specifying the respective values
- a dictionary of the hash and range keys with their names and values. This method can pull for alternate indexes.

`query(query_expr: Optional[Rule], filter_expr: Optional[Rule], limit: Optional[str], exclusive_start_key: Optional[tuple[Any]], order: str = 'asc'`
- Providing a `query_expr` parameter will try to apply the keys of the expression to an

- Providing a `query_expr` parameter will try to apply the keys of the expression to an
existing index.
- Providing a `filter_expr` parameter will filter the results of
- Providing a `filter_expr` parameter will filter the results of
a passed `query_expr` or run a dynamodb `scan` if no `query_expr` is passed.
- An empty call to `query()` will return the scan results (and be resource
- An empty call to `query()` will return the scan results (and be resource
intensive).
- Providing a `limit` parameter will limit the number of results. If more results remain, the returned dataset will have an `last_evaluated_key` property that can be passed to `exclusive_start_key` to continue with the next page.
- Providing `order='desc'` will return the result set in descending order. This is not available for query calls that "scan" dynamodb.
- Providing a `limit` parameter will limit the number of results. If more results remain, the returned dataset will have an `last_evaluated_key` property that can be passed to `exclusive_start_key` to continue with the next page.
- Providing `order='desc'` will return the result set in descending order. This is not available for query calls that "scan" dynamodb.

`count(query_expr: Optional[Rule], exclusive_start_key: Optional[tuple[Any]], order: str = 'asc'`
- Same as `query` but returns an integer count as total. (When calling `query` with a limit, the count dynamodb returns is <= the limit you provide)

- Same as `query` but returns an integer count as total. (When calling `query` with a limit, the count dynamodb returns is <= the limit you provide)

## Backend Configuration Members

Expand Down
8 changes: 7 additions & 1 deletion pydanticrud/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from warnings import warn
from pydantic import BaseModel as PydanticBaseModel
from pydantic._internal._model_construction import ModelMetaclass

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

@classmethod
def get_table_name(cls) -> str:
return cls.db_config.title.lower()
warn("'title' has been renamed to 'table_name'.", DeprecationWarning)
return (
cls.db_config.table_name.lower()
if cls.db_config.table_name.lower()
else cls.db_config.title.lower() # Will be deprecated
)

@classmethod
def exists(cls) -> bool:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SimpleKeyModel(BaseModel):
hash: UUID

class db_config:
title = "ModelTitle123"
table_name = "ModelTitle123"
hash_key = "name"
ttl = "expires"
backend = DynamoDbBackend
Expand All @@ -57,7 +57,7 @@ def type_from_typ(cls, values):
return values

class db_config:
title = "AliasTitle123"
table_name = "AliasTitle123"
hash_key = "name"
backend = DynamoDbBackend
endpoint = "http://localhost:18002"
Expand All @@ -73,7 +73,7 @@ class ComplexKeyModel(BaseModel):
body: str = "some random string"

class db_config:
title = "ComplexModelTitle123"
table_name = "ComplexModelTitle123"
hash_key = "account"
range_key = "sort_date_key"
backend = DynamoDbBackend
Expand Down Expand Up @@ -103,7 +103,7 @@ class NestedModel(BaseModel):
other: Union[Ticket, SomethingElse]

class db_config:
title = "NestedModelTitle123"
table_name = "NestedModelTitle123"
hash_key = "account"
range_key = "sort_date_key"
backend = DynamoDbBackend
Expand Down
4 changes: 2 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Model(BaseModel):
total: float

class db_config:
title = "ModelTitle123"
table_name = "ModelTitle123"
backend = FalseBackend


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


def test_model_table_name_from_title():
assert Model.get_table_name() == Model.db_config.title.lower()
assert Model.get_table_name() == Model.db_config.table_name.lower()
2 changes: 1 addition & 1 deletion tests/test_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Model(BaseModel):
items: List[int]

class db_config:
title = "ModelTitle123"
table_name = "ModelTitle123"
hash_key = "id"
backend = SqliteBackend
database = ":memory:"
Expand Down

0 comments on commit 882c74b

Please sign in to comment.