Skip to content

Commit 75114a1

Browse files
committed
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.
1 parent 60397be commit 75114a1

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/server/templates/typescript.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ export type Database = {
144144
views,
145145
})
146146
147+
if (column.name.toLowerCase().startsWith("decimal")) {
148+
console.log("YOYOYOYO Insert", `'${column.name}'`, `'${column.format}'`)
149+
}
150+
151+
// Numeric types can be inserted as strings to preserve
152+
// precision.
153+
if (column.format === "numeric") {
154+
output += '| string'
155+
}
156+
147157
if (column.is_nullable) {
148158
output += '| null'
149159
}
@@ -166,6 +176,12 @@ export type Database = {
166176
views,
167177
})}`
168178
179+
// Numeric types can be inserted as strings to preserve
180+
// precision.
181+
if (column.format === "numeric") {
182+
output += '| string'
183+
}
184+
169185
if (column.is_nullable) {
170186
output += '| null'
171187
}

test/server/typegen.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,13 @@ test('typegen: typescript', async () => {
227227
status: Database["public"]["Enums"]["user_status"] | null
228228
}
229229
Insert: {
230-
decimal?: number | null
230+
decimal?: number | string | null
231231
id?: number
232232
name?: string | null
233233
status?: Database["public"]["Enums"]["user_status"] | null
234234
}
235235
Update: {
236-
decimal?: number | null
236+
decimal?: number | string | null
237237
id?: number
238238
name?: string | null
239239
status?: Database["public"]["Enums"]["user_status"] | null
@@ -870,13 +870,13 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
870870
status: Database["public"]["Enums"]["user_status"] | null
871871
}
872872
Insert: {
873-
decimal?: number | null
873+
decimal?: number | string | null
874874
id?: number
875875
name?: string | null
876876
status?: Database["public"]["Enums"]["user_status"] | null
877877
}
878878
Update: {
879-
decimal?: number | null
879+
decimal?: number | string | null
880880
id?: number
881881
name?: string | null
882882
status?: Database["public"]["Enums"]["user_status"] | null
@@ -1528,13 +1528,13 @@ test('typegen: typescript w/ postgrestVersion', async () => {
15281528
status: Database["public"]["Enums"]["user_status"] | null
15291529
}
15301530
Insert: {
1531-
decimal?: number | null
1531+
decimal?: number | string | null
15321532
id?: number
15331533
name?: string | null
15341534
status?: Database["public"]["Enums"]["user_status"] | null
15351535
}
15361536
Update: {
1537-
decimal?: number | null
1537+
decimal?: number | string | null
15381538
id?: number
15391539
name?: string | null
15401540
status?: Database["public"]["Enums"]["user_status"] | null
@@ -1682,7 +1682,7 @@ test('typegen: typescript w/ postgrestVersion', async () => {
16821682
status: Database["public"]["Enums"]["user_status"] | null
16831683
}
16841684
Insert: {
1685-
decimal?: number | null
1685+
decimal?: number | string | null
16861686
id?: number | null
16871687
name?: string | null
16881688
status?: Database["public"]["Enums"]["user_status"] | null

0 commit comments

Comments
 (0)