pinia-orm icon indicating copy to clipboard operation
pinia-orm copied to clipboard

Unset values default to null

Open s314cy opened this issue 1 year ago • 4 comments

Describe the feature

When defining a Pinia ORM model in TypeScript, optional fields must be typed as field: type | null because the save method of Pinia ORM repos converts unset values (null, undefined) to null.

However, the usual TypeScript way is declaring an optional field as field?: type, which treats an unset value as undefined. Furthermore, TypeScript's optional chaining (a?.b?.c) and utility types (Required<T>, Partial<T>, etc.) also revolve around the undefined value.

My proposition is to replace the usage of null for unset values with undefined. This seems fairly easy and is rather a matter of acceptance by/relevance to the project.

Additional information

  • [X] Would you be willing to help implement this feature?

Final checks

s314cy avatar Jan 30 '24 10:01 s314cy

Probably something to investigate: attributes default to null, but it seems that relations default to undefined 🤔

s314cy avatar Jan 30 '24 10:01 s314cy

I think you are right. But changing this, is something for v2 i think. Because it's a huge breaking change. But i could already implement this behaviour with a feature flag.

CodeDredd avatar May 05 '24 19:05 CodeDredd

The optional chaining works with undefined and null

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.

MartinX3 avatar Aug 12 '24 13:08 MartinX3