Allow strings for numeric type inserts and updates#973
Allow strings for numeric type inserts and updates#973Shadowfiend wants to merge 1 commit intosupabase:masterfrom
numeric type inserts and updates#973Conversation
When generating TypeScript types, the current code "correctly" reports numeric types as `number`, as Postgres and Postgrest return such columns as JSON numbers by default without a query `::text` cast. However, generated insert and update types are limited to `number`, whereas the underlying APIs will accept a string and allow preserving precision that would be lost in conversion to JS `number` this way. Generated types for inserting and updating numeric fields now include `| string` as a possibility to allow for this.
There was a problem hiding this comment.
Thanks for the contribution! I appreciate the effort you put into this PR.
That said, I’m not in favor of merging this as a global change. In most cases, this approach could introduce issues down the line, especially since it primarily affects bigint and similar fields. And could also constitute a breaking change in user code where functions might expect things to be a number and not a number | string type. Instead, I’d recommend handling this on a per-column basis like mentioned in the guide, if you know a specific column may have precision issues when converted in JavaScript, you can override it to be typed as number | string or number | bigint.
This way, we avoid broad changes that might cause unintended side effects while still giving flexibility where needed.
| if (column.name.toLowerCase().startsWith("decimal")) { | ||
| console.log("YOYOYOYO Insert", `'${column.name}'`, `'${column.format}'`) | ||
| } |
There was a problem hiding this comment.
issue
Probably don't want this to stay around.
Hmmm, why do you think so? The updated typing here (test logging notwithstanding 😅) is more reflective of what the backend actually allows and expects. It also allows for more correct code—JS numbers are lossy, Postgres decimal types are not, and forcing I hear you on overriding the types after the fact and appreciate the suggestion, but in practice I've found that to be quite a brittle approach to type management. |
When generating TypeScript types, the current code "correctly" reports numeric types as
number, as Postgres and Postgrest return such columns as JSON numbers by default without a query::textcast.However, generated insert and update types are limited to
number, whereas the underlying APIs will accept a string and allow preserving precision that would be lost in conversion to JSnumberthis way.Generated types for inserting and updating numeric fields now include
| stringas a possibility to allow for this.Closes #972.