Data Model (Prisma Schema)
Relevant source files
The following files were used as context for generating this wiki page:
The data model for Inbox Zero is defined using Prisma Schema Language and serves as the single source of truth for the application's database structure. It is managed within the prisma/ directory and defines all entities, their fields, and the relationships between them. This model is fundamental to the application, underpinning features like user authentication, AI rule processing, knowledge base management, and email organization.
The Prisma client, instantiated as a singleton, provides a type-safe interface for all database operations throughout the backend, including in API routes and Server Actions. This ensures that data access is consistent and predictable.
Sources: ARCHITECTURE.md:33-37, apps/web/utils/prisma.ts
Entity Relationship Diagram
This diagram illustrates the primary entities within the Inbox Zero data model and their relationships.
This ER diagram shows the core relationships between users, their email accounts, and the associated rules, knowledge base, and organizational entities.
Sources: apps/web/prisma/schema.prisma
Core Models
These models form the foundation of the application, managing users, authentication, and linked email accounts.
User and Authentication
The User, Account, Session, and VerificationToken models are standard components for handling authentication, likely with a library like NextAuth.js. The User model is the central entity for a person using the application.
- User: Represents an application user. It stores basic profile information and their premium subscription status.
- EmailAccount: Represents a specific email account (e.g., a Gmail or Outlook account) linked by a
User. A user can have multiple email accounts. This model is crucial as most other data, likeRules andKnowledge, is scoped to anEmailAccount. - ApiKey: Stores API keys associated with an
EmailAccount, enabling programmatic access.
Sources: apps/web/prisma/schema.prisma:11-73
Prisma Client Instantiation
The application ensures that only one instance of the Prisma client is active by using a singleton pattern. This is a standard practice to avoid exhausting database connections in a serverless environment.
// apps/web/utils/prisma.ts:3-15
import { PrismaClient } from "@prisma/client";
const prismaClientSingleton = () => {
return new PrismaClient();
};
declare global {
// eslint-disable-next-line no-var
var prisma: undefined | ReturnType<typeof prismaClientSingleton>;
}
const prisma = globalThis.prisma ?? prismaClientSingleton();
export default prisma;
if (process.env.NODE_ENV !== "production") globalThis.prisma = prisma;
Sources: apps/web/utils/prisma.ts:3-15
AI Assistant and Rules Engine
This set of models powers the AI Personal Assistant feature, defining the logic for how incoming emails are processed.
Sources: ARCHITECTURE.md:100-112
Rule Model
The Rule is the central entity of the AI engine. Each rule defines a set of conditions and corresponding actions to be taken on an email.
| Field | Type | Description - |
| id | String | Unique identifier. - |
| name | String | User-defined name for the rule. Must be unique per emailAccountId. - |
| emailAccountId | String | Foreign key linking to the EmailAccount. - |
| conditionalOperator | LogicalOperator | AND or OR. Determines how conditions are combined. - |
| instructions | String? | Natural language instructions for the AI to follow if conditions are met. - |
| from / to / subject | String? | Static conditions that match on email headers. - |
| categoryFilters | Category[] | A list of Category entities used to filter emails. - |
| actions | Action[] | A list of Action entities to be executed if the rule matches. - |
| automate | Boolean | If true, the rule runs automatically without user confirmation. - |
Sources: apps/web/prisma/schema.prisma:102-140
Action Model
An Action represents a specific operation to be performed when a Rule is triggered. A single rule can have multiple actions.
| Field | Type | Description - |
| id | String | Unique identifier. - |
type: An enum (ActionType) specifying the action, e.g.,ARCHIVE,REPLY,ADD_LABEL,SEND_EMAIL.fields: Various optional fields likelabel,to,subject,contentto support the action type. For example, aSEND_EMAILaction would use theto,subject, andcontentfields.
Sources: apps/web/prisma/schema.prisma:142-161, apps/web/utils/rule/rule.ts:241-258
Rule Creation and Update Flow
The creation and updating of rules are handled by server-side functions that interact directly with the Prisma client. The safeCreateRule and safeUpdateRule functions provide robust logic for managing rules, including handling duplicate names and tracking changes.
This diagram shows the logic flow when creating a new rule.
Sources: apps/web/utils/rule/rule.ts:40-79
Knowledge Base
The Knowledge model provides a way for users to store information that the AI can use to draft email replies. This allows for personalized and context-aware response generation.
title: A user-defined title for the knowledge entry.content: The body of the knowledge, which can be a significant amount of text.emailAccountId: Scopes the knowledge to a specific email account.
Premium tiers can place limits on the number of knowledge items and the character count per item.
Sources: apps/web/prisma/schema.prisma:214-222, apps/web/utils/actions/knowledge.ts:22-35
Knowledge Creation Sequence
The following diagram shows the sequence of creating a new knowledge base entry via a server action.
This sequence demonstrates the interaction between the frontend, server action, premium feature check, and the database when a user adds to their knowledge base.
Sources: apps/web/utils/actions/knowledge.ts:15-43, apps/web/app/(app)/[emailAccountId]/assistant/knowledge/KnowledgeBase.tsx
Email and Sender Organization
These models are used to categorize and manage incoming emails and their senders, forming the basis for features like Smart Categories and the Bulk Unsubscriber.
- Category: A user-defined category (e.g., "Work", "Finance") that can be assigned to
Senders and used inRulefilters. - Sender: Represents an email address that has sent an email to the user. It can be associated with a
Category. - Whitelist: A list of email addresses that should bypass certain rules, such as the cold email blocker.
- Newsletter: Tracks senders identified as newsletters and their unsubscription status (
PENDING,UNSUBSCRIBED,APPROVED,AUTO_ARCHIVED).
Sources: apps/web/prisma/schema.prisma:224-261, apps/web/app/(app)/[emailAccountId]/bulk-unsubscribe/BulkUnsubscribeSummary.tsx
Premium Features
The data model supports premium features through the PremiumTier enum and the PremiumOrder model.
PremiumTier: An enum defining the available subscription levels (FREE,BUSINESS_PLUS_MONTHLY, etc.). This is stored on theUsermodel.PremiumOrder: Records transactions from a payment provider (e.g., Lemon Squeezy), linking them to aUser.
This structure allows server-side logic, like in createKnowledgeAction, to check a user's premiumTier before allowing access to certain features or higher usage limits.
Sources: apps/web/prisma/schema.prisma:11-23, apps/web/prisma/schema.prisma:263-277, apps/web/utils/actions/knowledge.ts:22-35
Summary
The Inbox Zero data model, defined in schema.prisma, is a well-structured and comprehensive schema that effectively supports the application's core functionalities. It clearly separates user data, email account configurations, and the complex logic for the AI-powered rules engine. The use of Prisma provides type-safety and a clear, declarative way to manage database state, which is leveraged across the application in server actions and API routes to build features like the AI assistant, knowledge base, and email organization tools.