Initialise new fields in existing entities with the default value #4050
snigdha920
started this conversation in
Ideas
Replies: 1 comment
-
|
This is the perfect case for a DB migration, didn't cross my mind. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Description
I have an entity schema which was used to create many entities in the database. Now, I'm updating the entity schema to add a new embeddable field.
When fetching an existing entity, the value of the new field is
undefinedwhich is normal because the field did not exist when the entity was created.However I want this field to be initialised with a default value, even for the entities which were created in the database before this field was added.
I thought just setting the
defaultfield on the properties in the embeddable would achieve this, but it doesn't.This is also understandable, since:
The field I want to add is a mandatory field, but the entities in the database just return
undefinedfor that field. Since it is a mandatory field, theundefinedisn't caught at compile time, and I get runtime errors.Here is the reproduction repo: https://github.com/snigdha920/nestjs-mikro-embeddables
Proposal
So I propose: we add an option like
valueForExisting(just a placeholder name) in the@Embeddable/@Properydecorators to explicitly set the fields with the value given for existing entities.It could mean a write transaction to the database when only a read transaction is expected, which is an anti-pattern, but I'm not sure what can be done about it?
How does the idea seem? Is there a different way you propose I handle this? Happy to hear thoughts and provide more info!
Beta Was this translation helpful? Give feedback.
All reactions