xargr
xargr

Reputation: 3096

how to I get the drizzle-orm relations join return type?

here is my drizzle-orm schema

export const user = pgTable('users', {
  id: serial("id").primaryKey(),
  createdAt: timestamp("created_at").defaultNow(),
  updatedAt: timestamp("updated_at").$onUpdate(() => new Date()),
  name: varchar("name").notNull()
});

export const post = pgTable('posts', {
  id: serial("id").primaryKey(),
  createdAt: timestamp("created_at").defaultNow(),
  updatedAt: timestamp("updated_at").$onUpdate(() => new Date())
  title: varchar("title").notNull(),
  userId: integer("user_id").notNull().references(() => user.id)
});

export type AddUser = typeof user.$inferInsert;
export type GetUser = typeof user.$inferSelect;

export type AddPost = typeof post.$inferInsert;
export type GetPost = typeof post.$inferSelect;


export const userRelations = relations(user, ({ many }) => ({
  posts: many(post),
}));

export const postRelations = relations(post, ({ one }) => ({
  user: one(user, {
    fields: [post.userId],
    references: [user.id],
  }),
}));

how to get types for joins

const usersWithPost = await db.query.user.findMany({
        with: {
            posts: true,
        }
    });

Upvotes: 0

Views: 484

Answers (1)

xargr
xargr

Reputation: 3096

there we are

type UserWithPost = GetUser & { posts: GetPost[] };

Upvotes: -1

Related Questions