-
Notifications
You must be signed in to change notification settings - Fork 27
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
Return instances of model class #278
Comments
@jthomerson thanks for reporting this issue. This is not the first time we get this request, as you realized yourself. So I hope to be able to kickoff a first code change to get this implementation started, will keep you posted. |
@michaelwittwer any update on this? Alternatively, do you have any guidance on where / how you would implement it? It's the biggest nitpick I have with this library, which otherwise suits my needs very well. I could try contributing the feature, but I'm sure you have in mind a specific interface / way that it would work. |
Hi, I wonder if you have any news on this topic. I wish I could help with the implementation but it is not the case at the moment. |
Same question as @mariobittencourt. I've built dozens of classes with DE this past week, and this has bitten me so many times; I'm constantly copy/pastaing a bunch of ugly static methods to convert DB records into instances. Additionally, as I built one today, I thought of a related usecase - it needs to support not just methods like class Foo {
@Property({ name: 'bar' })
private _bar: string;
}
|
Hi @michaelwittwer and @simonmumenthaler , if possible provide a feedback so I can understand if I should wait or move and try to look for alternatives, what would be sad given how interesting and easy this package behaved. |
Hi @jthomerson @mariobittencourt I basically just return This way the values are defined directly on a new instance or through a setter if present. Would this work for you or do you might have another (better?) approach? |
Hi @simonmumenthaler thank you for the reply. I will try to use locally. |
@simonmumenthaler that's basically what I've been doing for my models, so yes, I think it generally works. As @mariobittencourt mentioned, it would be nice to be able to use private constructors, but right now that doesn't seem possible because when I try to create the store, it doesn't work when my constructor is private. So, that may be a separate issue. One other edge case that I've come across is where I need to do something async immediately after construction. I've been using a static factory method for this - clients who need to construct an object do so through the static factory method. To be able to accomplish that with Dynamo-Easy, perhaps one of these ideas would help:
Of course, those are both additional features above-and-beyond the basic default behavior that this issue is focused on - which I think is addressed by your lab branch. Let me know if those comments don't make sense or you have questions. |
@mariobittencourt you can use the published prerelease version |
Hi, @simonmumenthaler it worked for me with both public and private constructors. Thank you. @jthomerson I used this simple code to test private constructor: https://gist.github.com/mariobittencourt/8341b6d92ba4c3dc31f0859010c2a1f3 Maybe you can take a look and see if there is something different that would cause issues in your case. |
@simonmumenthaler one question. I wonder how did you get the modelConstructor? I have a situation where I will have some properties that are polimorphic (example, I have a collection of events and the events can be of different types). I was planning on having something like this:
In your code you have the modelConstructor being injected. In my case how can I get the modelConstructor? I would add the @model in each event. |
@simonmumenthaler it doesn't seem like this ever landed in the mainline. Do you have plans to merge and release this? Tangentially: to try to determine if it had landed, I needed to use git log extensively since I'm on 5.5.0 and you're now up to 7.1.0. Do you have any plans to have a CHANGELOG? |
@jthomerson thanks for coming back to this. Your assumption is right, this never landed so far. We would love to merge this, but didn't have the time to verify on our side. What I could offer is releasing a new version branching of the head ( Concerning the |
@michaelwittwer thanks! Yes, if you're able to make a version of this feature off of 7.1.0, I can test it out this week / next week as I'm working on some new model classes for a new service I'm building. |
…ance # Conflicts: # .releaserc.yml
@jthomerson just released a new version -> v7.2.0-pr278.1. thanks for verifying! |
Still testing, and will probably have more feedback, but wanted to pass along this initial thought: This will definitely need to be labeled as a breaking change. I upgraded and a bunch of model classes that do not have a default constructor broke. For example, if they are Initial thoughts on how to overcome this is to support pluggable constructors for the model. In otherwords, similar to how you do this: construct(o: Bar): Bar {
return Object.assign(new Bar(o.foo), o));
}
@Model({ constructor: construct })
class Bar {
public constructor(foo: string) {
this.foo = foo.toLowerCase();
}
} One other thought while I'm here and so I don't forget: the comment above from June 3rd about needing access to a mapper so you can use this on stream objects ... have you given any thought to that yet? |
I don't think I have any more feedback other than what I left last above. That limitation made it where I really couldn't use this in our existing apps. Sum-up: I think without a way of supporting non-default constructors, you'd basically have to design your model classes around Dynamo-Easy. That has a long-reaching effect: if I can't require certain fields in my constructor, then some fields on my model class have to become optional, which starts to break things. If you want any required fields, you typically require them in your constructor. So if we can't do that, then how do we build our models? |
@michaelwittwer any updates on this? |
Is your feature request related to a problem? Please describe.
I'm frustrated that I interact with the model class everywhere to create and write data, but then when I read data (get/query/scan/etc), I get raw JS objects back instead of instances of the model class.
Describe the solution you'd like
I would like
store.get('some-key').exec()
to return the actual instance of the model. The type signature says it does, but it doesn't ... it actually returns a raw JS object with the same fields as the model class.Describe alternatives you've considered
Every model that I define will have to have a
public constructor(o: ModelType)
that takes in the raw object and "hydrates" the model object. That's super repetitive, and (to me) defeats the purpose of the "ORM" (I know this isn't technically an ORM, but it's filling that need in a DynamoDB world).Related: see #220
The text was updated successfully, but these errors were encountered: