Let's say I have two entities: Vehicle and Tariff. They are related as 1-to-many (vehicle can has many tariffs).
This is my basic types definitio...
For further actions, you may consider blocking this person and/or reporting abuse
You could do
Leveraging generics is probably the way to go.
Ok, this is good one. Thank you.
How can I optimize other fields?
I'm on mobile at the moment. I'll post some other suggestions in a bit when I'm back at my laptop. On general though, union types and generics really help with these kinds of situations to make types more maintainable.
From there you could even do a Mongoose document generic type.
You could refine this more if you wanted to make a Vehicle type which just wraps the
Entity<T>
.You can see it in action in the enhanced TypeScript playground.
Also, this might interest you.
An Enhanced TypeScript Playground
Nick Taylor
Ok, I will play with it. Thank you.
You can even go a bit further.
See the updated playground example
You could always use something like TypeORM. I haven't try it myself, as the last time I needed an ODM I used camo but I think it has been deprecated now.
I recently found typegoose, which seems promising and seems to attempt to solve this problem. Disclaimer: I haven't used it.
I will say, I'm about a year into a codebase with typescript and mongoose, and I wish I would've just started with TypeORM, and at some point I may switch. Mongoose seems like a project that's either already dead or on life-support.
Simple: Use Python!
...
Yes, I am absolutely, undeniably kidding. I've heard that joke in my head every time I read your article title, since I posted!
(Honestly, though, I know nothing about TypeScript or mongoose, so I can't make any factual or useful comparisons to Python...and anyway, even if I could, it wouldn't be useful, since you already picked the stack you need!)
I moved to Node.JS from Python :)
Hey there you can use typegoose also you can completely remove the double Model/Schema generation and even the DTOs are not required..
Example with nestjs + typegoose + mongodb :
github.com/gochev/nest-js-poc-mongodb
also I have added a blog post about that
dev.to/gochev/nestjs-mongo-typegoo...