Aaron
Aaron
DTDrizzle Team
Created by titongo on 11/9/2023 in #help
need help abstracting a function
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
12 replies
DTDrizzle Team
Created by titongo on 11/9/2023 in #help
need help abstracting a function
@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 😄
12 replies
DTDrizzle Team
Created by Aaron on 12/22/2023 in #help
executing query builder sql
Thank you, just found I can use format from @planetscale/database to get my rawSql to pass into sql.raw()
const { rows } = await db.execute(sql.raw(rawSql));
const { rows } = await db.execute(sql.raw(rawSql));
4 replies
DTDrizzle Team
Created by titongo on 11/9/2023 in #help
need help abstracting a function
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;
}
12 replies