RFC - Typescript support for entities #558
Replies: 5 comments 4 replies
-
I was kind of wondering about the complexity of |
Beta Was this translation helpful? Give feedback.
-
Would it makes sense to create decorators for readonly properties and classes? With this you only have to annotate readonly properties and everything else is Updateable by default? With this you don't have to create wrapper types for Actionable etc Or maybe even just use the Typescript ReadOnly type https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype |
Beta Was this translation helpful? Give feedback.
-
Another take is just define this in typeorm: With typescript we just have to create interfaces for each type and define what is mutable
So we only fetch with |
Beta Was this translation helpful? Give feedback.
-
The following is a brief summary of how we will incrementally add Typescript to Medusa. Motivation Decision We want the following benefits from Typescript:
Implementation The process is as follows:
Note, that properties can be optional
This is a rough outline of the steps. Obviously, more tweaks and additions are needed, but for the sake of this summary, those are omitted. For a more detailed description of the implementation, we refer to branch
Finally, it's important to once again stress, that we add Typescript to support the development process, so we prioritize readability, easy transition from JS, and an initial loosely typed approach. All feedback and input is welcomed - thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
Following up with a brief description of how we could potentially add Typescript to our API validation layer. Motivation (Potential) Decision Implementation (first take) Initially, our route looks something like this:
Now, the steps to refactor the above route to use class validation instead of Joi can be boiled down to the following two steps:
See below:
The
Let me know, what you think :) |
Beta Was this translation helpful? Give feedback.
-
Hi all!
As some of you might know from looking in our roadmap, we are currently scoping how to approach Typescript support for Medusa. There are several things to which, we would like to get your input; folder structure, naming conventions, etc.. Though, to ease the process of migrating our project, we've chosen to take a "Divide and Conquer" approach.
This issue touches upon a single task related to how we should support Typescript for our entities / models. Allow me to outline, what we want to achieve starting from the end goal and an example entity and working our way out.
I want to stress, that this is very experimental, so any input is appreciated. We are currently investigating the best possible solution and this might as well not be a good foundation (and might not be possible). So feel free to throw any ideas in the mix.
Say we have an entity
Item
, that has the following properties:The important thing to note about this entity is, that
id
andcart_id
are properties, that can only be set upon creating the entity (let's call this "Creatable"). Whereasunit_price
andtitle
are properties, that can be set on creation but also later updated (let's call this "Updateable").Then we would have a service,
ItemService
, that handles the business logic around the entities. The service would have (among many others) the following methods:These two methods create and update users. We are using a type
PartialEntity
, which takes a generic typeT
(entity) and aCondition
and gives back a partial entity with only the props that pass that condition.The type
PartialEntity
is defined, alongside a couple of helpers, as such:The two methods alongside these three types should allow us to achieve the following when updating and creating items in our service layer:
This leaves us with the final step; defining the properties on the entities as Creatable and Updateable. And this is where it gets a bit tricky.
We would like to be able to say something along the lines of:
And then use that type when defining Entity properties:
This concludes the outline for what we want to achieve. As mentioned earlier, we are still investigating possible solutions so this issue should be seen as more a foundation for discussion, and not so much a specific locked-in approach we want to take. There might be several other ways to go about this, that are a whole lot better, so please pitch in.
We would love to get some feedback and input ❤️
Beta Was this translation helpful? Give feedback.
All reactions