Soka
Soka
DTDrizzle Team
Created by Soka on 2/23/2025 in #help
Generic table with specific fields
I'm trying to create a function that takes in a table that has some arbitrary columns and the columns created_at and updated_at. I want to create a generic that describes this correctly so it also works with things like drizzle-zod. The ideal solution would be something like
export function createCrudSchemas<TTable extends WithColumns<AnyPgTable, {
created_at: AnyPgColumn,
updated_at: AnyPgColumn,
}>>(
table: TTable,
db: NodePgDatabase,
) {
const createSchema = {
body: createInsertSchema(table)
.omit({
created_at: true, // This will not throw a TypeError
updated_at: true,
}),
response: {
200: createSelectSchema(table),
},
};
...
export function createCrudSchemas<TTable extends WithColumns<AnyPgTable, {
created_at: AnyPgColumn,
updated_at: AnyPgColumn,
}>>(
table: TTable,
db: NodePgDatabase,
) {
const createSchema = {
body: createInsertSchema(table)
.omit({
created_at: true, // This will not throw a TypeError
updated_at: true,
}),
response: {
200: createSelectSchema(table),
},
};
...
So far the closest I've been able to get is the following, which isn't exactly elegant.
export function createCrudSchemas<TTable extends PgTable>(
table: TTable,
) {
const createSchema = {
body: createInsertSchema(table)
.extend({
created_at: z.never(),
updated_at: z.never(),
})
.omit({
created_at: true,
updated_at: true,
}),
response: {
200: createSelectSchema(table),
},
};
...
export function createCrudSchemas<TTable extends PgTable>(
table: TTable,
) {
const createSchema = {
body: createInsertSchema(table)
.extend({
created_at: z.never(),
updated_at: z.never(),
})
.omit({
created_at: true,
updated_at: true,
}),
response: {
200: createSelectSchema(table),
},
};
...
1 replies