| 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 recognised as a commit author - under every address they push with. |
| 251 | * |
| 252 | * Better Auth only ever knows `user.email`; these rows are resolved to it before its endpoints run. Only a verified row counts, otherwise adding an address would be enough to claim someone else's commits or sign-in. |
| 253 | */ |
| 254 | export const userEmail = |
| 255 | "user_email", |
| 256 | { |
| 257 | id: text("id |
| 258 | userId: text("user_id |
| 259 | .notNull() |
| 260 | .references(() => |
| 261 | // Stored lowercased; addresses are matched case-insensitively. |
| 262 | email: text("email |
| 263 | verified: boolean("verified |
| 264 | // Single-use verification token, cleared once the address is confirmed. |
| 265 | token: text("token |
| 266 | tokenExpiresAt: timestamp("token_expires_at |
| 267 | createdAt: timestamp("created_at |
| 268 | .defaultNow() |
| 269 | .notNull(), |
| 270 | }, |
| 271 | (table) => |
| 272 | ); |
| 273 | |
| 274 | export const userEmailRelations = |
| 275 | user: one(user |
| 276 | fields: [userEmail.userId |
| 277 | references: [user.id |
| 278 | }), |
| 279 | })); |
| 280 | |
| 281 | export const userRelations = |
| 282 | sessions: many(session |
| 283 | emails: many(userEmail |
| 284 | gpgKeys: many(userGpgKey |
| 285 | sshKeys: many(userSshKey |
| 286 | accounts: many(account |
| 287 | teamMembers: many(teamMember |
| 288 | members: many(member |
| 289 | invitations: many(invitation |
| 290 | })); |
| 291 | |
| 292 | export const sessionRelations = |
| 293 | user: one(user |
| 294 | fields: [session.userId |
| 295 | references: [user.id |
| 296 | }), |
| 297 | })); |
| 298 | |
| 299 | export const accountRelations = |
| 300 | user: one(user |
| 301 | fields: [account.userId |
| 302 | references: [user.id |
| 303 | }), |
| 304 | })); |
| 305 | |
| 306 | export const organizationRelations = |
| 307 | organizationRoles: many(organizationRole |
| 308 | teams: many(team |
| 309 | members: many(member |
| 310 | invitations: many(invitation |
| 311 | })); |
| 312 | |
| 313 | export const organizationRoleRelations = |
| 314 | organizationRole, |
| 315 | ({ one }) |
| 316 | organization: one(organization |
| 317 | fields: [organizationRole.organizationId |
| 318 | references: [organization.id |
| 319 | }), |
| 320 | }), |
| 321 | ); |
| 322 | |
| 323 | export const teamRelations = |
| 324 | organization: one(organization |
| 325 | fields: [team.organizationId |
| 326 | references: [organization.id |
| 327 | }), |
| 328 | teamMembers: many(teamMember |
| 329 | })); |
| 330 | |
| 331 | export const teamMemberRelations = |
| 332 | team: one(team |
| 333 | fields: [teamMember.teamId |
| 334 | references: [team.id |
| 335 | }), |
| 336 | user: one(user |
| 337 | fields: [teamMember.userId |
| 338 | references: [user.id |
| 339 | }), |
| 340 | })); |
| 341 | |
| 342 | export const memberRelations = |
| 343 | organization: one(organization |
| 344 | fields: [member.organizationId |
| 345 | references: [organization.id |
| 346 | }), |
| 347 | user: one(user |
| 348 | fields: [member.userId |
| 349 | references: [user.id |
| 350 | }), |
| 351 | })); |
| 352 | |
| 353 | export const invitationRelations = |
| 354 | organization: one(organization |
| 355 | fields: [invitation.organizationId |
| 356 | references: [organization.id |
| 357 | }), |
| 358 | user: one(user |
| 359 | fields: [invitation.inviterId |
| 360 | references: [user.id |
| 361 | }), |
| 362 | })); |
| 363 | |
| 364 | /** OpenPGP public keys an account uploads so its signed commits can be shown as verified. */ |
| 365 | export const userGpgKey = |
| 366 | "user_gpg_key", |
| 367 | { |
| 368 | id: text("id |
| 369 | userId: text("user_id |
| 370 | .notNull() |
| 371 | .references(() => |
| 372 | // Long key id of the primary key, lowercase hex. |
| 373 | keyId: text("key_id |
| 374 | fingerprint: text("fingerprint |
| 375 | // Armored public key, exactly as it was uploaded. |
| 376 | publicKey: text("public_key |
| 377 | createdAt: timestamp("created_at |
| 378 | .defaultNow() |
| 379 | .notNull(), |
| 380 | }, |
| 381 | (table) => |
| 382 | ); |
| 383 | |
| 384 | export const userGpgKeyRelations = |
| 385 | user: one(user |
| 386 | fields: [userGpgKey.userId |
| 387 | references: [user.id |
| 388 | }), |
| 389 | })); |
| 390 | |
| 391 | /** OpenSSH public keys an account uploads so it can push and fetch over SSH. */ |
| 392 | export const userSshKey = |
| 393 | "user_ssh_key", |
| 394 | { |
| 395 | id: text("id |
| 396 | userId: text("user_id |
| 397 | .notNull() |
| 398 | .references(() => |
| 399 | title: text("title |
| 400 | // SHA256 of the key blob, base64 without padding - what `ssh-keygen -lf` prints after `SHA256:`. |
| 401 | fingerprint: text("fingerprint |
| 402 | // The key blob, base64, as the second field of an authorized_keys line. |
| 403 | publicKey: text("public_key |
| 404 | lastUsedAt: timestamp("last_used_at |
| 405 | createdAt: timestamp("created_at |
| 406 | .defaultNow() |
| 407 | .notNull(), |
| 408 | }, |
| 409 | (table) => |
| 410 | ); |
| 411 | |
| 412 | export const userSshKeyRelations = |
| 413 | user: one(user |
| 414 | fields: [userSshKey.userId |
| 415 | references: [user.id |
| 416 | }), |
| 417 | })); |