| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { |
| 3 | index, |
| 4 | pgEnum, |
| 5 | pgTable, |
| 6 | text, |
| 7 | timestamp, |
| 8 | uniqueIndex, |
| 9 | } from "drizzle-orm/pg-core"; |
| 10 | |
| 11 | import { user } |
| 12 | import { repository } |
| 13 | |
| 14 | /** Lowest to highest. The owner is not a role here: ownership lives on `repository.ownerId`. */ |
| 15 | export const repositoryRole = |
| 16 | "read", |
| 17 | "triage", |
| 18 | "write", |
| 19 | "maintain", |
| 20 | "admin", |
| 21 | ]); |
| 22 | |
| 23 | /** An invitation until `acceptedAt` is set, and only then a grant: a pending row gives its user nothing. */ |
| 24 | export const repositoryCollaborator = |
| 25 | "repository_collaborator", |
| 26 | { |
| 27 | id: text() |
| 28 | .primaryKey() |
| 29 | .$defaultFn(() => |
| 30 | repositoryId: text() |
| 31 | .references(() => |
| 32 | .notNull(), |
| 33 | userId: text() |
| 34 | .references(() => |
| 35 | .notNull(), |
| 36 | role: repositoryRole().notNull |
| 37 | invitedById: text().references |
| 38 | createdAt: timestamp({ |
| 39 | acceptedAt: timestamp({ |
| 40 | }, |
| 41 | (t) => |
| 42 | uniqueIndex("repository_collaborator_repository_user_idx"). |
| 43 | t.repositoryId, |
| 44 | t.userId, |
| 45 | ), |
| 46 | index("repository_collaborator_user_idx"). |
| 47 | ], |
| 48 | ); |