How do I type the `with` object part of a query builder if I extract it to it's own object?

I have a large with object that I reuse across multiple queries and I wanted to extract it to its own variable for reusing it. I typed this object with DBQueryConfig["with"] which seems to make typescript happy but the return type is not inferred correctly. Am I missing something?
const fullEventWithParams: DBQueryConfig["with"] = {
users: {
with: {
// this is a many-to-many table
// and this object is not getting inferred
user: {
columns: {
id: true,
email: true,
firstName: true,
lastName: true,
phone: true,
},
},
},
},
owner: { columns: { password: false } },
vehicleType: { columns: { id: true, name: true } },
location: true,
examination: true,
};

const events = await db.query.events.findMany({
with: fullEventWithParams,
});
const fullEventWithParams: DBQueryConfig["with"] = {
users: {
with: {
// this is a many-to-many table
// and this object is not getting inferred
user: {
columns: {
id: true,
email: true,
firstName: true,
lastName: true,
phone: true,
},
},
},
},
owner: { columns: { password: false } },
vehicleType: { columns: { id: true, name: true } },
location: true,
examination: true,
};

const events = await db.query.events.findMany({
with: fullEventWithParams,
});
5 Replies
Angelelz
Angelelz12mo ago
GitHub
Relations input · drizzle-team drizzle-orm · Discussion #1483
I have a schema that looks like somewhat like this: export const employeesSchema = pgTable('employees', { firstName: varchar('first_name', { length: 256 }), lastName: varchar('l...
Barrabasz
BarrabaszOP12mo ago
Thanks! @Angelelz Maybe as a suggestion for the future, there could be kind of a utility type that would extract all of those generics, kind of how FieldValues and Path works in react-hook-form.
Angelelz
Angelelz12mo ago
Yeah, there an open PR for this, should land soon
Barrabasz
BarrabaszOP12mo ago
It seems typing it with the type you provided works but it does not help the type inference. I think the issue is that I am specifying specific columns in a nested relation. Any other ideas?
Angelelz
Angelelz12mo ago
Yeah, you need to make your wrapper function generic. Take a look at the playground I put together in that discussion
Want results from more Discord servers?
Add your server