From 75114a1496f275416d039ce49b75d964eb272ba7 Mon Sep 17 00:00:00 2001 From: Antonio Salazar Cardozo Date: Fri, 8 Aug 2025 12:40:11 -0400 Subject: [PATCH] Allow strings for `numeric` type inserts and updates 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. --- src/server/templates/typescript.ts | 16 ++++++++++++++++ test/server/typegen.ts | 14 +++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/server/templates/typescript.ts b/src/server/templates/typescript.ts index 4f9cac03..dda78f55 100644 --- a/src/server/templates/typescript.ts +++ b/src/server/templates/typescript.ts @@ -144,6 +144,16 @@ export type Database = { views, }) + if (column.name.toLowerCase().startsWith("decimal")) { + console.log("YOYOYOYO Insert", `'${column.name}'`, `'${column.format}'`) + } + + // Numeric types can be inserted as strings to preserve + // precision. + if (column.format === "numeric") { + output += '| string' + } + if (column.is_nullable) { output += '| null' } @@ -166,6 +176,12 @@ export type Database = { views, })}` + // Numeric types can be inserted as strings to preserve + // precision. + if (column.format === "numeric") { + output += '| string' + } + if (column.is_nullable) { output += '| null' } diff --git a/test/server/typegen.ts b/test/server/typegen.ts index 76ac6218..5b38f1be 100644 --- a/test/server/typegen.ts +++ b/test/server/typegen.ts @@ -227,13 +227,13 @@ test('typegen: typescript', async () => { status: Database["public"]["Enums"]["user_status"] | null } Insert: { - decimal?: number | null + decimal?: number | string | null id?: number name?: string | null status?: Database["public"]["Enums"]["user_status"] | null } Update: { - decimal?: number | null + decimal?: number | string | null id?: number name?: string | null status?: Database["public"]["Enums"]["user_status"] | null @@ -870,13 +870,13 @@ test('typegen: typescript w/ one-to-one relationships', async () => { status: Database["public"]["Enums"]["user_status"] | null } Insert: { - decimal?: number | null + decimal?: number | string | null id?: number name?: string | null status?: Database["public"]["Enums"]["user_status"] | null } Update: { - decimal?: number | null + decimal?: number | string | null id?: number name?: string | null status?: Database["public"]["Enums"]["user_status"] | null @@ -1528,13 +1528,13 @@ test('typegen: typescript w/ postgrestVersion', async () => { status: Database["public"]["Enums"]["user_status"] | null } Insert: { - decimal?: number | null + decimal?: number | string | null id?: number name?: string | null status?: Database["public"]["Enums"]["user_status"] | null } Update: { - decimal?: number | null + decimal?: number | string | null id?: number name?: string | null status?: Database["public"]["Enums"]["user_status"] | null @@ -1682,7 +1682,7 @@ test('typegen: typescript w/ postgrestVersion', async () => { status: Database["public"]["Enums"]["user_status"] | null } Insert: { - decimal?: number | null + decimal?: number | string | null id?: number | null name?: string | null status?: Database["public"]["Enums"]["user_status"] | null