How the database persistence architecture works
mainThe application uses a prefix-based approach to persist chat messages in PostgreSQL, avoiding complex polymorphic relationships while maintaining type safety and performance.
Schema Design
The system relies on three primary tables:
chats: Stores chat sessions with auto-generated IDs.messages: Stores individual messages, including their role, timestamp, and a reference to the parent chat.parts: Stores the content of message parts using a prefix-based column naming convention.
Prefix Convention
Each message part type uses dedicated columns with specific prefixes to ensure structured storage:
text_*: Text content.reasoning_*: Reasoning or thinking process.file_*: File attachments.source_url_*: URL sources.source_document_*: Document sources.tool_[toolName]_*: Tool calls (e.g.,tool_getWeatherInformation_*).data_[dataType]_*: Custom data parts (e.g.,data_weather_*).
Data Integrity
The schema enforces integrity through constraints that require complete part definitions. For example, a source URL part must include both source_url_url and source_url_description. Tool calls must also provide a complete set of columns (input, output, state, etc.) to prevent partial or corrupted data.