| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { |
| 3 | index, |
| 4 | pgEnum, |
| 5 | pgTable, |
| 6 | primaryKey, |
| 7 | text, |
| 8 | timestamp, |
| 9 | uniqueIndex, |
| 10 | } from "drizzle-orm/pg-core"; |
| 11 | |
| 12 | import { team, user |
| 13 | import { repository } |
| 14 | |
| 15 | /** Lowest to highest. The owner is not a role here: ownership lives on `repository.ownerId`. */ |
| 16 | export const repositoryRole = |
| 17 | "read", |
| 18 | "triage", |
| 19 | "write", |
| 20 | "maintain", |
| 21 | "admin", |
| 22 | ]); |
| 23 | |
| 24 | /** An invitation until `acceptedAt` is set, and only then a grant: a pending row gives its user nothing. */ |
| 25 | export const repositoryCollaborator = |
| 26 | "repository_collaborator", |
| 27 | { |
| 28 | id: text() |
| 29 | .primaryKey() |
| 30 | .$defaultFn(() => |
| 31 | repositoryId: text() |
| 32 | .references(() => |
| 33 | .notNull(), |
| 34 | userId: text() |
| 35 | .references(() => |
| 36 | .notNull(), |
| 37 | role: repositoryRole().notNull |
| 38 | invitedById: text().references |
| 39 | createdAt: timestamp({ |
| 40 | acceptedAt: timestamp({ |
| 41 | }, |
| 42 | (t) => |
| 43 | uniqueIndex("repository_collaborator_repository_user_idx"). |
| 44 | t.repositoryId, |
| 45 | t.userId, |
| 46 | ), |
| 47 | index("repository_collaborator_user_idx"). |
| 48 | ], |
| 49 | ); |
| 50 | |
| 51 | /** A team's role on one of its organization's repositories. Every member of the team holds it. */ |
| 52 | export const repositoryTeam = |
| 53 | "repository_team", |
| 54 | { |
| 55 | repositoryId: text() |
| 56 | .references(() => |
| 57 | .notNull(), |
| 58 | teamId: text() |
| 59 | .references(() => |
| 60 | .notNull(), |
| 61 | role: repositoryRole().notNull |
| 62 | createdAt: timestamp({ |
| 63 | }, |
| 64 | (t) => |
| 65 | primaryKey({ columns: |
| 66 | index("repository_team_team_idx"). |
| 67 | ], |
| 68 | ); |