| 1 | import { relations } from "drizzle-orm"; |
| 2 | import { |
| 3 | boolean, |
| 4 | index, |
| 5 | integer, |
| 6 | pgTable, |
| 7 | text, |
| 8 | timestamp, |
| 9 | uniqueIndex, |
| 10 | } from "drizzle-orm/pg-core"; |
| 11 | |
| 12 | export const user = |
| 13 | id: text("id |
| 14 | name: text("name |
| 15 | email: text("email |
| 16 | emailVerified: boolean("email_verified |
| 17 | image: text("image |
| 18 | createdAt: timestamp("created_at |
| 19 | .defaultNow() |
| 20 | .notNull(), |
| 21 | updatedAt: timestamp("updated_at |
| 22 | .defaultNow() |
| 23 | .$onUpdate(() => |
| 24 | .notNull(), |
| 25 | username: text("username |
| 26 | displayUsername: text("display_username |
| 27 | }); |
| 28 | |
| 29 | export const session = |
| 30 | "session", |
| 31 | { |
| 32 | id: text("id |
| 33 | expiresAt: timestamp("expires_at |
| 34 | token: text("token |
| 35 | createdAt: timestamp("created_at |
| 36 | .defaultNow() |
| 37 | .notNull(), |
| 38 | updatedAt: timestamp("updated_at |
| 39 | .$onUpdate(() => |
| 40 | .notNull(), |
| 41 | ipAddress: text("ip_address |
| 42 | userAgent: text("user_agent |
| 43 | userId: text("user_id |
| 44 | .notNull() |
| 45 | .references(() => |
| 46 | activeOrganizationId: text("active_organization_id |
| 47 | activeTeamId: text("active_team_id |
| 48 | }, |
| 49 | (table) => |
| 50 | ); |
| 51 | |
| 52 | export const account = |
| 53 | "account", |
| 54 | { |
| 55 | id: text("id |
| 56 | accountId: text("account_id |
| 57 | providerId: text("provider_id |
| 58 | userId: text("user_id |
| 59 | .notNull() |
| 60 | .references(() => |
| 61 | accessToken: text("access_token |
| 62 | refreshToken: text("refresh_token |
| 63 | idToken: text("id_token |
| 64 | accessTokenExpiresAt: timestamp("access_token_expires_at |
| 65 | withTimezone: true, |
| 66 | }), |
| 67 | refreshTokenExpiresAt: timestamp("refresh_token_expires_at |
| 68 | withTimezone: true, |
| 69 | }), |
| 70 | scope: text("scope |
| 71 | issuer: text("issuer |
| 72 | password: text("password |
| 73 | createdAt: timestamp("created_at |
| 74 | .defaultNow() |
| 75 | .notNull(), |
| 76 | updatedAt: timestamp("updated_at |
| 77 | .$onUpdate(() => |
| 78 | .notNull(), |
| 79 | }, |
| 80 | (table) => |
| 81 | ); |
| 82 | |
| 83 | export const verification = |
| 84 | "verification", |
| 85 | { |
| 86 | id: text("id |
| 87 | identifier: text("identifier |
| 88 | value: text("value |
| 89 | expiresAt: timestamp("expires_at |
| 90 | createdAt: timestamp("created_at |
| 91 | .defaultNow() |
| 92 | .notNull(), |
| 93 | updatedAt: timestamp("updated_at |
| 94 | .defaultNow() |
| 95 | .$onUpdate(() => |
| 96 | .notNull(), |
| 97 | }, |
| 98 | (table) => |
| 99 | ); |
| 100 | |
| 101 | export const organization = |
| 102 | "organization", |
| 103 | { |
| 104 | id: text("id |
| 105 | name: text("name |
| 106 | slug: text("slug |
| 107 | logo: text("logo |
| 108 | createdAt: timestamp("created_at |
| 109 | metadata: text("metadata |
| 110 | }, |
| 111 | (table) => |
| 112 | ); |
| 113 | |
| 114 | export const organizationRole = |
| 115 | "organization_role", |
| 116 | { |
| 117 | id: text("id |
| 118 | organizationId: text("organization_id |
| 119 | .notNull() |
| 120 | .references(() => |
| 121 | role: text("role |
| 122 | permission: text("permission |
| 123 | createdAt: timestamp("created_at |
| 124 | .defaultNow() |
| 125 | .notNull(), |
| 126 | updatedAt: timestamp("updated_at |
| 127 | () => /* @__PURE__ |
| 128 | ), |
| 129 | }, |
| 130 | (table) => |
| 131 | index("organizationRole_organizationId_idx"). |
| 132 | index("organizationRole_role_idx"). |
| 133 | ], |
| 134 | ); |
| 135 | |
| 136 | export const team = |
| 137 | "team", |
| 138 | { |
| 139 | id: text("id |
| 140 | name: text("name |
| 141 | memberCount: integer("member_count |
| 142 | organizationId: text("organization_id |
| 143 | .notNull() |
| 144 | .references(() => |
| 145 | createdAt: timestamp("created_at |
| 146 | updatedAt: timestamp("updated_at |
| 147 | () => /* @__PURE__ |
| 148 | ), |
| 149 | }, |
| 150 | (table) => |
| 151 | ); |
| 152 | |
| 153 | export const teamMember = |
| 154 | "team_member", |
| 155 | { |
| 156 | id: text("id |
| 157 | teamId: text("team_id |
| 158 | .notNull() |
| 159 | .references(() => |
| 160 | userId: text("user_id |
| 161 | .notNull() |
| 162 | .references(() => |
| 163 | membershipKey: text("membership_key |
| 164 | createdAt: timestamp("created_at |
| 165 | }, |
| 166 | (table) => |
| 167 | index("teamMember_teamId_idx"). |
| 168 | index("teamMember_userId_idx"). |
| 169 | ], |
| 170 | ); |
| 171 | |
| 172 | export const member = |
| 173 | "member", |
| 174 | { |
| 175 | id: text("id |
| 176 | organizationId: text("organization_id |
| 177 | .notNull() |
| 178 | .references(() => |
| 179 | userId: text("user_id |
| 180 | .notNull() |
| 181 | .references(() => |
| 182 | role: text("role |
| 183 | createdAt: timestamp("created_at |
| 184 | }, |
| 185 | (table) => |
| 186 | index("member_organizationId_idx"). |
| 187 | index("member_userId_idx"). |
| 188 | ], |
| 189 | ); |
| 190 | |
| 191 | export const invitation = |
| 192 | "invitation", |
| 193 | { |
| 194 | id: text("id |
| 195 | organizationId: text("organization_id |
| 196 | .notNull() |
| 197 | .references(() => |
| 198 | email: text("email |
| 199 | role: text("role |
| 200 | teamId: text("team_id |
| 201 | status: text("status |
| 202 | expiresAt: timestamp("expires_at |
| 203 | createdAt: timestamp("created_at |
| 204 | .defaultNow() |
| 205 | .notNull(), |
| 206 | inviterId: text("inviter_id |
| 207 | .notNull() |
| 208 | .references(() => |
| 209 | }, |
| 210 | (table) => |
| 211 | index("invitation_organizationId_idx"). |
| 212 | index("invitation_email_idx"). |
| 213 | ], |
| 214 | ); |
| 215 | |
| 216 | export const apikey = |
| 217 | "apikey", |
| 218 | { |
| 219 | id: text("id |
| 220 | configId: text("config_id |
| 221 | name: text("name |
| 222 | start: text("start |
| 223 | referenceId: text("reference_id |
| 224 | prefix: text("prefix |
| 225 | key: text("key |
| 226 | refillInterval: integer("refill_interval |
| 227 | refillAmount: integer("refill_amount |
| 228 | lastRefillAt: timestamp("last_refill_at |
| 229 | enabled: boolean("enabled |
| 230 | rateLimitEnabled: boolean("rate_limit_enabled |
| 231 | rateLimitTimeWindow: integer("rate_limit_time_window |
| 232 | rateLimitMax: integer("rate_limit_max |
| 233 | requestCount: integer("request_count |
| 234 | remaining: integer("remaining |
| 235 | lastRequest: timestamp("last_request |
| 236 | expiresAt: timestamp("expires_at |
| 237 | createdAt: timestamp("created_at |
| 238 | updatedAt: timestamp("updated_at |
| 239 | permissions: text("permissions |
| 240 | metadata: text("metadata |
| 241 | }, |
| 242 | (table) => |
| 243 | index("apikey_configId_idx"). |
| 244 | index("apikey_referenceId_idx"). |
| 245 | index("apikey_key_idx"). |
| 246 | ], |
| 247 | ); |
| 248 | |
| 249 | /** |
| 250 | * Additional addresses an account owns, so one person can be reached - and |
| 251 | * recognised as a commit author - under every address they push with. |
| 252 | * |
| 253 | * Better Auth only ever knows `user.email`; these rows are resolved to it |
| 254 | * before its endpoints run. Only a verified row counts, otherwise adding an |
| 255 | * address would be enough to claim someone else's commits or sign-in. |
| 256 | */ |
| 257 | export const userEmail = |
| 258 | "user_email", |
| 259 | { |
| 260 | id: text("id |
| 261 | userId: text("user_id |
| 262 | .notNull() |
| 263 | .references(() => |
| 264 | // Stored lowercased; addresses are matched case-insensitively. |
| 265 | email: text("email |
| 266 | verified: boolean("verified |
| 267 | // Single-use verification token, cleared once the address is confirmed. |
| 268 | token: text("token |
| 269 | tokenExpiresAt: timestamp("token_expires_at |
| 270 | createdAt: timestamp("created_at |
| 271 | .defaultNow() |
| 272 | .notNull(), |
| 273 | }, |
| 274 | (table) => |
| 275 | ); |
| 276 | |
| 277 | export const userEmailRelations = |
| 278 | user: one(user |
| 279 | fields: [userEmail.userId |
| 280 | references: [user.id |
| 281 | }), |
| 282 | })); |
| 283 | |
| 284 | export const userRelations = |
| 285 | sessions: many(session |
| 286 | emails: many(userEmail |
| 287 | accounts: many(account |
| 288 | teamMembers: many(teamMember |
| 289 | members: many(member |
| 290 | invitations: many(invitation |
| 291 | })); |
| 292 | |
| 293 | export const sessionRelations = |
| 294 | user: one(user |
| 295 | fields: [session.userId |
| 296 | references: [user.id |
| 297 | }), |
| 298 | })); |
| 299 | |
| 300 | export const accountRelations = |
| 301 | user: one(user |
| 302 | fields: [account.userId |
| 303 | references: [user.id |
| 304 | }), |
| 305 | })); |
| 306 | |
| 307 | export const organizationRelations = |
| 308 | organizationRoles: many(organizationRole |
| 309 | teams: many(team |
| 310 | members: many(member |
| 311 | invitations: many(invitation |
| 312 | })); |
| 313 | |
| 314 | export const organizationRoleRelations = |
| 315 | organizationRole, |
| 316 | ({ one }) |
| 317 | organization: one(organization |
| 318 | fields: [organizationRole.organizationId |
| 319 | references: [organization.id |
| 320 | }), |
| 321 | }), |
| 322 | ); |
| 323 | |
| 324 | export const teamRelations = |
| 325 | organization: one(organization |
| 326 | fields: [team.organizationId |
| 327 | references: [organization.id |
| 328 | }), |
| 329 | teamMembers: many(teamMember |
| 330 | })); |
| 331 | |
| 332 | export const teamMemberRelations = |
| 333 | team: one(team |
| 334 | fields: [teamMember.teamId |
| 335 | references: [team.id |
| 336 | }), |
| 337 | user: one(user |
| 338 | fields: [teamMember.userId |
| 339 | references: [user.id |
| 340 | }), |
| 341 | })); |
| 342 | |
| 343 | export const memberRelations = |
| 344 | organization: one(organization |
| 345 | fields: [member.organizationId |
| 346 | references: [organization.id |
| 347 | }), |
| 348 | user: one(user |
| 349 | fields: [member.userId |
| 350 | references: [user.id |
| 351 | }), |
| 352 | })); |
| 353 | |
| 354 | export const invitationRelations = |
| 355 | organization: one(organization |
| 356 | fields: [invitation.organizationId |
| 357 | references: [organization.id |
| 358 | }), |
| 359 | user: one(user |
| 360 | fields: [invitation.inviterId |
| 361 | references: [user.id |
| 362 | }), |
| 363 | })); |