Overview
The Hono app uses PostgreSQL as its database with Drizzle ORM for type-safe database operations. The schema is defined insrc/db/schema.ts.
Common Fields
File:src/db/schema.ts:14-18
All tables with timestamps include these common fields:
createdAt- Automatically set to current timestamp on creationupdatedAt- Automatically updated to current timestamp on modificationdeletedAt- Nullable timestamp for soft deletes
Authentication Tables
These tables are used by Better Auth for authentication and session management.user
File:src/db/schema.ts:22-29
Stores user account information.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | PRIMARY KEY | Unique user identifier |
name | text | NOT NULL | User’s display name |
email | text | NOT NULL, UNIQUE | User’s email address |
emailVerified | boolean | NOT NULL, DEFAULT false | Email verification status |
image | text | nullable | URL to user’s profile image |
createdAt | timestamp | NOT NULL | Account creation timestamp |
updatedAt | timestamp | nullable | Last update timestamp |
deletedAt | timestamp | nullable | Soft delete timestamp |
- One-to-many with
sessiontable - One-to-many with
accounttable
session
File:src/db/schema.ts:33-43
Stores active user sessions.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | PRIMARY KEY | Unique session identifier |
expiresAt | timestamp | NOT NULL | Session expiration time |
token | text | NOT NULL, UNIQUE | Session token for authentication |
ipAddress | text | nullable | IP address of the session |
userAgent | text | nullable | Browser/client user agent |
userId | text | NOT NULL, FOREIGN KEY | Reference to user |
createdAt | timestamp | NOT NULL | Session creation timestamp |
updatedAt | timestamp | nullable | Last update timestamp |
deletedAt | timestamp | nullable | Soft delete timestamp |
userIdreferencesuser.idwith CASCADE delete
account
File:src/db/schema.ts:47-62
Stores OAuth provider accounts and credentials linked to users.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | PRIMARY KEY | Unique account identifier |
accountId | text | NOT NULL | Provider-specific account ID |
providerId | text | NOT NULL | OAuth provider identifier |
userId | text | NOT NULL, FOREIGN KEY | Reference to user |
accessToken | text | nullable | OAuth access token |
refreshToken | text | nullable | OAuth refresh token |
idToken | text | nullable | OAuth ID token |
accessTokenExpiresAt | timestamp | nullable | Access token expiration |
refreshTokenExpiresAt | timestamp | nullable | Refresh token expiration |
scope | text | nullable | OAuth scope |
password | text | nullable | Hashed password for email/password auth |
createdAt | timestamp | NOT NULL | Account creation timestamp |
updatedAt | timestamp | nullable | Last update timestamp |
deletedAt | timestamp | nullable | Soft delete timestamp |
userIdreferencesuser.idwith CASCADE delete
- OAuth provider accounts (Google, GitHub, etc.)
- Email/password authentication (password field)
- Token management for API access
verification
File:src/db/schema.ts:66-72
Stores verification tokens for email verification, password resets, etc.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | text | PRIMARY KEY | Unique verification identifier |
identifier | text | NOT NULL | Email or identifier to verify |
value | text | NOT NULL | Verification token/code |
expiresAt | timestamp | NOT NULL | Token expiration time |
createdAt | timestamp | NOT NULL | Token creation timestamp |
updatedAt | timestamp | nullable | Last update timestamp |
deletedAt | timestamp | nullable | Soft delete timestamp |
- Email verification tokens
- Password reset tokens
- Magic link authentication
- Two-factor authentication codes
Rate Limiting Table
rate_limit
File:src/db/schema.ts:79-84
Stores rate limiting data for request throttling.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PRIMARY KEY | Auto-generated UUID |
key | text | NOT NULL, UNIQUE | Rate limit key (session ID or IP) |
count | integer | NOT NULL, DEFAULT 0 | Request count in current window |
lastRequest | bigint | NOT NULL | Timestamp of last request (milliseconds) |
- Authenticated users:
session:<session_id> - Anonymous users:
ip:<ip_address>
- Tracks request counts per client within time windows
- Used by
DbStorein rate limiting middleware - Automatically managed by rate limiter (increment/decrement/reset)
- Expired records are automatically deleted on access
lastRequest.
Schema Exports
File:src/db/schema.ts:30-31,44-45,63-64,73-75
Each table exports both a Zod schema and TypeScript type:
- Runtime validation with Zod schemas
- Type safety with TypeScript types
- Auto-generated from Drizzle table definitions
- Consistent with database schema
Database Connection
File:src/db/index.ts
The database connection is exported from the db module:
src/auth/libs/index.ts:16-24
