import { sql } from "drizzle-orm"; import { index, integer, primaryKey, sqliteTable, text, uniqueIndex, } from "drizzle-orm/sqlite-core"; const timestamps = { createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }; const newId = () => crypto.randomUUID(); export const users = sqliteTable("users", { id: text("id").primaryKey().$defaultFn(newId), email: text("email").notNull(), username: text("username").notNull(), passwordHash: text("password_hash").notNull(), emailVerifiedAt: integer("email_verified_at", { mode: "timestamp" }), name: text("name").notNull(), headline: text("headline").notNull().default(""), about: text("about").notNull().default(""), location: text("location").notNull().default(""), avatarUrl: text("avatar_url"), coverUrl: text("cover_url"), openToWork: integer("open_to_work", { mode: "boolean" }).notNull().default(false), ...timestamps, }, (table) => [ uniqueIndex("users_email_unique").on(table.email), uniqueIndex("users_username_unique").on(table.username), index("users_name_idx").on(table.name), ]); export const sessions = sqliteTable("sessions", { id: text("id").primaryKey().$defaultFn(newId), userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }), tokenHash: text("token_hash").notNull(), expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), lastSeenAt: integer("last_seen_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [ uniqueIndex("sessions_token_hash_unique").on(table.tokenHash), index("sessions_user_idx").on(table.userId), index("sessions_expiry_idx").on(table.expiresAt), ]); export const emailVerificationTokens = sqliteTable("email_verification_tokens", { id: text("id").primaryKey().$defaultFn(newId), userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }), tokenHash: text("token_hash").notNull(), sentToEmail: text("sent_to_email").notNull(), expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), consumedAt: integer("consumed_at", { mode: "timestamp" }), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [ uniqueIndex("email_verification_tokens_hash_unique").on(table.tokenHash), index("email_verification_tokens_user_idx").on(table.userId), index("email_verification_tokens_expiry_idx").on(table.expiresAt), ]); export const subscriptions = sqliteTable("subscriptions", { id: text("id").primaryKey().$defaultFn(newId), userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }), stripeCustomerId: text("stripe_customer_id"), stripeSubscriptionId: text("stripe_subscription_id"), stripePriceId: text("stripe_price_id").notNull(), status: text("status").notNull().default("incomplete"), currentPeriodStart: integer("current_period_start", { mode: "timestamp" }), currentPeriodEnd: integer("current_period_end", { mode: "timestamp" }), cancelAtPeriodEnd: integer("cancel_at_period_end", { mode: "boolean" }).notNull().default(false), checkoutSessionId: text("checkout_session_id"), ...timestamps, }, (table) => [ uniqueIndex("subscriptions_user_unique").on(table.userId), uniqueIndex("subscriptions_stripe_customer_unique").on(table.stripeCustomerId), uniqueIndex("subscriptions_stripe_subscription_unique").on(table.stripeSubscriptionId), index("subscriptions_status_idx").on(table.status), ]); export const organizations = sqliteTable("organizations", { id: text("id").primaryKey().$defaultFn(newId), name: text("name").notNull(), slug: text("slug").notNull(), tagline: text("tagline").notNull().default(""), description: text("description").notNull().default(""), industry: text("industry").notNull().default(""), website: text("website"), location: text("location").notNull().default(""), employeeCount: integer("employee_count").notNull().default(0), logoUrl: text("logo_url"), settings: text("settings", { mode: "json" }).$type<{ allowMemberPosts: boolean; defaultJobVisibility: "public" | "tenant" }>() .notNull().default({ allowMemberPosts: true, defaultJobVisibility: "public" }), ...timestamps, }, (table) => [uniqueIndex("organizations_slug_unique").on(table.slug)]); export const memberships = sqliteTable("memberships", { organizationId: text("organization_id").notNull().references(() => organizations.id, { onDelete: "cascade" }), userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }), role: text("role", { enum: ["owner", "admin", "recruiter", "member"] }).notNull().default("member"), title: text("title").notNull().default(""), joinedAt: integer("joined_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [ primaryKey({ columns: [table.organizationId, table.userId] }), index("memberships_user_idx").on(table.userId), ]); export const experiences = sqliteTable("experiences", { id: text("id").primaryKey().$defaultFn(newId), userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }), organizationId: text("organization_id").references(() => organizations.id, { onDelete: "set null" }), companyName: text("company_name").notNull(), title: text("title").notNull(), location: text("location").notNull().default(""), description: text("description").notNull().default(""), startDate: text("start_date").notNull(), endDate: text("end_date"), current: integer("current", { mode: "boolean" }).notNull().default(false), }, (table) => [index("experiences_user_idx").on(table.userId)]); export const posts = sqliteTable("posts", { id: text("id").primaryKey().$defaultFn(newId), authorId: text("author_id").notNull().references(() => users.id, { onDelete: "cascade" }), organizationId: text("organization_id").references(() => organizations.id, { onDelete: "set null" }), content: text("content").notNull(), mediaUrl: text("media_url"), visibility: text("visibility").notNull().default("network"), ...timestamps, }, (table) => [ index("posts_created_idx").on(table.createdAt), index("posts_author_idx").on(table.authorId), index("posts_org_idx").on(table.organizationId), ]); export const postLikes = sqliteTable("post_likes", { postId: text("post_id").notNull().references(() => posts.id, { onDelete: "cascade" }), userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [primaryKey({ columns: [table.postId, table.userId] })]); export const comments = sqliteTable("comments", { id: text("id").primaryKey().$defaultFn(newId), postId: text("post_id").notNull().references(() => posts.id, { onDelete: "cascade" }), authorId: text("author_id").notNull().references(() => users.id, { onDelete: "cascade" }), content: text("content").notNull(), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [ index("comments_post_idx").on(table.postId, table.createdAt), index("comments_author_idx").on(table.authorId), ]); export const connections = sqliteTable("connections", { id: text("id").primaryKey().$defaultFn(newId), requesterId: text("requester_id").notNull().references(() => users.id, { onDelete: "cascade" }), recipientId: text("recipient_id").notNull().references(() => users.id, { onDelete: "cascade" }), status: text("status", { enum: ["pending", "accepted", "declined"] }).notNull().default("pending"), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), respondedAt: integer("responded_at", { mode: "timestamp" }), }, (table) => [ uniqueIndex("connections_pair_unique").on(table.requesterId, table.recipientId), index("connections_recipient_status_idx").on(table.recipientId, table.status), ]); export const follows = sqliteTable("follows", { userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }), targetUserId: text("target_user_id").notNull().references(() => users.id, { onDelete: "cascade" }), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [ primaryKey({ columns: [table.userId, table.targetUserId] }), index("follows_target_idx").on(table.targetUserId, table.createdAt), ]); export const conversations = sqliteTable("conversations", { id: text("id").primaryKey().$defaultFn(newId), tenantId: text("tenant_id").references(() => organizations.id, { onDelete: "set null" }), subject: text("subject").notNull().default(""), updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }); export const conversationMembers = sqliteTable("conversation_members", { conversationId: text("conversation_id").notNull().references(() => conversations.id, { onDelete: "cascade" }), userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }), lastReadAt: integer("last_read_at", { mode: "timestamp" }), }, (table) => [ primaryKey({ columns: [table.conversationId, table.userId] }), index("conversation_members_user_idx").on(table.userId), ]); export const messages = sqliteTable("messages", { id: text("id").primaryKey().$defaultFn(newId), conversationId: text("conversation_id").notNull().references(() => conversations.id, { onDelete: "cascade" }), senderId: text("sender_id").notNull().references(() => users.id, { onDelete: "cascade" }), body: text("body").notNull(), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [index("messages_conversation_idx").on(table.conversationId, table.createdAt)]); export const notifications = sqliteTable("notifications", { id: text("id").primaryKey().$defaultFn(newId), userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }), actorId: text("actor_id").references(() => users.id, { onDelete: "set null" }), type: text("type", { enum: ["connection", "post_like", "comment", "message", "job"] }).notNull(), title: text("title").notNull(), body: text("body").notNull().default(""), href: text("href").notNull().default("/notifications"), readAt: integer("read_at", { mode: "timestamp" }), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [index("notifications_user_read_idx").on(table.userId, table.readAt, table.createdAt)]); export const jobs = sqliteTable("jobs", { id: text("id").primaryKey().$defaultFn(newId), organizationId: text("organization_id").notNull().references(() => organizations.id, { onDelete: "cascade" }), createdBy: text("created_by").notNull().references(() => users.id), title: text("title").notNull(), description: text("description").notNull(), requirements: text("requirements").notNull().default(""), location: text("location").notNull(), workplaceType: text("workplace_type").notNull().default("Hybrid"), employmentType: text("employment_type").notNull().default("Full-time"), seniority: text("seniority").notNull().default("Mid-Senior level"), salaryMin: integer("salary_min"), salaryMax: integer("salary_max"), currency: text("currency").notNull().default("USD"), status: text("status").notNull().default("published"), tenantVisibility: text("tenant_visibility").notNull().default("public"), publishedAt: integer("published_at", { mode: "timestamp" }).default(sql`(unixepoch())`), ...timestamps, }, (table) => [ index("jobs_org_status_idx").on(table.organizationId, table.status), index("jobs_location_idx").on(table.location), index("jobs_published_idx").on(table.publishedAt), ]); export const jobApplications = sqliteTable("job_applications", { id: text("id").primaryKey().$defaultFn(newId), jobId: text("job_id").notNull().references(() => jobs.id, { onDelete: "cascade" }), applicantId: text("applicant_id").notNull().references(() => users.id, { onDelete: "cascade" }), status: text("status", { enum: ["submitted", "reviewing", "interview", "rejected", "hired"] }).notNull().default("submitted"), coverNote: text("cover_note").notNull().default(""), resumeUrl: text("resume_url"), answers: text("answers", { mode: "json" }).$type>().notNull().default({}), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [ uniqueIndex("job_applications_job_applicant_unique").on(table.jobId, table.applicantId), index("job_applications_applicant_idx").on(table.applicantId), ]); export const savedJobs = sqliteTable("saved_jobs", { jobId: text("job_id").notNull().references(() => jobs.id, { onDelete: "cascade" }), userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [ primaryKey({ columns: [table.jobId, table.userId] }), index("saved_jobs_user_idx").on(table.userId, table.createdAt), ]); export const auditLogs = sqliteTable("audit_logs", { id: text("id").primaryKey().$defaultFn(newId), tenantId: text("tenant_id").references(() => organizations.id, { onDelete: "set null" }), actorId: text("actor_id").references(() => users.id, { onDelete: "set null" }), action: text("action").notNull(), entityType: text("entity_type").notNull(), entityId: text("entity_id").notNull(), metadata: text("metadata", { mode: "json" }).$type>().notNull().default({}), createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(sql`(unixepoch())`), }, (table) => [index("audit_logs_tenant_created_idx").on(table.tenantId, table.createdAt)]); export type User = typeof users.$inferSelect; export type Job = typeof jobs.$inferSelect; export type Organization = typeof organizations.$inferSelect;