A
arktype3w ago
noah

Generic wrapper for drizzle-arktype functions

When using the drizzle-arktype functions I found myself writing the following two lines over and over again when validating my API requests:
createInsertSchema(todos)
.omit('id', 'createdAt', 'updatedAt')
.onUndeclaredKey('delete')
createInsertSchema(todos)
.omit('id', 'createdAt', 'updatedAt')
.onUndeclaredKey('delete')
So I wanted to abstract this away into a function that currently looks like this:
export function insertSchema<
T extends AnyPgTable & {
id: AnyPgColumn
createdAt: AnyPgColumn
updatedAt: AnyPgColumn
},
>(table: T) {
return createInsertSchema(table)
.omit('id', 'createdAt', 'updatedAt')
.onUndeclaredKey('delete')
}
export function insertSchema<
T extends AnyPgTable & {
id: AnyPgColumn
createdAt: AnyPgColumn
updatedAt: AnyPgColumn
},
>(table: T) {
return createInsertSchema(table)
.omit('id', 'createdAt', 'updatedAt')
.onUndeclaredKey('delete')
}
The issue I am facing now is that I get a Property 'omit' does not exist on type ... error. Initially I just used AnyPgTable as the generic type so I thought that adding the column definitions might help but I was seemingly wrong. I don't know if this is the right place to ask but I would greatly appreciate any help :)
5 Replies
ssalbdivad
ssalbdivad3w ago
CC @Mario564 do we know that createInsertSchema will always create an object Type? If it's inferred as Type<any> or similar that would probably be the reason object-specific methods like .omit weren't available
Mario564
Mario5643w ago
The generic type should be declared as:
T extends PgTableWithColumns<{
// Change the below properties to other types in case they don't match the ones used in your schema
id: PgIntegerColumn;
createdAt: PgTimestampColumn;
updatedAt: PgTimestampColumn;
}>
T extends PgTableWithColumns<{
// Change the below properties to other types in case they don't match the ones used in your schema
id: PgIntegerColumn;
createdAt: PgTimestampColumn;
updatedAt: PgTimestampColumn;
}>
You may also need to wrap the Column types with other types like NotNull, HasDefault, etc.
noah
noahOP2w ago
I don't know if I am being stupid but I dont think that PgIntegerColumn etc. exist. Also the PgTableWithColumns type takes a bunch of other arguments like name, dialect etc. The suggested code above did not work for me. I now tried the following. Manually providing the types like so:
export function insertSchema<T extends PgTableWithColumns<{
name: "",
dialect: "pg",
schema: undefined,
columns: {
id: PgIntegerBuilderInitial<"id">,
}
}>(table: T) {
return createInsertSchema(table)
.omit('id', 'createdAt', 'updatedAt')
.onUndeclaredKey('delete')
}
export function insertSchema<T extends PgTableWithColumns<{
name: "",
dialect: "pg",
schema: undefined,
columns: {
id: PgIntegerBuilderInitial<"id">,
}
}>(table: T) {
return createInsertSchema(table)
.omit('id', 'createdAt', 'updatedAt')
.onUndeclaredKey('delete')
}
Or just taking the type of a real table like so:
const testTable = pgTable('test', (t) => ({
...WITH_ID,
...WITH_TIMESTAMPS,
}))

export function insertSchema<T extends typeof testTable>(table: T) {
return createInsertSchema(table)
.omit('id', 'createdAt', 'updatedAt')
.onUndeclaredKey('delete')
}
const testTable = pgTable('test', (t) => ({
...WITH_ID,
...WITH_TIMESTAMPS,
}))

export function insertSchema<T extends typeof testTable>(table: T) {
return createInsertSchema(table)
.omit('id', 'createdAt', 'updatedAt')
.onUndeclaredKey('delete')
}
Neither of these worked for me @Mario564 sorry for the ping but would you mind looking at this one more time id really appreciate it
Mario564
Mario5642w ago
You could try:
PgTableWithColumns<{
name: any,
dialect: any,
schema: any,
columns: {
id: PgInteger<... /* You may need to fill more stuff here */>,
}
}
PgTableWithColumns<{
name: any,
dialect: any,
schema: any,
columns: {
id: PgInteger<... /* You may need to fill more stuff here */>,
}
}
noah
noahOP2w ago
yeah even when filling out all the attributes it does not work. Would it not make sense to just always emit an object type? After all its a datatable it will always be an object

Did you find this page helpful?