schema.ts112 lines, 3.3 KB
| 1 | import { relations } from "drizzle-orm"; |
| 2 | import { |
| 3 | integer, |
| 4 | pgEnum, |
| 5 | pgTable, |
| 6 | serial, |
| 7 | timestamp, |
| 8 | varchar, |
| 9 | text, |
| 10 | primaryKey, |
| 11 | boolean, |
| 12 | } from "drizzle-orm/pg-core"; |
| 13 | import type { AdapterAccount } from "next-auth/adapters"; |
| 14 | |
| 15 | // Define user roles enum |
| 16 | export const userRoleEnum = pgEnum("user_role", ["user", "admin"]); |
| 17 | |
| 18 | // Define user table |
| 19 | export const users = pgTable("users", { |
| 20 | id: varchar("id", { length: 255 }).notNull().primaryKey(), |
| 21 | name: varchar("name", { length: 255 }), |
| 22 | email: varchar("email", { length: 255 }).notNull().unique(), |
| 23 | emailVerified: timestamp("emailVerified", { mode: "date" }), |
| 24 | image: text("image"), |
| 25 | password: text("password"), |
| 26 | role: userRoleEnum("role").default("user").notNull(), |
| 27 | createdAt: timestamp("created_at").notNull().defaultNow(), |
| 28 | updatedAt: timestamp("updated_at").notNull().defaultNow(), |
| 29 | }); |
| 30 | |
| 31 | export const accounts = pgTable( |
| 32 | "accounts", |
| 33 | { |
| 34 | userId: varchar("user_id", { length: 255 }) |
| 35 | .notNull() |
| 36 | .references(() => users.id, { onDelete: "cascade" }), |
| 37 | type: varchar("type", { length: 255 }) |
| 38 | .$type<AdapterAccount["type"]>() |
| 39 | .notNull(), |
| 40 | provider: varchar("provider", { length: 255 }).notNull(), |
| 41 | providerAccountId: varchar("provider_account_id", { |
| 42 | length: 255, |
| 43 | }).notNull(), |
| 44 | refresh_token: text("refresh_token"), |
| 45 | access_token: text("access_token"), |
| 46 | expires_at: integer("expires_at"), |
| 47 | token_type: varchar("token_type", { length: 255 }), |
| 48 | scope: varchar("scope", { length: 255 }), |
| 49 | id_token: text("id_token"), |
| 50 | session_state: varchar("session_state", { length: 255 }), |
| 51 | }, |
| 52 | (account) => [ |
| 53 | { |
| 54 | compoundKey: primaryKey({ |
| 55 | columns: [account.provider, account.providerAccountId], |
| 56 | }), |
| 57 | }, |
| 58 | ] |
| 59 | ); |
| 60 | |
| 61 | export const sessions = pgTable("sessions", { |
| 62 | sessionToken: varchar("session_token", { length: 255 }) |
| 63 | .notNull() |
| 64 | .primaryKey(), |
| 65 | userId: varchar("user_id", { length: 255 }) |
| 66 | .notNull() |
| 67 | .references(() => users.id, { onDelete: "cascade" }), |
| 68 | expires: timestamp("expires", { mode: "date" }).notNull(), |
| 69 | }); |
| 70 | |
| 71 | export const verificationTokens = pgTable( |
| 72 | "verification_token", |
| 73 | { |
| 74 | identifier: varchar("identifier", { length: 255 }).notNull(), |
| 75 | token: varchar("token", { length: 255 }).notNull(), |
| 76 | expires: timestamp("expires", { mode: "date" }).notNull(), |
| 77 | }, |
| 78 | (verificationToken) => [ |
| 79 | { |
| 80 | compositePk: primaryKey({ |
| 81 | columns: [verificationToken.identifier, verificationToken.token], |
| 82 | }), |
| 83 | }, |
| 84 | ] |
| 85 | ); |
| 86 | |
| 87 | export const urls = pgTable("urls", { |
| 88 | id: serial("id").primaryKey(), |
| 89 | originalUrl: varchar("original_url", { length: 2000 }).notNull(), |
| 90 | shortCode: varchar("short_code", { length: 10 }).notNull().unique(), |
| 91 | createdAt: timestamp("created_at").notNull().defaultNow(), |
| 92 | updatedAt: timestamp("updated_at").notNull().defaultNow(), |
| 93 | clicks: integer("clicks").default(0).notNull(), |
| 94 | userId: varchar("user_id", { length: 255 }).references(() => users.id, { |
| 95 | onDelete: "set null", |
| 96 | }), |
| 97 | flagged: boolean("flagged").default(false).notNull(), |
| 98 | flagReason: text("flag_reason"), |
| 99 | }); |
| 100 | |
| 101 | export const usersRelations = relations(users, ({ many }) => ({ |
| 102 | urls: many(urls), |
| 103 | accounts: many(accounts), |
| 104 | sessions: many(sessions), |
| 105 | })); |
| 106 | |
| 107 | export const urlsRelations = relations(urls, ({ one }) => ({ |
| 108 | user: one(users, { |
| 109 | fields: [urls.userId], |
| 110 | references: [users.id], |
| 111 | }), |
| 112 | })); |