Overview
The Drizzle provider is a production-ready implementation of theConversationStore interface using PostgreSQL and Drizzle ORM.
Installation
Install the required dependencies:Database Schema
The provider uses two tables in theff_ai schema:
threads Table
Stores conversation threads:Internal primary key (auto-incrementing)
Your application’s thread identifier (from
ThreadIdentifier.threadId)Your application’s resource identifier (from
ThreadIdentifier.resourceId)When the thread was first created
When the thread was last updated (any message activity)
messages Table
Stores individual messages:Internal primary key for efficient querying
The message’s public UUID (from
ConversationMessage.id)Foreign key to threads table (cascading delete)
The complete AI SDK
ModelMessage object (role, content, etc.)When the message was created
Setup
1. Create the Database Schema
Generate and run migrations:2. Configure the Provider
3. Use in Your Application
API Reference
createDrizzleStoreLayer
Create a Layer that provides the ConversationStore service.A postgres.js connection instance
Database column naming convention. Set to
'snake_case' if your database uses snake_case naming.An Effect Layer that provides the ConversationStore service
Implementation Details
Window Size Query
The provider implements the window size feature efficiently:- Finds the thread by
resourceIdandpublicId - Queries for the N most recent user messages
- Identifies the oldest user message in the window
- Returns all messages (user, assistant, tool) from that point forward
Transactions
ThesaveMessages method uses a database transaction to ensure atomicity:
- Thread is created/updated atomically with messages
- Either all messages save or none do (no partial writes)
- Thread
updatedAtalways reflects latest activity
Cascade Deletes
Messages are automatically deleted when their thread is deleted:Performance Considerations
Indexes
Indexes
The schema includes essential indexes:Consider adding:
Connection Pooling
Connection Pooling
Use connection pooling for better performance:
JSONB Performance
JSONB Performance
The
aiSdkV5 column stores the complete message as JSONB:- Pros: Flexible schema, easy queries, no migration needed
- Cons: Slightly slower than relational columns
- Extracting frequently-queried fields to columns
- Using JSONB operators for efficient queries
- Adding GIN indexes for JSONB queries
Window Size Impact
Window Size Impact
Larger window sizes require more database queries:
- Window size 10: ~50-100 messages typically returned
- Window size 50: ~250-500 messages typically returned
Troubleshooting
Schema not found error
Schema not found error
Ensure the
ff_ai schema exists:Connection errors
Connection errors
Check your connection string and network:
Casing mismatches
Casing mismatches
If you see column name errors, set the casing option:
Foreign key violations
Foreign key violations
Ensure threads are created before messages:
Migration from Other Stores
If you’re migrating from another storage system:Next Steps
Turn Handler
Use the turn handler with Drizzle storage
Conversation Store
Learn about the store interface
Examples
See complete examples with Drizzle