| 1 | import { defineRelations } from "drizzle-orm"; |
| 2 | import { int, mysqlTable, timestamp, varchar } from "drizzle-orm/mysql-core"; |
| 3 | |
| 4 | export const Users = mysqlTable("users", { |
| 5 | id: int("id").primaryKey().autoincrement(), |
| 6 | email: varchar("email", { length: 255 }).notNull().unique(), |
| 7 | name: varchar("name", { length: 255 }).notNull(), |
| 8 | createdAt: timestamp("created_at").notNull().defaultNow(), |
| 9 | }); |
| 10 | export type User = typeof Users.$inferSelect; |
| 11 | |
| 12 | export const Posts = mysqlTable("posts", { |
| 13 | id: int("id").primaryKey().autoincrement(), |
| 14 | userId: int("user_id").notNull(), |
| 15 | title: varchar("title", { length: 255 }).notNull(), |
| 16 | body: varchar("body", { length: 4096 }).notNull(), |
| 17 | createdAt: timestamp("created_at").notNull().defaultNow(), |
| 18 | }); |
| 19 | export type Post = typeof Posts.$inferSelect; |
| 20 | |
| 21 | export const relations = defineRelations({ Users, Posts }, (t) => ({ |
| 22 | Users: { |
| 23 | posts: t.many.Posts(), |
| 24 | }, |
| 25 | Posts: { |
| 26 | user: t.one.Users({ |
| 27 | from: t.Posts.userId, |
| 28 | to: t.Users.id, |
| 29 | }), |
| 30 | }, |
| 31 | })); |