1import { defineRelations } from "drizzle-orm";
2import { integer, pgTable, serial, text, timestamp } from "drizzle-orm/pg-core";
3
4export const Users = pgTable("users", {
5 id: serial("id").primaryKey(),
6 email: text("email").notNull().unique(),
7 name: text("name").notNull(),
8 createdAt: timestamp("created_at", { withTimezone: true })
9 .notNull()
10 .defaultNow(),
11});
12export type User = typeof Users.$inferSelect;
13
14export const Posts = pgTable("posts", {
15 id: serial("id").primaryKey(),
16 userId: integer("user_id")
17 .notNull()
18 .references(() => Users.id, { onDelete: "cascade" }),
19 title: text("title").notNull(),
20 body: text("body").notNull(),
21 createdAt: timestamp("created_at", { withTimezone: true })
22 .notNull()
23 .defaultNow(),
24});
25export type Post = typeof Posts.$inferSelect;
26
27export const relations = defineRelations({ Users, Posts }, (t) => ({
28 Users: {
29 posts: t.many.Posts(),
30 },
31 Posts: {
32 user: t.one.Users({
33 from: t.Posts.userId,
34 to: t.Users.id,
35 }),
36 },
37}));