-
Notifications
You must be signed in to change notification settings - Fork 0
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
Model hooks / default attributes #45
Comments
Was surprised that I didn't find this existing as it's been asked several times before + is definitely a legitimate feature request. It's also confusing as folks use factory attrs as "defaults" then wonder how to use them in route handlers. (But factory attrs serve a different purpose). It's definitely a gap in Mirage atm – best solution today is to just make some sort of "createUser" function that has the default logic, and can be shared in route handlers and/or factories. Hooks are a good idea. As for our priority right now, it's getting Mirage working in some different environments that it doesn't right now (e.g. Next.js) and shipping a 1.0. Then, before we add a feature like this, we need to get ES6 classes in. I think that's our rough roadmap so while I think this is an important feature it's not yet being worked on. |
Thanks for the response, that makes sense. With regards to your point about the factory attributes as defaults- I guess you mean something like this?
I haven't tried the above because I ended up extending the save() method (detailed below). I didn't find the concept of factory attributes confusing but I was thrown by the fact the fixtures don't use the factories or even the models to create the data, they're just copied into the database verbatim. I'd set up some fixtures with some static data like IDs which I always wanted to know they're there then was hoping to fill in lots of arbitrary data in using factories or models. It meant eventually i had to drop the use of fixtures and just create everything with server.create(). In the mean time, I've found the below sufficient, it simply adds some attributes on.
|
Sorry about the delayed response! I'm behind on issues a bit due to some consulting/training work.
I wasn't clear – my point was that some folks reach for using factory attributes as model defaults, but that's incorrect usage and should not be done. I was just validating that your need for model defaults is valid + a gap in Mirage today.
I personally do this too and think it's the best approach, since using
Model Hopefully that helps! Let me know if you have any more questions. |
Any update on the recommended way to have default attributes? |
Unfortunately not, I think functions are the best bet today. Would gladly accept PRs in this direction if someone were so inclined! |
Is it possible to have timestamps automatically created/touched on models when created/updated?
I saw it can be done with factories when seeding and then explicitly writing handlers for all the routes, but if I want to use the nice shorthand route handlers then it'd be nice to have some hooks i can set up on the model itself. which always run when created/touched perhaps?
Something like the below maybe?
The following hooks may be useful.
The text was updated successfully, but these errors were encountered: