The GitHub Repository Analyzer uses SQLite with Prisma ORM for persistent data storage. This enables:
- Repository analysis history
- Chat session persistence
- Message conversation history
- Pagination and efficient querying
The database is automatically created and initialized when you run:
npm run devThis will:
- Create/migrate the SQLite database (
dev.db) - Create all necessary tables
- Validate the connection
If needed, manually run migrations:
npx prisma migrate devRepository
id(primary key)owner— Repository owner usernamename— Repository namefullName—owner/name(unique)url— GitHub repository URLstars— Star countforks— Fork countlanguage— Primary languagedescription— Repository descriptioncreatedAt,updatedAt— Timestamps
Analysis
id(primary key)repositoryId— Foreign key to Repositorysummary— Repository summarystructure— Architecture/structure infokeyFiles— Important files (JSON array)technologies— Technology stack (JSON array)createdAt,updatedAt— Timestamps
ChatSession
id(primary key)repositoryId— Foreign key to Repositorytitle— Session titlecreatedAt,updatedAt— Timestamps
ChatMessage
id(primary key)sessionId— Foreign key to ChatSessionrole— Message role ('user' or 'assistant')content— Message contentcreatedAt— Timestamp
View and edit database records with a GUI:
npx prisma studioOpens at http://localhost:5555
The prisma client is exported from src/lib/prisma.ts:
import { prisma } from '@/lib/prisma';
// Create
const repo = await prisma.repository.create({
data: { owner: 'facebook', name: 'react', ... }
});
// Read
const repo = await prisma.repository.findUnique({
where: { fullName: 'facebook/react' }
});
// Update
await prisma.repository.update({
where: { id: 'xxx' },
data: { stars: 200000 }
});
// Delete
await prisma.repository.delete({
where: { id: 'xxx' }
});Database indexes are defined on:
Repository.fullName(unique)Repository(owner, name)Analysis.repositoryIdChatSession.repositoryIdChatMessage.sessionId
For better performance:
// ✅ Efficient - loads related data
const session = await prisma.chatSession.findUnique({
where: { id },
include: { messages: true, repository: true }
});
// ✅ Efficient - pagination
const sessions = await prisma.chatSession.findMany({
take: 10,
skip: page * 10,
orderBy: { createdAt: 'desc' }
});
// ❌ Inefficient - N+1 queries
for (const session of sessions) {
const messages = await prisma.chatMessage.findMany({
where: { sessionId: session.id }
});
}On Windows, SQLite file locking can cause timeouts. The solution:
Database URL Configuration:
DATABASE_URL="file:./dev.db?timeout=10000"The ?timeout=10000 parameter gives SQLite 10 seconds to acquire file locks.
Retry Logic:
All API routes implement exponential backoff retry logic (up to 3 attempts) for timeout errors.
Cause: Multiple processes accessing the database simultaneously Solution:
- Stop the dev server
- Clear lock files:
rm -rf .next - Restart:
npm run dev
Cause: Corrupted database file Solution:
rm dev.db dev.db-journal
npm run dev # Recreates databaseCheck:
- Use Prisma Studio to inspect data volume
- Review indexes on frequently queried fields
- Use
.include()instead of separate queries
cp dev.db dev.db.backupnpx prisma db execute --stdin < export.sqlFor production, consider:
- PostgreSQL or MySQL (SQLite is dev-only)
- Connection pooling (PgBouncer for Postgres)
- Automated backups
- Read replicas
Update prisma/schema.prisma:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}Then redeploy with: npx prisma migrate deploy