Skip to content

Commit 1decaa7

Browse files
committed
views schema working
1 parent a2e2202 commit 1decaa7

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/server/templates/zod.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ export const apply = async (meta: GeneratorMetadata): Promise<string> => {
251251
.filter((t) => t.schema === defaultSchema.name)
252252
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
253253

254+
const defaultSchemaViews = [...views]
255+
.filter((v) => v.schema === defaultSchema.name)
256+
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
257+
254258
// Helpers are now defined at the top of the file
255259

256260
const makeListShapeLine = (col: PostgresColumn, ctx: PgTypeCtx) => {
@@ -281,7 +285,7 @@ export const apply = async (meta: GeneratorMetadata): Promise<string> => {
281285
type: 'to-one',
282286
targetTable: relation.referenced_relation,
283287
targetKey: relation.referenced_columns[0],
284-
sourceKey: relation.columns[0]
288+
sourceKey: relation.columns[0],
285289
})
286290
typeVal = withNullable(typeVal, true)
287291

@@ -305,16 +309,17 @@ export const apply = async (meta: GeneratorMetadata): Promise<string> => {
305309
${zodHelperScript}
306310
307311
export const supabaseZodSchemas = {
308-
${defaultSchemaTables
309-
.map((table) => {
312+
${[...defaultSchemaTables, ...defaultSchemaViews]
313+
.map((tableOrView) => {
314+
const isView = defaultSchemaViews.includes(tableOrView as any)
310315
const ctx: PgTypeCtx = { types, schemas, tables, views }
311-
const cols = columnsByTableId[table.id] ?? []
316+
const cols = columnsByTableId[tableOrView.id] ?? []
312317
const relevantRels = relationships
313318
.filter(
314319
(relationship) =>
315-
relationship.schema === table.schema &&
316-
relationship.referenced_schema === table.schema &&
317-
relationship.relation === table.name
320+
relationship.schema === tableOrView.schema &&
321+
relationship.referenced_schema === tableOrView.schema &&
322+
relationship.relation === tableOrView.name
318323
)
319324
.sort(
320325
(a, b) =>
@@ -329,7 +334,7 @@ export const supabaseZodSchemas = {
329334
.join(',\n ')
330335
331336
// Get many-to-many relationships for this table
332-
const manyToManyRels = tableToManyToMany[table.name] || []
337+
const manyToManyRels = tableToManyToMany[tableOrView.name] || []
333338
const manyToManyShape = manyToManyRels
334339
.filter((relatedTable) => {
335340
// Don't add if there's already a relevant relationship with the same table
@@ -359,7 +364,15 @@ export const supabaseZodSchemas = {
359364
.filter((shape) => shape.length > 0)
360365
.join(',\n ')
361366
362-
return `${JSON.stringify(table.name)}: {
367+
if (isView) {
368+
return `${JSON.stringify(tableOrView.name)}: {
369+
list: z.object({
370+
${finalListShape}
371+
}),
372+
}`
373+
}
374+
375+
return `${JSON.stringify(tableOrView.name)}: {
363376
list: z.object({
364377
${finalListShape}
365378
}),

0 commit comments

Comments
 (0)