| 1 | import { relations } from "drizzle-orm"; |
| 2 | import { |
| 3 | bigint, |
| 4 | boolean, |
| 5 | index, |
| 6 | integer, |
| 7 | pgTable, |
| 8 | text, |
| 9 | timestamp, |
| 10 | uniqueIndex, |
| 11 | } from "drizzle-orm/pg-core"; |
| 12 | |
| 13 | export const user = |
| 14 | id: text("id |
| 15 | // Integer id for the GitHub compatibility layer only (REST `id`, GraphQL `databaseId`); nothing references it and Ghost's own API does not expose it. |
| 16 | numericId: bigint("numeric_id |
| 17 | .generatedAlwaysAsIdentity() |
| 18 | .notNull() |
| 19 | .unique(), |
| 20 | name: text("name |
| 21 | email: text("email |
| 22 | emailVerified: boolean("email_verified |
| 23 | image: text("image |
| 24 | createdAt: timestamp("created_at |
| 25 | .defaultNow() |
| 26 | .notNull(), |
| 27 | updatedAt: timestamp("updated_at |
| 28 | .defaultNow() |
| 29 | .$onUpdate(() => |
| 30 | .notNull(), |
| 31 | username: text("username |
| 32 | displayUsername: text("display_username |
| 33 | }); |
| 34 | |
| 35 | export const session = |
| 36 | "session", |
| 37 | { |
| 38 | id: text("id |
| 39 | expiresAt: timestamp("expires_at |
| 40 | token: text("token |
| 41 | createdAt: timestamp("created_at |
| 42 | .defaultNow() |
| 43 | .notNull(), |
| 44 | updatedAt: timestamp("updated_at |
| 45 | .$onUpdate(() => |
| 46 | .notNull(), |
| 47 | ipAddress: text("ip_address |
| 48 | userAgent: text("user_agent |
| 49 | userId: text("user_id |
| 50 | .notNull() |
| 51 | .references(() => |
| 52 | activeOrganizationId: text("active_organization_id |
| 53 | activeTeamId: text("active_team_id |
| 54 | }, |
| 55 | (table) => |
| 56 | ); |
| 57 | |
| 58 | export const account = |
| 59 | "account", |
| 60 | { |
| 61 | id: text("id |
| 62 | accountId: text("account_id |
| 63 | providerId: text("provider_id |
| 64 | userId: text("user_id |
| 65 | .notNull() |
| 66 | .references(() => |
| 67 | accessToken: text("access_token |
| 68 | refreshToken: text("refresh_token |
| 69 | idToken: text("id_token |
| 70 | accessTokenExpiresAt: timestamp("access_token_expires_at |
| 71 | withTimezone: true, |
| 72 | }), |
| 73 | refreshTokenExpiresAt: timestamp("refresh_token_expires_at |
| 74 | withTimezone: true, |
| 75 | }), |
| 76 | scope: text("scope |
| 77 | issuer: text("issuer |
| 78 | password: text("password |
| 79 | createdAt: timestamp("created_at |
| 80 | .defaultNow() |
| 81 | .notNull(), |
| 82 | updatedAt: timestamp("updated_at |
| 83 | .$onUpdate(() => |
| 84 | .notNull(), |
| 85 | }, |
| 86 | (table) => |
| 87 | ); |
| 88 | |
| 89 | export const verification = |
| 90 | "verification", |
| 91 | { |
| 92 | id: text("id |
| 93 | identifier: text("identifier |
| 94 | value: text("value |
| 95 | expiresAt: timestamp("expires_at |
| 96 | createdAt: timestamp("created_at |
| 97 | .defaultNow() |
| 98 | .notNull(), |
| 99 | updatedAt: timestamp("updated_at |
| 100 | .defaultNow() |
| 101 | .$onUpdate(() => |
| 102 | .notNull(), |
| 103 | }, |
| 104 | (table) => |
| 105 | ); |
| 106 | |
| 107 | export const organization = |
| 108 | "organization", |
| 109 | { |
| 110 | id: text("id |
| 111 | // Integer id for the GitHub compatibility layer only (REST `id`, GraphQL `databaseId`); nothing references it and Ghost's own API does not expose it. |
| 112 | numericId: bigint("numeric_id |
| 113 | .generatedAlwaysAsIdentity() |
| 114 | .notNull() |
| 115 | .unique(), |
| 116 | name: text("name |
| 117 | slug: text("slug |
| 118 | logo: text("logo |
| 119 | createdAt: timestamp("created_at |
| 120 | metadata: text("metadata |
| 121 | }, |
| 122 | (table) => |
| 123 | ); |
| 124 | |
| 125 | export const organizationRole = |
| 126 | "organization_role", |
| 127 | { |
| 128 | id: text("id |
| 129 | organizationId: text("organization_id |
| 130 | .notNull() |
| 131 | .references(() => |
| 132 | role: text("role |
| 133 | permission: text("permission |
| 134 | createdAt: timestamp("created_at |
| 135 | .defaultNow() |
| 136 | .notNull(), |
| 137 | updatedAt: timestamp("updated_at |
| 138 | () => /* @__PURE__ |
| 139 | ), |
| 140 | }, |
| 141 | (table) => |
| 142 | index("organizationRole_organizationId_idx"). |
| 143 | index("organizationRole_role_idx"). |
| 144 | ], |
| 145 | ); |
| 146 | |
| 147 | export const team = |
| 148 | "team", |
| 149 | { |
| 150 | id: text("id |
| 151 | name: text("name |
| 152 | memberCount: integer("member_count |
| 153 | organizationId: text("organization_id |
| 154 | .notNull() |
| 155 | .references(() => |
| 156 | createdAt: timestamp("created_at |
| 157 | updatedAt: timestamp("updated_at |
| 158 | () => /* @__PURE__ |
| 159 | ), |
| 160 | }, |
| 161 | (table) => |
| 162 | ); |
| 163 | |
| 164 | export const teamMember = |
| 165 | "team_member", |
| 166 | { |
| 167 | id: text("id |
| 168 | teamId: text("team_id |
| 169 | .notNull() |
| 170 | .references(() => |
| 171 | userId: text("user_id |
| 172 | .notNull() |
| 173 | .references(() => |
| 174 | membershipKey: text("membership_key |
| 175 | createdAt: timestamp("created_at |
| 176 | }, |
| 177 | (table) => |
| 178 | index("teamMember_teamId_idx"). |
| 179 | index("teamMember_userId_idx"). |
| 180 | ], |
| 181 | ); |
| 182 | |
| 183 | export const member = |
| 184 | "member", |
| 185 | { |
| 186 | id: text("id |
| 187 | organizationId: text("organization_id |
| 188 | .notNull() |
| 189 | .references(() => |
| 190 | userId: text("user_id |
| 191 | .notNull() |
| 192 | .references(() => |
| 193 | role: text("role |
| 194 | createdAt: timestamp("created_at |
| 195 | }, |
| 196 | (table) => |
| 197 | index("member_organizationId_idx"). |
| 198 | index("member_userId_idx"). |
| 199 | ], |
| 200 | ); |
| 201 | |
| 202 | export const invitation = |
| 203 | "invitation", |
| 204 | { |
| 205 | id: text("id |
| 206 | organizationId: text("organization_id |
| 207 | .notNull() |
| 208 | .references(() => |
| 209 | email: text("email |
| 210 | role: text("role |
| 211 | teamId: text("team_id |
| 212 | status: text("status |
| 213 | expiresAt: timestamp("expires_at |
| 214 | createdAt: timestamp("created_at |
| 215 | .defaultNow() |
| 216 | .notNull(), |
| 217 | inviterId: text("inviter_id |
| 218 | .notNull() |
| 219 | .references(() => |
| 220 | }, |
| 221 | (table) => |
| 222 | index("invitation_organizationId_idx"). |
| 223 | index("invitation_email_idx"). |
| 224 | ], |
| 225 | ); |
| 226 | |
| 227 | export const apikey = |
| 228 | "apikey", |
| 229 | { |
| 230 | id: text("id |
| 231 | configId: text("config_id |
| 232 | name: text("name |
| 233 | start: text("start |
| 234 | referenceId: text("reference_id |
| 235 | prefix: text("prefix |
| 236 | key: text("key |
| 237 | refillInterval: integer("refill_interval |
| 238 | refillAmount: integer("refill_amount |
| 239 | lastRefillAt: timestamp("last_refill_at |
| 240 | enabled: boolean("enabled |
| 241 | rateLimitEnabled: boolean("rate_limit_enabled |
| 242 | rateLimitTimeWindow: integer("rate_limit_time_window |
| 243 | rateLimitMax: integer("rate_limit_max |
| 244 | requestCount: integer("request_count |
| 245 | remaining: integer("remaining |
| 246 | lastRequest: timestamp("last_request |
| 247 | expiresAt: timestamp("expires_at |
| 248 | createdAt: timestamp("created_at |
| 249 | updatedAt: timestamp("updated_at |
| 250 | permissions: text("permissions |
| 251 | metadata: text("metadata |
| 252 | }, |
| 253 | (table) => |
| 254 | index("apikey_configId_idx"). |
| 255 | index("apikey_referenceId_idx"). |
| 256 | index("apikey_key_idx"). |
| 257 | ], |
| 258 | ); |
| 259 | |
| 260 | /** Extra addresses an account owns, so one person is reached and recognised as a commit author under each. Better Auth only knows `user.email`, so these resolve to it before its endpoints run; only verified rows count, or adding an address would claim someone else's commits or sign-in. */ |
| 261 | export const userEmail = |
| 262 | "user_email", |
| 263 | { |
| 264 | id: text("id |
| 265 | userId: text("user_id |
| 266 | .notNull() |
| 267 | .references(() => |
| 268 | // Stored lowercased; addresses are matched case-insensitively. |
| 269 | email: text("email |
| 270 | verified: boolean("verified |
| 271 | // Single-use verification token, cleared once the address is confirmed. |
| 272 | token: text("token |
| 273 | tokenExpiresAt: timestamp("token_expires_at |
| 274 | createdAt: timestamp("created_at |
| 275 | .defaultNow() |
| 276 | .notNull(), |
| 277 | }, |
| 278 | (table) => |
| 279 | ); |
| 280 | |
| 281 | export const deviceCode = |
| 282 | id: text("id |
| 283 | deviceCode: text("device_code |
| 284 | userCode: text("user_code |
| 285 | userId: text("user_id |
| 286 | expiresAt: timestamp("expires_at |
| 287 | status: text("status |
| 288 | lastPolledAt: timestamp("last_polled_at |
| 289 | pollingInterval: integer("polling_interval |
| 290 | clientId: text("client_id |
| 291 | scope: text("scope |
| 292 | }); |
| 293 | |
| 294 | export const userEmailRelations = |
| 295 | user: one(user |
| 296 | fields: [userEmail.userId |
| 297 | references: [user.id |
| 298 | }), |
| 299 | })); |
| 300 | |
| 301 | export const userRelations = |
| 302 | sessions: many(session |
| 303 | emails: many(userEmail |
| 304 | gpgKeys: many(userGpgKey |
| 305 | sshKeys: many(userSshKey |
| 306 | accounts: many(account |
| 307 | teamMembers: many(teamMember |
| 308 | members: many(member |
| 309 | invitations: many(invitation |
| 310 | })); |
| 311 | |
| 312 | export const sessionRelations = |
| 313 | user: one(user |
| 314 | fields: [session.userId |
| 315 | references: [user.id |
| 316 | }), |
| 317 | })); |
| 318 | |
| 319 | export const accountRelations = |
| 320 | user: one(user |
| 321 | fields: [account.userId |
| 322 | references: [user.id |
| 323 | }), |
| 324 | })); |
| 325 | |
| 326 | export const organizationRelations = |
| 327 | organizationRoles: many(organizationRole |
| 328 | teams: many(team |
| 329 | members: many(member |
| 330 | invitations: many(invitation |
| 331 | })); |
| 332 | |
| 333 | export const organizationRoleRelations = |
| 334 | organizationRole, |
| 335 | ({ one }) |
| 336 | organization: one(organization |
| 337 | fields: [organizationRole.organizationId |
| 338 | references: [organization.id |
| 339 | }), |
| 340 | }), |
| 341 | ); |
| 342 | |
| 343 | export const teamRelations = |
| 344 | organization: one(organization |
| 345 | fields: [team.organizationId |
| 346 | references: [organization.id |
| 347 | }), |
| 348 | teamMembers: many(teamMember |
| 349 | })); |
| 350 | |
| 351 | export const teamMemberRelations = |
| 352 | team: one(team |
| 353 | fields: [teamMember.teamId |
| 354 | references: [team.id |
| 355 | }), |
| 356 | user: one(user |
| 357 | fields: [teamMember.userId |
| 358 | references: [user.id |
| 359 | }), |
| 360 | })); |
| 361 | |
| 362 | export const memberRelations = |
| 363 | organization: one(organization |
| 364 | fields: [member.organizationId |
| 365 | references: [organization.id |
| 366 | }), |
| 367 | user: one(user |
| 368 | fields: [member.userId |
| 369 | references: [user.id |
| 370 | }), |
| 371 | })); |
| 372 | |
| 373 | export const invitationRelations = |
| 374 | organization: one(organization |
| 375 | fields: [invitation.organizationId |
| 376 | references: [organization.id |
| 377 | }), |
| 378 | user: one(user |
| 379 | fields: [invitation.inviterId |
| 380 | references: [user.id |
| 381 | }), |
| 382 | })); |
| 383 | |
| 384 | /** OpenPGP public keys an account uploads so its signed commits can be shown as verified. */ |
| 385 | export const userGpgKey = |
| 386 | "user_gpg_key", |
| 387 | { |
| 388 | id: text("id |
| 389 | userId: text("user_id |
| 390 | .notNull() |
| 391 | .references(() => |
| 392 | // Long key id of the primary key, lowercase hex. |
| 393 | keyId: text("key_id |
| 394 | fingerprint: text("fingerprint |
| 395 | // Armored public key, exactly as it was uploaded. |
| 396 | publicKey: text("public_key |
| 397 | createdAt: timestamp("created_at |
| 398 | .defaultNow() |
| 399 | .notNull(), |
| 400 | }, |
| 401 | (table) => |
| 402 | ); |
| 403 | |
| 404 | export const userGpgKeyRelations = |
| 405 | user: one(user |
| 406 | fields: [userGpgKey.userId |
| 407 | references: [user.id |
| 408 | }), |
| 409 | })); |
| 410 | |
| 411 | /** OpenSSH public keys an account uploads so it can push and fetch over SSH. */ |
| 412 | export const userSshKey = |
| 413 | "user_ssh_key", |
| 414 | { |
| 415 | id: text("id |
| 416 | userId: text("user_id |
| 417 | .notNull() |
| 418 | .references(() => |
| 419 | title: text("title |
| 420 | // SHA256 of the key blob, base64 without padding - what `ssh-keygen -lf` prints after `SHA256:`. |
| 421 | fingerprint: text("fingerprint |
| 422 | // The key blob, base64, as the second field of an authorized_keys line. |
| 423 | publicKey: text("public_key |
| 424 | // The OAuth app that added the key through the GitHub API, so organization OAuth app restrictions follow it onto SSH; null for keys added in Ghost's settings. |
| 425 | oauthClientId: text("oauth_client_id |
| 426 | // Keys added before oauthClientId was recorded, whose provenance is unknown; organizations with OAuth app restrictions refuse them over SSH. |
| 427 | legacy: boolean("legacy |
| 428 | lastUsedAt: timestamp("last_used_at |
| 429 | createdAt: timestamp("created_at |
| 430 | .defaultNow() |
| 431 | .notNull(), |
| 432 | }, |
| 433 | (table) => |
| 434 | ); |
| 435 | |
| 436 | export const userSshKeyRelations = |
| 437 | user: one(user |
| 438 | fields: [userSshKey.userId |
| 439 | references: [user.id |
| 440 | }), |
| 441 | })); |