Page not found
Page not found

Reputation: 3585

How to get the model type in Drizzle ORM?

Suppose I query the database like this:

const post = await db.query.posts.findFirst({ where: ...});

What is the type of post?

Context: I want to create a component that accepts a post like this:

type ComponentProps = {
  post: ???
}

Edit: I realized it ain't possible because the teturn type depends on the where clause so wrapping the db.query in a function f and then use ReturnType<typeof f> is probably the way to go here. Is this correct?

Upvotes: 3

Views: 3692

Answers (1)

Page not found
Page not found

Reputation: 3585

Figured it out.

import { posts } from "~/server/db/schema";
type Post = typeof posts.$inferSelect;

Upvotes: 5

Related Questions