Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,26 @@ export type Database = {
})}
}

export const Constants = {
${schemas
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
.map((schema) => {
const schemaEnums = types
.filter((type) => type.schema === schema.name && type.enums.length > 0)
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
return `${JSON.stringify(schema.name)}: {
Enums: {
${schemaEnums.map(
(enum_) =>
`${JSON.stringify(enum_.name)}: [${enum_.enums
.map((variant) => JSON.stringify(variant))
.join(', ')}]`
)}
}
}`
})}
} as const

type DefaultSchema = Database[Extract<keyof Database, ${JSON.stringify(GENERATE_TYPES_DEFAULT_SCHEMA)}>]

export type Tables<
Expand Down
27 changes: 27 additions & 0 deletions test/server/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,15 @@ test('typegen: typescript', async () => {
}
}

export const Constants = {
public: {
Enums: {
meme_status: ["new", "old", "retired"],
user_status: ["ACTIVE", "INACTIVE"],
},
},
} as const

type DefaultSchema = Database[Extract<keyof Database, "public">]

export type Tables<
Expand Down Expand Up @@ -986,6 +995,15 @@ test('typegen w/ one-to-one relationships', async () => {
}
}

export const Constants = {
public: {
Enums: {
meme_status: ["new", "old", "retired"],
user_status: ["ACTIVE", "INACTIVE"],
},
},
} as const

type DefaultSchema = Database[Extract<keyof Database, "public">]

export type Tables<
Expand Down Expand Up @@ -1541,6 +1559,15 @@ test('typegen: typescript w/ one-to-one relationships', async () => {
}
}

export const Constants = {
public: {
Enums: {
meme_status: ["new", "old", "retired"],
user_status: ["ACTIVE", "INACTIVE"],
},
},
} as const

type DefaultSchema = Database[Extract<keyof Database, "public">]

export type Tables<
Expand Down