Reputation: 3096
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