|
| 1 | +import { relations } from 'drizzle-orm'; |
| 2 | +import { |
| 3 | + integer, |
| 4 | + pgEnum, |
| 5 | + pgTable, |
| 6 | + primaryKey, |
| 7 | + text, |
| 8 | + timestamp, |
| 9 | +} from 'drizzle-orm/pg-core'; |
| 10 | +import { type AdapterAccount } from 'next-auth/adapters'; |
| 11 | +import { file } from './file'; |
| 12 | + |
| 13 | +export const yearEnum = pgEnum('year', [ |
| 14 | + 'Freshman', |
| 15 | + 'Sophomore', |
| 16 | + 'Junior', |
| 17 | + 'Senior', |
| 18 | + 'Grad Student', |
| 19 | +]); |
| 20 | + |
| 21 | +export const user = pgTable('user', { |
| 22 | + id: text('id').notNull().primaryKey(), |
| 23 | + name: text('name'), |
| 24 | + email: text('email').notNull(), |
| 25 | + emailVerified: timestamp('emailVerified', { mode: 'date' }), |
| 26 | + image: text('image'), |
| 27 | +}); |
| 28 | + |
| 29 | +export const userMetadata = pgTable('user_metadata', { |
| 30 | + id: text('id').notNull().primaryKey(), |
| 31 | + firstName: text('first_name').notNull(), |
| 32 | + lastName: text('last_name').notNull(), |
| 33 | + major: text('major').notNull(), |
| 34 | + minor: text('minor'), |
| 35 | + year: yearEnum('year') |
| 36 | + .$default(() => 'Freshman') |
| 37 | + .notNull(), |
| 38 | +}); |
| 39 | + |
| 40 | +export const accounts = pgTable( |
| 41 | + 'account', |
| 42 | + { |
| 43 | + userId: text('userId') |
| 44 | + .notNull() |
| 45 | + .references(() => user.id, { onDelete: 'cascade' }), |
| 46 | + type: text('type').$type<AdapterAccount['type']>().notNull(), |
| 47 | + provider: text('provider').notNull(), |
| 48 | + providerAccountId: text('providerAccountId').notNull(), |
| 49 | + refresh_token: text('refresh_token'), |
| 50 | + access_token: text('access_token'), |
| 51 | + expires_at: integer('expires_at'), |
| 52 | + token_type: text('token_type'), |
| 53 | + scope: text('scope'), |
| 54 | + id_token: text('id_token'), |
| 55 | + session_state: text('session_state'), |
| 56 | + }, |
| 57 | + (t) => [primaryKey({ columns: [t.provider, t.providerAccountId] })], |
| 58 | +); |
| 59 | + |
| 60 | +export const sessions = pgTable('session', { |
| 61 | + sessionToken: text('sessionToken').notNull().primaryKey(), |
| 62 | + userId: text('userId') |
| 63 | + .notNull() |
| 64 | + .references(() => user.id, { onDelete: 'cascade' }), |
| 65 | + expires: timestamp('expires', { mode: 'date' }).notNull(), |
| 66 | +}); |
| 67 | + |
| 68 | +export const verificationTokens = pgTable( |
| 69 | + 'verificationToken', |
| 70 | + { |
| 71 | + identifier: text('identifier').notNull(), |
| 72 | + token: text('token').notNull(), |
| 73 | + expires: timestamp('expires', { mode: 'date' }).notNull(), |
| 74 | + }, |
| 75 | + (vt) => [primaryKey({ columns: [vt.identifier, vt.token] })], |
| 76 | +); |
| 77 | +export const userMetadataRelations = relations(user, ({ many }) => ({ |
| 78 | + files: many(file), |
| 79 | +})); |
0 commit comments