|
| 1 | +import { relations } from "drizzle-orm"; |
1 | 2 | import { |
2 | 3 | mysqlTable, |
3 | 4 | varchar, |
4 | 5 | text, |
5 | 6 | timestamp, |
6 | 7 | boolean, |
| 8 | + index, |
7 | 9 | } from "drizzle-orm/mysql-core"; |
8 | 10 |
|
9 | 11 | export const user = mysqlTable("user", { |
10 | 12 | id: varchar("id", { length: 36 }).primaryKey(), |
11 | | - name: text("name").notNull(), |
| 13 | + name: varchar("name", { length: 255 }).notNull(), |
12 | 14 | email: varchar("email", { length: 255 }).notNull().unique(), |
13 | | - emailVerified: boolean("email_verified").notNull(), |
| 15 | + emailVerified: boolean("email_verified").default(false).notNull(), |
14 | 16 | image: text("image"), |
15 | | - createdAt: timestamp("created_at").notNull(), |
16 | | - updatedAt: timestamp("updated_at").notNull(), |
| 17 | + createdAt: timestamp("created_at", { fsp: 3 }).defaultNow().notNull(), |
| 18 | + updatedAt: timestamp("updated_at", { fsp: 3 }) |
| 19 | + .defaultNow() |
| 20 | + .$onUpdate(() => /* @__PURE__ */ new Date()) |
| 21 | + .notNull(), |
17 | 22 | }); |
18 | 23 |
|
19 | | -export const session = mysqlTable("session", { |
20 | | - id: varchar("id", { length: 36 }).primaryKey(), |
21 | | - expiresAt: timestamp("expires_at").notNull(), |
22 | | - token: varchar("token", { length: 255 }).notNull().unique(), |
23 | | - createdAt: timestamp("created_at").notNull(), |
24 | | - updatedAt: timestamp("updated_at").notNull(), |
25 | | - ipAddress: text("ip_address"), |
26 | | - userAgent: text("user_agent"), |
27 | | - userId: varchar("user_id", { length: 36 }) |
28 | | - .notNull() |
29 | | - .references(() => user.id, { onDelete: "cascade" }), |
30 | | -}); |
| 24 | +export const session = mysqlTable( |
| 25 | + "session", |
| 26 | + { |
| 27 | + id: varchar("id", { length: 36 }).primaryKey(), |
| 28 | + expiresAt: timestamp("expires_at", { fsp: 3 }).notNull(), |
| 29 | + token: varchar("token", { length: 255 }).notNull().unique(), |
| 30 | + createdAt: timestamp("created_at", { fsp: 3 }).defaultNow().notNull(), |
| 31 | + updatedAt: timestamp("updated_at", { fsp: 3 }) |
| 32 | + .$onUpdate(() => /* @__PURE__ */ new Date()) |
| 33 | + .notNull(), |
| 34 | + ipAddress: text("ip_address"), |
| 35 | + userAgent: text("user_agent"), |
| 36 | + userId: varchar("user_id", { length: 36 }) |
| 37 | + .notNull() |
| 38 | + .references(() => user.id, { onDelete: "cascade" }), |
| 39 | + }, |
| 40 | + (table) => [index("session_userId_idx").on(table.userId)], |
| 41 | +); |
31 | 42 |
|
32 | | -export const account = mysqlTable("account", { |
33 | | - id: varchar("id", { length: 36 }).primaryKey(), |
34 | | - accountId: text("account_id").notNull(), |
35 | | - providerId: text("provider_id").notNull(), |
36 | | - userId: varchar("user_id", { length: 36 }) |
37 | | - .notNull() |
38 | | - .references(() => user.id, { onDelete: "cascade" }), |
39 | | - accessToken: text("access_token"), |
40 | | - refreshToken: text("refresh_token"), |
41 | | - idToken: text("id_token"), |
| 43 | +export const account = mysqlTable( |
| 44 | + "account", |
| 45 | + { |
| 46 | + id: varchar("id", { length: 36 }).primaryKey(), |
| 47 | + accountId: text("account_id").notNull(), |
| 48 | + providerId: text("provider_id").notNull(), |
| 49 | + userId: varchar("user_id", { length: 36 }) |
| 50 | + .notNull() |
| 51 | + .references(() => user.id, { onDelete: "cascade" }), |
| 52 | + accessToken: text("access_token"), |
| 53 | + refreshToken: text("refresh_token"), |
| 54 | + idToken: text("id_token"), |
| 55 | + accessTokenExpiresAt: timestamp("access_token_expires_at", { fsp: 3 }), |
| 56 | + refreshTokenExpiresAt: timestamp("refresh_token_expires_at", { fsp: 3 }), |
| 57 | + scope: text("scope"), |
| 58 | + password: text("password"), |
| 59 | + createdAt: timestamp("created_at", { fsp: 3 }).defaultNow().notNull(), |
| 60 | + updatedAt: timestamp("updated_at", { fsp: 3 }) |
| 61 | + .$onUpdate(() => /* @__PURE__ */ new Date()) |
| 62 | + .notNull(), |
| 63 | + }, |
| 64 | + (table) => [index("account_userId_idx").on(table.userId)], |
| 65 | +); |
42 | 66 |
|
43 | | - accessTokenExpiresAt: timestamp("access_token_expires_at"), |
44 | | - refreshTokenExpiresAt: timestamp("refresh_token_expires_at"), |
45 | | - scope: text("scope"), |
46 | | - password: text("password"), |
47 | | - createdAt: timestamp("created_at").notNull(), |
48 | | - updatedAt: timestamp("updated_at").notNull(), |
49 | | -}); |
| 67 | +export const verification = mysqlTable( |
| 68 | + "verification", |
| 69 | + { |
| 70 | + id: varchar("id", { length: 36 }).primaryKey(), |
| 71 | + identifier: varchar("identifier", { length: 255 }).notNull(), |
| 72 | + value: text("value").notNull(), |
| 73 | + expiresAt: timestamp("expires_at", { fsp: 3 }).notNull(), |
| 74 | + createdAt: timestamp("created_at", { fsp: 3 }).defaultNow().notNull(), |
| 75 | + updatedAt: timestamp("updated_at", { fsp: 3 }) |
| 76 | + .defaultNow() |
| 77 | + .$onUpdate(() => /* @__PURE__ */ new Date()) |
| 78 | + .notNull(), |
| 79 | + }, |
| 80 | + (table) => [index("verification_identifier_idx").on(table.identifier)], |
| 81 | +); |
50 | 82 |
|
51 | | -export const verification = mysqlTable("verification", { |
52 | | - id: varchar("id", { length: 36 }).primaryKey(), |
53 | | - identifier: text("identifier").notNull(), |
54 | | - value: text("value").notNull(), |
55 | | - expiresAt: timestamp("expires_at").notNull(), |
56 | | - createdAt: timestamp("created_at"), |
57 | | - updatedAt: timestamp("updated_at"), |
58 | | -}); |
| 83 | +export const userRelations = relations(user, ({ many }) => ({ |
| 84 | + sessions: many(session), |
| 85 | + accounts: many(account), |
| 86 | +})); |
| 87 | + |
| 88 | +export const sessionRelations = relations(session, ({ one }) => ({ |
| 89 | + user: one(user, { |
| 90 | + fields: [session.userId], |
| 91 | + references: [user.id], |
| 92 | + }), |
| 93 | +})); |
| 94 | + |
| 95 | +export const accountRelations = relations(account, ({ one }) => ({ |
| 96 | + user: one(user, { |
| 97 | + fields: [account.userId], |
| 98 | + references: [user.id], |
| 99 | + }), |
| 100 | +})); |
0 commit comments