Skip to content

Commit e9cd3fd

Browse files
committed
Add constant arrays of enum values for typescript
1 parent 8be8982 commit e9cd3fd

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

Diff for: src/server/templates/typescript.ts

+20
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,26 @@ export type Database = {
391391
})}
392392
}
393393
394+
export const Constants = {
395+
${schemas
396+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
397+
.map((schema) => {
398+
const schemaEnums = types
399+
.filter((type) => type.schema === schema.name && type.enums.length > 0)
400+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
401+
return `${JSON.stringify(schema.name)}: {
402+
Enums: {
403+
${schemaEnums.map(
404+
(enum_) =>
405+
`${JSON.stringify(enum_.name)}: [${enum_.enums
406+
.map((variant) => JSON.stringify(variant))
407+
.join(', ')}]`
408+
)}
409+
}
410+
}`
411+
})}
412+
} as const
413+
394414
type DefaultSchema = Database[Extract<keyof Database, ${JSON.stringify(GENERATE_TYPES_DEFAULT_SCHEMA)}>]
395415
396416
export type Tables<

Diff for: test/server/typegen.ts

+27
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,15 @@ test('typegen: typescript', async () => {
431431
}
432432
}
433433
434+
export const Constants = {
435+
public: {
436+
Enums: {
437+
meme_status: ["new", "old", "retired"],
438+
user_status: ["ACTIVE", "INACTIVE"],
439+
},
440+
},
441+
} as const
442+
434443
type DefaultSchema = Database[Extract<keyof Database, "public">]
435444
436445
export type Tables<
@@ -986,6 +995,15 @@ test('typegen w/ one-to-one relationships', async () => {
986995
}
987996
}
988997
998+
export const Constants = {
999+
public: {
1000+
Enums: {
1001+
meme_status: ["new", "old", "retired"],
1002+
user_status: ["ACTIVE", "INACTIVE"],
1003+
},
1004+
},
1005+
} as const
1006+
9891007
type DefaultSchema = Database[Extract<keyof Database, "public">]
9901008
9911009
export type Tables<
@@ -1541,6 +1559,15 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
15411559
}
15421560
}
15431561
1562+
export const Constants = {
1563+
public: {
1564+
Enums: {
1565+
meme_status: ["new", "old", "retired"],
1566+
user_status: ["ACTIVE", "INACTIVE"],
1567+
},
1568+
},
1569+
} as const
1570+
15441571
type DefaultSchema = Database[Extract<keyof Database, "public">]
15451572
15461573
export type Tables<

0 commit comments

Comments
 (0)