import { createId } from "@paralleldrive/cuid2"; import { relations } from "drizzle-orm"; import { boolean, index, integer, pgTable, text, timestamp, } from "drizzle-orm/pg-core"; export const userTable = pgTable("user", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), name: text("name").notNull(), email: text("email").notNull().unique(), emailVerified: boolean("email_verified") .$defaultFn(() => false) .notNull(), image: text("image"), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { mode: "date" }) .defaultNow() .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), isAnonymous: boolean("is_anonymous").default(false), }); export const sessionTable = pgTable( "session", { id: text("id").primaryKey(), expiresAt: timestamp("expires_at", { mode: "date" }).notNull(), token: text("token").notNull().unique(), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { mode: "date" }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), ipAddress: text("ip_address"), userAgent: text("user_agent"), userId: text("user_id") .notNull() .references(() => userTable.id, { onDelete: "cascade" }), activeOrganizationId: text("active_organization_id"), activeTeamId: text("active_team_id"), }, (table) => [index("session_userId_idx").on(table.userId)], ); export const accountTable = pgTable( "account", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), accountId: text("account_id").notNull(), providerId: text("provider_id").notNull(), userId: text("user_id") .notNull() .references(() => userTable.id, { onDelete: "cascade" }), accessToken: text("access_token"), refreshToken: text("refresh_token"), idToken: text("id_token"), accessTokenExpiresAt: timestamp("access_token_expires_at", { mode: "date", }), refreshTokenExpiresAt: timestamp("refresh_token_expires_at", { mode: "date", }), scope: text("scope"), password: text("password"), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { mode: "date" }) .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }, (table) => [index("account_userId_idx").on(table.userId)], ); export const verificationTable = pgTable( "verification", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), identifier: text("identifier").notNull(), value: text("value").notNull(), expiresAt: timestamp("expires_at", { mode: "date" }).notNull(), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { mode: "date" }) .defaultNow() .$onUpdate(() => /* @__PURE__ */ new Date()) .notNull(), }, (table) => [index("verification_identifier_idx").on(table.identifier)], ); export const workspaceTable = pgTable("workspace", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), name: text("name").notNull(), slug: text("slug").notNull().unique(), logo: text("logo"), metadata: text("metadata"), description: text("description"), createdAt: timestamp("created_at", { mode: "date" }).notNull(), }); export const workspaceUserTable = pgTable( "workspace_member", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), workspaceId: text("workspace_id") .notNull() .references(() => workspaceTable.id, { onDelete: "cascade", }), userId: text("user_id") .notNull() .references(() => userTable.id, { onDelete: "cascade", }), role: text("role").default("member").notNull(), joinedAt: timestamp("joined_at", { mode: "date" }).notNull(), }, (table) => [ index("workspace_member_workspaceId_idx").on(table.workspaceId), index("workspace_member_userId_idx").on(table.userId), ], ); export const teamTable = pgTable( "team", { id: text("id").primaryKey(), name: text("name").notNull(), workspaceId: text("workspace_id") .notNull() .references(() => workspaceTable.id, { onDelete: "cascade" }), createdAt: timestamp("created_at").notNull(), updatedAt: timestamp("updated_at").$onUpdate( () => /* @__PURE__ */ new Date(), ), }, (table) => [index("team_workspaceId_idx").on(table.workspaceId)], ); export const teamMemberTable = pgTable( "team_member", { id: text("id").primaryKey(), teamId: text("team_id") .notNull() .references(() => teamTable.id, { onDelete: "cascade" }), userId: text("user_id") .notNull() .references(() => userTable.id, { onDelete: "cascade" }), createdAt: timestamp("created_at"), }, (table) => [ index("teamMember_teamId_idx").on(table.teamId), index("teamMember_userId_idx").on(table.userId), ], ); export const invitationTable = pgTable( "invitation", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), workspaceId: text("workspace_id") .notNull() .references(() => workspaceTable.id, { onDelete: "cascade" }), email: text("email").notNull(), role: text("role"), teamId: text("team_id"), status: text("status").default("pending").notNull(), expiresAt: timestamp("expires_at").notNull(), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), inviterId: text("inviter_id") .notNull() .references(() => userTable.id, { onDelete: "cascade" }), }, (table) => [ index("invitation_workspaceId_idx").on(table.workspaceId), index("invitation_email_idx").on(table.email), ], ); export const projectTable = pgTable("project", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), workspaceId: text("workspace_id") .notNull() .references(() => workspaceTable.id, { onDelete: "cascade", onUpdate: "cascade", }), slug: text("slug").notNull(), icon: text("icon").default("Layout"), name: text("name").notNull(), description: text("description"), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), isPublic: boolean("is_public").default(false), }); export const columnTable = pgTable( "column", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), projectId: text("project_id") .notNull() .references(() => projectTable.id, { onDelete: "cascade", onUpdate: "cascade", }), name: text("name").notNull(), slug: text("slug").notNull(), position: integer("position").notNull().default(0), icon: text("icon"), color: text("color"), isFinal: boolean("is_final").default(false).notNull(), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { mode: "date" }) .defaultNow() .$onUpdate(() => new Date()) .notNull(), }, (table) => [index("column_projectId_idx").on(table.projectId)], ); export const workflowRuleTable = pgTable( "workflow_rule", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), projectId: text("project_id") .notNull() .references(() => projectTable.id, { onDelete: "cascade", onUpdate: "cascade", }), integrationType: text("integration_type").notNull(), eventType: text("event_type").notNull(), columnId: text("column_id") .notNull() .references(() => columnTable.id, { onDelete: "cascade", onUpdate: "cascade", }), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { mode: "date" }) .defaultNow() .$onUpdate(() => new Date()) .notNull(), }, (table) => [index("workflow_rule_projectId_idx").on(table.projectId)], ); export const taskTable = pgTable("task", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), projectId: text("project_id") .notNull() .references(() => projectTable.id, { onDelete: "cascade", onUpdate: "cascade", }), position: integer("position").default(0), number: integer("number").default(1), userId: text("assignee_id").references(() => userTable.id, { onDelete: "cascade", onUpdate: "cascade", }), title: text("title").notNull(), description: text("description"), status: text("status").notNull().default("to-do"), columnId: text("column_id").references(() => columnTable.id, { onDelete: "set null", onUpdate: "cascade", }), priority: text("priority").default("low"), dueDate: timestamp("due_date", { mode: "date" }), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), }); export const timeEntryTable = pgTable("time_entry", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), taskId: text("task_id") .notNull() .references(() => taskTable.id, { onDelete: "cascade", onUpdate: "cascade", }), userId: text("user_id").references(() => userTable.id, { onDelete: "cascade", onUpdate: "cascade", }), description: text("description"), startTime: timestamp("start_time", { mode: "date" }).notNull(), endTime: timestamp("end_time", { mode: "date" }), duration: integer("duration").default(0), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), }); export const activityTable = pgTable("activity", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), taskId: text("task_id") .notNull() .references(() => taskTable.id, { onDelete: "cascade", onUpdate: "cascade", }), type: text("type").notNull(), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), userId: text("user_id").references(() => userTable.id, { onDelete: "cascade", onUpdate: "cascade", }), content: text("content"), externalUserName: text("external_user_name"), externalUserAvatar: text("external_user_avatar"), externalSource: text("external_source"), externalUrl: text("external_url"), }); export const assetTable = pgTable( "asset", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), workspaceId: text("workspace_id") .notNull() .references(() => workspaceTable.id, { onDelete: "cascade", onUpdate: "cascade", }), projectId: text("project_id") .notNull() .references(() => projectTable.id, { onDelete: "cascade", onUpdate: "cascade", }), taskId: text("task_id").references(() => taskTable.id, { onDelete: "cascade", onUpdate: "cascade", }), activityId: text("activity_id").references(() => activityTable.id, { onDelete: "cascade", onUpdate: "cascade", }), objectKey: text("object_key").notNull().unique(), filename: text("filename").notNull(), mimeType: text("mime_type").notNull(), size: integer("size").notNull(), kind: text("kind").notNull().default("image"), surface: text("surface").notNull().default("description"), createdBy: text("created_by").references(() => userTable.id, { onDelete: "set null", onUpdate: "cascade", }), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), }, (table) => [ index("asset_workspaceId_idx").on(table.workspaceId), index("asset_projectId_idx").on(table.projectId), index("asset_taskId_idx").on(table.taskId), index("asset_activityId_idx").on(table.activityId), ], ); export const labelTable = pgTable("label", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), name: text("name").notNull(), color: text("color").notNull(), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), taskId: text("task_id").references(() => taskTable.id, { onDelete: "cascade", onUpdate: "cascade", }), workspaceId: text("workspace_id").references(() => workspaceTable.id, { onDelete: "cascade", onUpdate: "cascade", }), }); export const notificationTable = pgTable("notification", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), userId: text("user_id") .notNull() .references(() => userTable.id, { onDelete: "cascade", onUpdate: "cascade", }), title: text("title").notNull(), content: text("content"), type: text("type").notNull().default("info"), isRead: boolean("is_read").default(false), resourceId: text("resource_id"), resourceType: text("resource_type"), createdAt: timestamp("created_at", { mode: "date", withTimezone: true }) .defaultNow() .notNull(), }); export const githubIntegrationTable = pgTable("github_integration", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), projectId: text("project_id") .notNull() .references(() => projectTable.id, { onDelete: "cascade", onUpdate: "cascade", }) .unique(), repositoryOwner: text("repository_owner").notNull(), repositoryName: text("repository_name").notNull(), installationId: integer("installation_id"), isActive: boolean("is_active").default(true), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { mode: "date" }).defaultNow().notNull(), }); export const integrationTable = pgTable( "integration", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), projectId: text("project_id") .notNull() .references(() => projectTable.id, { onDelete: "cascade", onUpdate: "cascade", }), type: text("type").notNull(), config: text("config").notNull(), isActive: boolean("is_active").default(true), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { mode: "date" }) .defaultNow() .$onUpdate(() => new Date()) .notNull(), }, (table) => [ index("integration_projectId_idx").on(table.projectId), index("integration_type_idx").on(table.type), ], ); export const externalLinkTable = pgTable( "external_link", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), taskId: text("task_id") .notNull() .references(() => taskTable.id, { onDelete: "cascade", onUpdate: "cascade", }), integrationId: text("integration_id") .notNull() .references(() => integrationTable.id, { onDelete: "cascade", onUpdate: "cascade", }), resourceType: text("resource_type").notNull(), externalId: text("external_id").notNull(), url: text("url").notNull(), title: text("title"), metadata: text("metadata"), createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { mode: "date" }) .defaultNow() .$onUpdate(() => new Date()) .notNull(), }, (table) => [ index("external_link_taskId_idx").on(table.taskId), index("external_link_integrationId_idx").on(table.integrationId), index("external_link_externalId_idx").on(table.externalId), index("external_link_resourceType_idx").on(table.resourceType), ], ); export const apikeyTable = pgTable( "apikey", { id: text("id") .$defaultFn(() => createId()) .primaryKey(), configId: text("config_id").default("default").notNull(), name: text("name"), start: text("start"), referenceId: text("reference_id") .notNull() .references(() => userTable.id, { onDelete: "cascade" }), prefix: text("prefix"), key: text("key").notNull(), userId: text("user_id").references(() => userTable.id, { onDelete: "cascade", }), refillInterval: integer("refill_interval"), refillAmount: integer("refill_amount"), lastRefillAt: timestamp("last_refill_at", { mode: "date" }), enabled: boolean("enabled").default(true), rateLimitEnabled: boolean("rate_limit_enabled").default(true), rateLimitTimeWindow: integer("rate_limit_time_window").default(86400000), rateLimitMax: integer("rate_limit_max").default(10), requestCount: integer("request_count").default(0), remaining: integer("remaining"), lastRequest: timestamp("last_request", { mode: "date" }), expiresAt: timestamp("expires_at", { mode: "date" }), createdAt: timestamp("created_at", { mode: "date" }).notNull(), updatedAt: timestamp("updated_at", { mode: "date" }).notNull(), permissions: text("permissions"), metadata: text("metadata"), }, (table) => [ index("apikey_configId_idx").on(table.configId), index("apikey_key_idx").on(table.key), index("apikey_referenceId_idx").on(table.referenceId), index("apikey_userId_idx").on(table.userId), ], ); // Auth-schema compatible aliases in schema.ts export const user = userTable; export const session = sessionTable; export const account = accountTable; export const verification = verificationTable; export const workspace = workspaceTable; export const team = teamTable; export const teamMember = teamMemberTable; export const workspace_member = workspaceUserTable; export const invitation = invitationTable; export const apikey = apikeyTable; // Auth-schema compatible relation exports in schema.ts export const userRelations = relations(user, ({ many }) => ({ sessions: many(session), accounts: many(account), teamMembers: many(teamMember), workspace_members: many(workspace_member), invitations: many(invitation), })); export const sessionRelations = relations(session, ({ one }) => ({ user: one(user, { fields: [session.userId], references: [user.id], }), })); export const accountRelations = relations(account, ({ one }) => ({ user: one(user, { fields: [account.userId], references: [user.id], }), })); export const workspaceRelations = relations(workspace, ({ many }) => ({ teams: many(team), workspace_members: many(workspace_member), invitations: many(invitation), })); export const teamRelations = relations(team, ({ one, many }) => ({ workspace: one(workspace, { fields: [team.workspaceId], references: [workspace.id], }), teamMembers: many(teamMember), })); export const teamMemberRelations = relations(teamMember, ({ one }) => ({ team: one(team, { fields: [teamMember.teamId], references: [team.id], }), user: one(user, { fields: [teamMember.userId], references: [user.id], }), })); export const workspace_memberRelations = relations( workspace_member, ({ one }) => ({ workspace: one(workspace, { fields: [workspace_member.workspaceId], references: [workspace.id], }), user: one(user, { fields: [workspace_member.userId], references: [user.id], }), }), ); export const invitationRelations = relations(invitation, ({ one }) => ({ workspace: one(workspace, { fields: [invitation.workspaceId], references: [workspace.id], }), user: one(user, { fields: [invitation.inviterId], references: [user.id], }), }));