Skip to content

Creating a generic select function in Drizzle ORM #1767

Closed Answered by Angelelz
riculum asked this question in Q&A
Discussion options

You must be logged in to vote

I believe you don't need to be so specific in your generic, I think this should work:

async function getItem<T extends AnyPgTable>(  //  <-- If you're using pg
  schema: T,
): Promise<T["$inferSelect"]> {
  const query = db.select().from(schema).limit(1);

  const [item] = await query;

  return item;
}

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@riculum
Comment options

@Angelelz
Comment options

Angelelz Jan 8, 2024
Collaborator

@ChoaibMouhrach
Comment options

Answer selected by riculum
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants