Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve EndpointCreator flexibility by adding methods to be overriden #80

Closed

Conversation

JakNowy
Copy link
Contributor

@JakNowy JakNowy commented May 9, 2024

This is general demonstration of my concept of EndpointCreator changes discussed briefly in #79 (comment)

It hopefully helps to draw some general concept of FastCRUD-EndpointCreator responsibilities.

@JakNowy JakNowy force-pushed the feat/endpoint-creator-overrides branch 2 times, most recently from 6d5de93 to 3a78972 Compare May 9, 2024 19:34
def _create_item(self):
"""Creates an endpoint for creating items in the database."""

async def endpoint(
db: AsyncSession = Depends(self.session),
item: self.create_schema = Body(...), # type: ignore
):
unique_columns = _extract_unique_columns(self.model)
Copy link
Contributor Author

@JakNowy JakNowy May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this exists check - it generates a number of database calls. In fact, I just tested that every "unique" field in SQLAlchemy introduces an index on that field, so the responses are immediate. Anyway, I still find it sufficient to just handle generic IntegrityError exception as presented.

@@ -249,9 +269,13 @@ def _read_item(self):

@apply_model_pk(**self._primary_keys_types)
async def endpoint(db: AsyncSession = Depends(self.session), **pkeys):
item = await self.crud.get(db, **pkeys)
if stmt := self._base_read_statement():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way, while we're trying to make FastCRUD as useful as possible, we're not forcing anyone to use either FastCRUD or EndpointCreator. The latter may be used with raw SQLAlchemy querries and the former can be used in your usual single FastAPI endpoints. And of course you will probably get most by combining the 2 features, but you're never going into a situation where you're limited by the library with something you could achieve with raw SQLAlchemy.

We could do the same with other methods, especially to easily implement complex joins and filters.

@JakNowy JakNowy force-pushed the feat/endpoint-creator-overrides branch from 3a78972 to d4a34df Compare May 9, 2024 19:37
@JakNowy JakNowy closed this May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant