@thrivereflections/realtime-store-prisma
v0.1.0
Published
Prisma-based persistence store for the Thrive Realtime Voice Platform
Readme
@thrive/realtime-store-prisma
Prisma-based persistence store for the Thrive Realtime Voice Platform.
Overview
This package provides a Prisma-based implementation of the PersistenceStore interface, enabling database persistence for voice sessions, transcripts, tool events, and summaries.
Features
- Database Persistence: Store sessions, transcripts, tool events, and summaries
- Consent Management: Respect user consent before persisting data
- PII Redaction: Redact sensitive information before storage
- Graceful Fallback: Falls back to memory store when database is unavailable
- Type Safety: Full TypeScript support with Prisma-generated types
Installation
pnpm add @thrive/realtime-store-prismaUsage
Basic Setup
import { createPrismaStore } from "@thrive/realtime-store-prisma";
import { redact } from "@thrive/realtime-security";
const config = {
databaseUrl: process.env.DATABASE_URL!,
logLevel: "warn" as const
};
const deps = {
redact: redact
};
const store = createPrismaStore(config, deps);Database Setup
Install Prisma CLI:
pnpm add -D prismaSet up environment variables:
DATABASE_URL="postgresql://user:password@localhost:5432/voice_app" DIRECT_URL="postgresql://user:password@localhost:5432/voice_app"Generate Prisma client:
pnpm db:generateRun migrations:
pnpm db:migrate
Configuration
The store accepts the following configuration:
interface PrismaStoreConfig {
databaseUrl: string; // PostgreSQL connection string
logLevel?: "error" | "warn" | "info" | "query"; // Prisma log level
}Dependencies
The store requires a PII redaction function:
interface PrismaStoreDeps {
redact: (text: string) => string; // Function to redact PII
}API Reference
createPrismaStore(config, deps)
Creates a Prisma-based persistence store.
Parameters:
config: Database configurationdeps: Required dependencies (redaction function)
Returns: PersistenceStore implementation
Store Methods
The store implements the PersistenceStore interface:
saveSessionMeta(sessionId, user, config, timings, consent): Save session metadataappendTranscript(sessionId, segment): Append transcript segmentappendToolEvent(sessionId, event): Append tool eventpersistSummary(sessionId, summary): Persist session summary
Database Schema
The package includes a Prisma schema with the following models:
- AppUser: User accounts and authentication
- Session: Voice session metadata
- Transcript: Conversation transcripts
- ToolEvent: Tool call events and results
- Summary: Session summaries
- UsageEvent: Usage tracking and analytics
Consent Management
The store respects user consent settings:
- Only persists data when
consent === "ACCEPTED" - Skips persistence when consent is declined
- Defaults to
DECLINEDfor new users
PII Protection
All text data is redacted before storage:
- Phone numbers, emails, addresses are masked
- Sensitive information is replaced with placeholders
- Redaction is applied to transcripts and summaries
Error Handling
The store includes comprehensive error handling:
- Graceful fallback when database is unavailable
- Validation of date fields
- Duplicate prevention for sessions
- Detailed logging for debugging
Development
Scripts
pnpm build: Build the packagepnpm typecheck: Run TypeScript type checkingpnpm lint: Run ESLintpnpm db:generate: Generate Prisma clientpnpm db:push: Push schema to databasepnpm db:migrate: Run database migrationspnpm db:studio: Open Prisma Studio
Testing
# Run tests
pnpm test
# Run tests with database
DATABASE_URL="postgresql://..." pnpm testDependencies
@prisma/client: Prisma database client@thrive/realtime-contracts: Shared type definitions
License
MIT
