Skip to content

Commit

Permalink
feat: add inspiration section to README and update TODO
Browse files Browse the repository at this point in the history
- Added an "Inspiration" section to the README for references.
- Expanded the TODO list with new tasks for improvements.
- Utilized `pydash` for converting camel case to snake case, enhancing compatibility.
- Ensured the automatic table name generation handles camel case, improving usability for model names like "LLMCache".

Generated-by: aiautocommit
  • Loading branch information
iloveitaly committed Nov 20, 2024
1 parent 37a51d0 commit b11fb09
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 39 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ This package provides a thin wrapper around SQLModel that provides a more Active

## Related Projects

* https://github.com/woofz/sqlmodel-basecrud
* https://github.com/woofz/sqlmodel-basecrud

## Inspiration

* https://github.com/peterdresslar/fastapi-sqlmodel-alembic-pg
* [Albemic instructions](https://github.com/fastapi/sqlmodel/pull/899/files)
* https://github.com/fastapiutils/fastapi-utils/
*
5 changes: 5 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ Docs are bad:
- Select on joined data
- One-to-one and one-to-many at the same time
- Comments on columns + tables https://github.com/tiangolo/sqlmodel/issues/492

TODO

- [ ] snake case for attributes https://github.com/sqlalchemy/sqlalchemy/issues/7149
- [ ] foreign key names https://github.com/fastapi/sqlmodel/discussions/1213
21 changes: 15 additions & 6 deletions activemodel/base_model.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import json
import typing as t

import pydash
import sqlalchemy as sa
from sqlalchemy.orm import declared_attr
from sqlmodel import SQLModel

from activemodel.utils.camelcase import camel2snake
from sqlmodel import Session, SQLModel

from .query_wrapper import QueryWrapper

Expand All @@ -17,7 +16,13 @@ class BaseModel(SQLModel):
https://github.com/woofz/sqlmodel-basecrud/blob/main/sqlmodel_basecrud/basecrud.py
"""

# TODO implement delete
# TODO implement actually calling these hooks

def before_delete(self):
pass

def after_delete(self):
pass

def before_save(self):
pass
Expand All @@ -31,15 +36,19 @@ def before_update(self):
def after_update(self):
pass

# TODO snake case tables automatically
@declared_attr
def __tablename__(cls) -> str:
"""
Automatically generates the table name for the model by converting the class name from camel case to snake case.
By default, the class is lower cased which makes it harder to read.
Many snake_case libraries struggle with snake case for names like LLMCache, which is why we are using a more
complicated implementation from pydash.
https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case
"""
return camel2snake(cls.__name__)
return pydash.strings.snake_case(cls.__name__)

@classmethod
def select(cls, *args):
Expand Down
29 changes: 0 additions & 29 deletions activemodel/utils/camelcase.py

This file was deleted.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ version = "0.2.0"
description = "Make SQLModel more like an a real ORM"
readme = "README.md"
requires-python = ">=3.10"
dependencies = ["sqlmodel>=0.0.22"]
dependencies = [
"pydash>=8.0.4",
"sqlmodel>=0.0.22",
]
authors = [{ name = "Michael Bianco", email = "[email protected]" }]
keywords = ["sqlmodel", "orm", "activerecord", "activemodel", "sqlalchemy"]
urls = { "Repository" = "https://github.com/iloveitaly/activemodel" }
Expand Down
5 changes: 5 additions & 0 deletions test/table_name_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ class TestTable(BaseModel):
id: int


class TestLLMCache(BaseModel):
id: int


def test_table_name():
assert TestTable.__tablename__ == "test_table"
assert TestLLMCache.__tablename__ == "test_llm_cache"
20 changes: 18 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b11fb09

Please sign in to comment.