import { index, pgTable, primaryKey, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core"; import { workspaceRoleEnum } from "./enums"; import { users } from "./auth"; export const workspaces = pgTable("workspace", { id: text("id").primaryKey(), name: text("name").notNull(), slug: text("slug").notNull().unique(), description: text("description"), imageUrl: text("image_url"), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(), }); export const workspaceMembers = pgTable( "workspace_member", { workspaceId: text("workspace_id") .notNull() .references(() => workspaces.id, { onDelete: "cascade" }), userId: text("user_id") .notNull() .references(() => users.id, { onDelete: "cascade" }), role: workspaceRoleEnum("role").notNull().default("MEMBER"), joinedAt: timestamp("joined_at", { withTimezone: true }).notNull().defaultNow(), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(), }, (table) => [ primaryKey({ columns: [table.workspaceId, table.userId] }), index("workspace_member_user_idx").on(table.userId), ], ); export const workspaceInvitations = pgTable( "workspace_invitation", { id: text("id").primaryKey(), workspaceId: text("workspace_id") .notNull() .references(() => workspaces.id, { onDelete: "cascade" }), email: text("email").notNull(), role: workspaceRoleEnum("role").notNull().default("MEMBER"), tokenHash: text("token_hash").notNull().unique(), expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(), acceptedAt: timestamp("accepted_at", { withTimezone: true }), createdByUserId: text("created_by_user_id") .notNull() .references(() => users.id, { onDelete: "restrict" }), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), }, (table) => [ uniqueIndex("workspace_invitation_email_active_idx").on(table.workspaceId, table.email), index("workspace_invitation_workspace_idx").on(table.workspaceId), ], );