proofit404 / stories

Service objects designed with OOP in mind.
https://proofit404.github.io/stories/
BSD 2-Clause "Simplified" License
297 stars 21 forks source link

Any examples when i need to return a database object from a story? #714

Closed al-stefanitsky-mozdor closed 2 years ago

al-stefanitsky-mozdor commented 2 years ago

Hi!

I need some example or advise, what i could to do, when i need to return an actual database object from a story.

For example, i have a story that authenticates a user with credentials (username and password, for example). Then, i need to return an object, that can be: Django / SQLAlchemy / Pewee or another ORM object.

So, i need to write it like "Any" type and just return it from repository -> story -> execution scope OR should i map it from ORM to a new object before returning from the repository to the story and then select it again from the database, when the story ends? For example: select from db -> map to ORM -> map from ORM to a dataclass -> return to the story -> set to a state from the story -> access user's id from the state outside the story after it ends -> select it again from the db?

Sorry, if i misinterpreted, i can try explain the problem again.

al-stefanitsky-mozdor commented 2 years ago

The main problem is that i want to reduce db queries and do it only once.

proofit404 commented 2 years ago

Good morning,

I can't believe I missed this ticket. That was unintentional.

Usually, I try to avoid usage of model instances in business process state.

I prefer to use database model as a query language and migration tool to manage schema.

There is an option to return data structure instead of model instance on every ORM I'm aware of.

If I need to have a logic related to returned data structure, I wrap it in entity class (protected by generics library).

A complete guide how to do this could be found here: https://github.com/proofit404/stories/issues/474

Have a good day :palm_tree: :cocktail:

Best regards, Artem.

al-stefanitsky-mozdor commented 2 years ago

Thank you!