DT
Drizzle Team•14mo ago
titongo

need help abstracting a function

This may be an easy answer but how can I implement the following type?
export const getUserWithEmail = (email: string, withConfig: any) => {
return db.query.usersTable.findFirst({
where: (dbUser, { eq }) => eq(dbUser.email, email),
with: withConfig
});
};
export const getUserWithEmail = (email: string, withConfig: any) => {
return db.query.usersTable.findFirst({
where: (dbUser, { eq }) => eq(dbUser.email, email),
with: withConfig
});
};
is this abstractions stupid? I would like withConfig to be correctly typed. Does it make sense or should I just repeat the function and be free?
Solution:
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...
Jump to solution
9 Replies
Angelelz
Angelelz•14mo ago
Lol, did you ask this question in the discussions in github?
Solution
Angelelz
Angelelz•14mo 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...
Angelelz
Angelelz•14mo ago
You can adapt that answer to what you need
titongo
titongoOP•14mo ago
Damn that's awfully similar to my problem haha yes it works, thanks for the answer
Aaron
Aaron•13mo ago
Thanks, this was helpful... I'm still trying to get the query client to change its return type based on what relations are passed in.
type Relations = ExtractTablesWithRelations<typeof schema>;

export type WithInput<T extends keyof Relations> = DBQueryConfig<
'many',
true,
Relations,
Relations[T]
>['with'];

async function getUserById(id: number, relations?: WithInput<'users'>) {
// TODO: does not return proper relation types (always typed as not having relations)
const user = await db.query.users.findFirst({
where: eq(users.id, id),
with: relations,
});

return user;
}
type Relations = ExtractTablesWithRelations<typeof schema>;

export type WithInput<T extends keyof Relations> = DBQueryConfig<
'many',
true,
Relations,
Relations[T]
>['with'];

async function getUserById(id: number, relations?: WithInput<'users'>) {
// TODO: does not return proper relation types (always typed as not having relations)
const user = await db.query.users.findFirst({
where: eq(users.id, id),
with: relations,
});

return user;
}
Stathis
Stathis•10mo ago
@Aaron did you managed to type the response incluiding the relations? Really struggling with this now...
Aaron
Aaron•10mo ago
@Stathis yeah, i've been doing:
type Relations = ExtractTablesWithRelations<typeof schema>;

export type WithInput<T extends keyof Relations> = DBQueryConfig<
'many',
true,
Relations,
Relations[T]
>['with'];

async function getUserById<R extends WithInput<'users'>>(id: User['id'], relations?: R) {
const user = await db.query.users.findFirst({
where: eq(users.id, id),
with: relations as R,
});

return user;
}

// then use like
const user = await getUserById(55, { posts: true });
type Relations = ExtractTablesWithRelations<typeof schema>;

export type WithInput<T extends keyof Relations> = DBQueryConfig<
'many',
true,
Relations,
Relations[T]
>['with'];

async function getUserById<R extends WithInput<'users'>>(id: User['id'], relations?: R) {
const user = await db.query.users.findFirst({
where: eq(users.id, id),
with: relations as R,
});

return user;
}

// then use like
const user = await getUserById(55, { posts: true });
this provides proper autocomplete and typesafety 😄
Stathis
Stathis•10mo ago
@Aaron sorry for bumping you again, how to do you type function parameters? eg. I want to pass the result to a function:
await doSomethingWithTheResult({
post: result[0],
});
await doSomethingWithTheResult({
post: result[0],
});
async function doSomethingWithTheResult(post: <HOW_TO_TYPE_THIS>) {}
async function doSomethingWithTheResult(post: <HOW_TO_TYPE_THIS>) {}
No matter what I tried I could not type the with, in this case post.post_images
Aaron
Aaron•10mo ago
you can likely just do something like this within your schema definitions?
export type Post = typeof posts.$inferSelect;
export type PostImages = typeof postImages.$inferSelect;
export type PostWithImages = Post & { post_images: PostImages[]; };
export type Post = typeof posts.$inferSelect;
export type PostImages = typeof postImages.$inferSelect;
export type PostWithImages = Post & { post_images: PostImages[]; };
there's probably other ways to infer the posts relationship using some drizzle types but I'd have to play around a bit to see
Want results from more Discord servers?
Add your server