| 1 | import { relations } from "drizzle-orm"; |
| 2 | import { |
| 3 | boolean, |
| 4 | index, |
| 5 | int, |
| 6 | mysqlTable, |
| 7 | text, |
| 8 | timestamp, |
| 9 | varchar, |
| 10 | } from "drizzle-orm/mysql-core"; |
| 11 | |
| 12 | export const user = |
| 13 | id: varchar("id |
| 14 | name: varchar("name |
| 15 | email: varchar("email |
| 16 | emailVerified: boolean("email_verified |
| 17 | image: text("image |
| 18 | createdAt: timestamp("created_at |
| 19 | updatedAt: timestamp("updated_at |
| 20 | .defaultNow() |
| 21 | .$onUpdate(() => |
| 22 | .notNull(), |
| 23 | // admin plugin |
| 24 | role: varchar("role |
| 25 | banned: boolean("banned |
| 26 | banReason: text("ban_reason |
| 27 | banExpires: timestamp("ban_expires |
| 28 | // twoFactor plugin |
| 29 | twoFactorEnabled: boolean("two_factor_enabled |
| 30 | // billing address |
| 31 | billingName: varchar("billing_name |
| 32 | billingAddressLine1: varchar("billing_address_line1 |
| 33 | billingAddressLine2: varchar("billing_address_line2 |
| 34 | billingCity: varchar("billing_city |
| 35 | billingState: varchar("billing_state |
| 36 | billingPostalCode: varchar("billing_postal_code |
| 37 | billingCountry: varchar("billing_country |
| 38 | // VAT |
| 39 | vatNumber: varchar("vat_number |
| 40 | vatCountry: varchar("vat_country |
| 41 | // i18n |
| 42 | locale: varchar("locale |
| 43 | notificationDiscordWebhook: text("notification_discord_webhook |
| 44 | notificationTelegramToken: text("notification_telegram_token |
| 45 | notificationTelegramChatId: text("notification_telegram_chat_id |
| 46 | }); |
| 47 | |
| 48 | export const session = |
| 49 | "session", |
| 50 | { |
| 51 | id: varchar("id |
| 52 | expiresAt: timestamp("expires_at |
| 53 | token: varchar("token |
| 54 | createdAt: timestamp("created_at |
| 55 | updatedAt: timestamp("updated_at |
| 56 | .$onUpdate(() => |
| 57 | .notNull(), |
| 58 | ipAddress: text("ip_address |
| 59 | userAgent: text("user_agent |
| 60 | userId: varchar("user_id |
| 61 | .notNull() |
| 62 | .references(() => |
| 63 | // admin plugin (impersonation) |
| 64 | impersonatedBy: varchar("impersonated_by |
| 65 | }, |
| 66 | (table) => |
| 67 | ); |
| 68 | |
| 69 | export const account = |
| 70 | "account", |
| 71 | { |
| 72 | id: varchar("id |
| 73 | accountId: text("account_id |
| 74 | providerId: text("provider_id |
| 75 | userId: varchar("user_id |
| 76 | .notNull() |
| 77 | .references(() => |
| 78 | accessToken: text("access_token |
| 79 | refreshToken: text("refresh_token |
| 80 | idToken: text("id_token |
| 81 | accessTokenExpiresAt: timestamp("access_token_expires_at |
| 82 | refreshTokenExpiresAt: timestamp("refresh_token_expires_at |
| 83 | scope: text("scope |
| 84 | password: text("password |
| 85 | createdAt: timestamp("created_at |
| 86 | updatedAt: timestamp("updated_at |
| 87 | .$onUpdate(() => |
| 88 | .notNull(), |
| 89 | }, |
| 90 | (table) => |
| 91 | ); |
| 92 | |
| 93 | export const verification = |
| 94 | "verification", |
| 95 | { |
| 96 | id: varchar("id |
| 97 | identifier: varchar("identifier |
| 98 | value: text("value |
| 99 | expiresAt: timestamp("expires_at |
| 100 | createdAt: timestamp("created_at |
| 101 | updatedAt: timestamp("updated_at |
| 102 | .defaultNow() |
| 103 | .$onUpdate(() => |
| 104 | .notNull(), |
| 105 | }, |
| 106 | (table) => |
| 107 | ); |
| 108 | |
| 109 | export const twoFactor = |
| 110 | id: varchar("id |
| 111 | secret: text("secret |
| 112 | backupCodes: text("backup_codes |
| 113 | userId: varchar("user_id |
| 114 | .notNull() |
| 115 | .references(() => |
| 116 | verified: boolean("verified |
| 117 | }); |
| 118 | |
| 119 | export const apikey = |
| 120 | "apikey", |
| 121 | { |
| 122 | id: varchar("id |
| 123 | configId: varchar("config_id |
| 124 | name: varchar("name |
| 125 | start: varchar("start |
| 126 | prefix: varchar("prefix |
| 127 | key: text("key |
| 128 | referenceId: varchar("reference_id |
| 129 | refillInterval: int("refill_interval |
| 130 | refillAmount: int("refill_amount |
| 131 | lastRefillAt: timestamp("last_refill_at |
| 132 | enabled: boolean("enabled |
| 133 | rateLimitEnabled: boolean("rate_limit_enabled |
| 134 | rateLimitTimeWindow: int("rate_limit_time_window |
| 135 | rateLimitMax: int("rate_limit_max |
| 136 | requestCount: int("request_count |
| 137 | remaining: int("remaining |
| 138 | lastRequest: timestamp("last_request |
| 139 | expiresAt: timestamp("expires_at |
| 140 | createdAt: timestamp("created_at |
| 141 | updatedAt: timestamp("updated_at |
| 142 | .defaultNow() |
| 143 | .$onUpdate(() => |
| 144 | .notNull(), |
| 145 | permissions: text("permissions |
| 146 | metadata: text("metadata |
| 147 | }, |
| 148 | (table) => |
| 149 | ); |
| 150 | |
| 151 | export const userRelations = |
| 152 | sessions: many(session |
| 153 | accounts: many(account |
| 154 | twoFactors: many(twoFactor |
| 155 | })); |
| 156 | |
| 157 | export const sessionRelations = |
| 158 | user: one(user |
| 159 | fields: [session.userId |
| 160 | references: [user.id |
| 161 | }), |
| 162 | })); |
| 163 | |
| 164 | export const accountRelations = |
| 165 | user: one(user |
| 166 | fields: [account.userId |
| 167 | references: [user.id |
| 168 | }), |
| 169 | })); |
| 170 | |
| 171 | export const twoFactorRelations = |
| 172 | user: one(user |
| 173 | fields: [twoFactor.userId |
| 174 | references: [user.id |
| 175 | }), |
| 176 | })); |