Database Schema
Relevant source files
The following files were used as context for generating this wiki page:
The Analog application uses a PostgreSQL database managed by the Drizzle ORM. This schema is designed to support multi-provider calendar aggregation, user authentication, and API access management. The database connection is established using the postgres library, and Drizzle is configured to automatically map camelCase field names in the code to snake_case column names in the database.
The schema is defined across multiple files within the packages/db/src/schema/ directory, which are then aggregated by index.ts for use by the ORM.
Sources: packages/db/src/index.ts:1-26, README.md:83-84, packages/db/src/schema/index.ts
Database Management
Database schema management and migrations are handled by drizzle-kit. The configuration specifies the schema entry point and the output directory for migration files.
Sources: packages/db/drizzle.config.ts:1-10
Drizzle Configuration
The drizzle.config.ts file configures drizzle-kit to locate the schema definitions and manage migrations.
// packages/db/drizzle.config.ts
import { type Config } from "drizzle-kit";
import { env } from "@repo/env/server";
export default {
dialect: "postgresql",
schema: "./src/schema/index.ts",
out: "./drizzle",
dbCredentials: {
url: env.DATABASE_URL,
},
} satisfies Config;
Sources: packages/db/drizzle.config.ts:1-12
Management Scripts
The packages/db/package.json file provides several scripts for developers to interact with the database schema.
| Script | Description |
|---|---|
db:generate | Generates SQL migration files based on changes in the schema. |
db:migrate | Applies pending migrations to the database. |
db:push | Pushes schema changes directly to the database without creating migration files (useful for development). |
db:studio | Starts the Drizzle Studio, a GUI for browsing the database. |
Sources: packages/db/package.json:8-13
Entity-Relationship Diagram
The following diagram illustrates the relationships between the main tables in the Analog database.
This diagram shows the core entities: user, account (for linked external providers), session, calendar, event, and apikey. A user can have multiple accounts, and each account can have multiple calendars and events.
Sources: packages/db/drizzle/meta/0008_snapshot.json
Table Schemas
The database is composed of several tables to manage authentication, calendar data, and other application concerns.
user
Stores information about registered users.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | Primary Key | Unique identifier for the user. |
name | text | Not Null | User's full name. |
email | text | Not Null, Unique | User's email address. |
email_verified | boolean | Not Null | Flag indicating if the email has been verified. |
image | text | Nullable | URL to the user's profile image. |
default_account_id | text | Nullable, Foreign Key to account.id | The default provider account for new events. |
default_calendar_id | text | Nullable | The default calendar for new events. |
created_at | timestamp | Not Null, Default now() | Timestamp of user creation. |
updated_at | timestamp | Not Null, Default now() | Timestamp of last user update. |
Sources: packages/db/src/schema/auth.ts:10-29, packages/db/drizzle/meta/0008_snapshot.json
account
Stores information about external accounts (e.g., Google, Microsoft) linked by a user.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | Primary Key | Unique identifier for the linked account. |
account_id | text | Not Null | The ID of the account from the external provider. |
provider_id | text | Not Null | The identifier of the provider (e.g., 'google'). |
name | text | Not Null | The name associated with the external account. |
email | text | Not Null | The email associated with the external account. |
user_id | text | Not Null, Foreign Key to user.id | The user who owns this account. |
access_token | text | Nullable | OAuth access token. |
refresh_token | text | Nullable | OAuth refresh token. |
access_token_expires_at | timestamp | Nullable | Expiration timestamp for the access token. |
Sources: packages/db/src/schema/auth.ts:31-54, packages/db/drizzle/meta/0008_snapshot.json
session
Stores user session information for authentication.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | Primary Key | Unique identifier for the session. |
expires_at | timestamp | Not Null | When the session expires. |
token | text | Not Null, Unique | The session token. |
user_id | text | Not Null, Foreign Key to user.id | The user associated with the session. |
ip_address | text | Nullable | IP address of the user. |
user_agent | text | Nullable | User agent string of the client. |
Sources: packages/db/src/schema/auth.ts:56-71, packages/db/drizzle/meta/0008_snapshot.json
calendar
Represents a calendar imported from a provider account.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | Primary Key | Unique identifier for the calendar record. |
name | text | Nullable | The name of the calendar. |
primary | boolean | Not Null, Default false | Whether this is the primary calendar for the account. |
color | text | Nullable | The color associated with the calendar. |
calendar_id | text | Not Null | The ID of the calendar from the external provider. |
provider_id | text | Not Null | The identifier of the provider (e.g., 'google'). |
account_id | text | Not Null, Foreign Key to account.id | The account this calendar belongs to. |
sync_token | text | Nullable | Token used for incremental synchronization. |
Sources: packages/db/src/schema/calendars.ts:13-39, packages/db/drizzle/meta/0008_snapshot.json
event
Represents an event within a calendar.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | Primary Key | Unique identifier for the event. |
title | text | Nullable | The title of the event. |
start | timestamp with time zone | Not Null | The start time of the event. |
end | timestamp with time zone | Not Null | The end time of the event. |
all_day | boolean | Nullable, Default false | Whether the event is an all-day event. |
recurring_event_id | text | Nullable | ID of the parent recurring event if this is an instance. |
calendar_id | text | Not Null, Foreign Key to calendar.id | The calendar this event belongs to. |
account_id | text | Not Null, Foreign Key to account.id | The account this event belongs to. |
Sources: packages/db/src/schema/calendars.ts:41-69, packages/db/drizzle/meta/0008_snapshot.json
apikey
Stores API keys for programmatic access to the Analog API.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | Primary Key | Unique identifier for the API key. |
name | text | Nullable | A user-defined name for the key. |
key | text | Not Null | The API key value. |
user_id | text | Not Null, Foreign Key to user.id | The user who owns the key. |
rate_limit_max | integer | Nullable, Default 10 | Maximum number of requests allowed in the time window. |
rate_limit_time_window | integer | Nullable, Default 86400000 | The time window for rate limiting in milliseconds. |
expires_at | timestamp | Nullable | Expiration timestamp for the key. |