#DTOs
1 messages · Page 1 of 1 (latest)
Only just saw the news about jolt-org/dtos, very cool!
@surreal edge maybe like this 😅
what is dtos
IT WAS NOT WORKING WHEN I TRIED IT LAST TIME
so is it like msgspec?
i dont understand it
There is actually a more basic level tutorial if that helps? https://docs.litestar.dev/latest/tutorials/dto-tutorial/index.html
maybe a crude explanation is that they just provide a clear contract for data exchange between different parts of your app. You can say "give me all fields for this schema internally, but if we are sending it as an external/public response only show me the name and email, hide the password and phone number"
(just for reference)
class Person(Struct):
name: str
age: int
email: str
@method
def get_person(name: str) -> Person:
# Your logic to retrieve the person goes here
# For demonstration purposes, a placeholder Person instance is returned
return Person(name=name, age=30, email=f"email_of_{name}@example.com")
For example, we may want to hide the user’s email address from the response.
Then we would require to create a new dataclass
without the email field
does python support something like typescript's Omit ?
Yeah
@dataclass
class Person:
name: str
age: int
email: str
address: Address
class ReadDTO(DataclassDTO[Person]):
config = DTOConfig(exclude={"email", "address.street"})
but you still need to fetch it from the db
i feel like this solves a problem ORM creates
if you dont use ORMs then this isnt a issue
the only issue is that python's typing system isn't sophisticated enough to let you write Omit<User, "email">
well yea i still dont understand this
(im not criticising your software, i dont understand, it im only trying)
all good 😄
@surreal edge is the domain expert here im sure he could explain it much better
Well its not just an ORM thing, but its any time you have a centralized set of domain models that you use to represent data internally. If you don't have that, or do that, then yeah its not an issue for you. But many do.
So, they are for manipulating a domain type for purposes of data transfer validation. That's pretty much it. And you don't need them, ever. You can always hand roll models that will do the exact same thing. This can get tedious when you do a lot of them, and that's the space these are attempting to fill.
rename Litestar Organization to Time Savers, Inc. ™
I might be missing something, but does anyone know if DTOs support sqlachemy's selectin_polymorhpic /with_polymorphic out of the box?
@wet swallow would know for sure