icomad
icomad
Explore posts from servers
DTDrizzle Team
Created by icomad on 9/9/2024 in #help
Correct typings for factory table
Hey all, I'm back again with a problem with typings on a factory function for creating tables. I already asked about this months ago but the solution that another user came up with, which was something similar to what I was doing, is not correct and presents some problems. The goal is to make a factory function that creates a table with some default columns, like timestamps, author columns etc... This is the "solution" we found at the time:
const baseTableSchema = {
id: [...],
createdAt: [...],
updatedAt: [...],
createdBy: [...],
updatedBy: [...],
}
export const createTableFactory = <
TTableName extends string,
TColumnsMap extends Record<string, PgColumnBuilderBase>,
>(
name: TTableName,
fields: TColumnsMap,
extraConfig?: (
self: BuildExtraConfigColumns<
TTableName,
TColumnsMap & typeof baseTableSchema,
"pg"
>,
) => PgTableExtraConfig,
) => {
const { id, ...baseColumns } = baseTableSchema;

return pgTable<string, TColumnsMap & typeof baseTableSchema>(
name,
{
id,
...fields,
...baseColumns,
},
extraConfig,
);
};
const baseTableSchema = {
id: [...],
createdAt: [...],
updatedAt: [...],
createdBy: [...],
updatedBy: [...],
}
export const createTableFactory = <
TTableName extends string,
TColumnsMap extends Record<string, PgColumnBuilderBase>,
>(
name: TTableName,
fields: TColumnsMap,
extraConfig?: (
self: BuildExtraConfigColumns<
TTableName,
TColumnsMap & typeof baseTableSchema,
"pg"
>,
) => PgTableExtraConfig,
) => {
const { id, ...baseColumns } = baseTableSchema;

return pgTable<string, TColumnsMap & typeof baseTableSchema>(
name,
{
id,
...fields,
...baseColumns,
},
extraConfig,
);
};
The problem I'm facing is that the with field when querying the db on the tables created using this factory is typed as {} | undefined, so something like this gives me lots of typescript errors
await db.query.testTable.findMany({
with: {
// no autocomplete with the related tables
testTable2: {
orderBy: (table, {desc}) => ... // both table and desc have type any
}
}
})
await db.query.testTable.findMany({
with: {
// no autocomplete with the related tables
testTable2: {
orderBy: (table, {desc}) => ... // both table and desc have type any
}
}
})
Does anyone know how to correct the typings?
2 replies
DTDrizzle Team
Created by icomad on 6/7/2024 in #help
Table factory typescript problem
I have a basic table schema that all tables share (id, timestamps and more) and I'm trying to write an helper function that returns a pgTable with these fields already added. Right now it looks like this:
const baseTable: PgTableFn = (tableName, fields, extraConfig) =>
pgTable(
tableName,
{
id: uuid("id").primaryKey(),
...fields,
createdAt: timestamp("created_at"),
updatedAt: timestamp("updated_at")
},
extraConfig
);

const testTable = baseTable(
"test_table",
{ test: text("test") },
table => ({
testIdx: index().on(table.test), // <- This is ok
errIdx: index().on(table.createdAt) // <- This gives me an error
})
);
const baseTable: PgTableFn = (tableName, fields, extraConfig) =>
pgTable(
tableName,
{
id: uuid("id").primaryKey(),
...fields,
createdAt: timestamp("created_at"),
updatedAt: timestamp("updated_at")
},
extraConfig
);

const testTable = baseTable(
"test_table",
{ test: text("test") },
table => ({
testIdx: index().on(table.test), // <- This is ok
errIdx: index().on(table.createdAt) // <- This gives me an error
})
);
This is wrong though because testTable is not typed correctly and it is missing all the shared fields. How would I go about correcting this?
3 replies
TTCTheo's Typesafe Cult
Created by icomad on 6/24/2023 in #questions
Uploadthing onUploadComplete file size is undefined
Hey all, the property file on the parameter of onUploadComplete callback should have a size property that is not undefined based on its type UploadedFile, but this is not the case. I've tried both on local and in production on vercel and I always get file.size as undefined. Is there something wrong in the implementation or is the type UploadedFile wrong and the file size is not actually added to the object?
3 replies