원클릭으로
epic-database
Guide on Prisma, SQLite, and LiteFS for Epic Stack
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
메뉴
Guide on Prisma, SQLite, and LiteFS for Epic Stack
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
SOC 직업 분류 기준
Guide on authentication, sessions, OAuth, 2FA, and passkeys for Epic Stack
Guide on caching with cachified, SQLite cache, and LRU cache for Epic Stack
Guide on deployment with Fly.io, multi-region setup, and CI/CD for Epic Stack
Guide on forms with Conform and validation with Zod for Epic Stack
Guide on RBAC system and permissions for Epic Stack
Guide on React patterns, performance optimization, and code quality for Epic Stack
| name | epic-database |
| description | Guide on Prisma, SQLite, and LiteFS for Epic Stack |
| categories | ["database","prisma","sqlite","litefs"] |
Use this skill when you need to:
Following Epic Web principles:
Do as little as possible - Only fetch the data you actually need. Use
select to fetch specific fields instead of entire models. Avoid over-fetching
data "just in case" - fetch what you need, when you need it.
Pragmatism over purity - Optimize queries when there's a measurable benefit, but don't over-optimize prematurely. Simple, readable queries are often better than complex optimized ones. Add indexes when queries are slow, not before.
Example - Fetch only what you need:
// ✅ Good - Fetch only needed fields
const user = await prisma.user.findUnique({
where: { id: userId },
select: {
id: true,
username: true,
name: true,
// Only fetch what you actually use
},
})
// ❌ Avoid - Fetching everything
const user = await prisma.user.findUnique({
where: { id: userId },
// Fetches all fields including password hash, email, etc.
})
Example - Pragmatic optimization:
// ✅ Good - Simple query first, optimize if needed
const notes = await prisma.note.findMany({
where: { ownerId: userId },
select: { id: true, title: true, updatedAt: true },
orderBy: { updatedAt: 'desc' },
take: 20,
})
// Only add indexes if this query is actually slow
// Don't pre-optimize
// ❌ Avoid - Over-optimizing before measuring
// Adding complex indexes, joins, etc. before knowing if it's needed
Epic Stack uses Prisma with SQLite as the database.
Basic configuration:
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["typedSql"]
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
Basic model:
model User {
id String @id @default(cuid())
email String @unique
username String @unique
name String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
notes Note[]
roles Role[]
}
model Note {
id String @id @default(cuid())
title String
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
owner User @relation(fields: [ownerId], references: [id])
ownerId String
@@index([ownerId])
@@index([ownerId, updatedAt])
}
Epic Stack uses CUID2 to generate unique IDs.
Advantages:
Example:
model User {
id String @id @default(cuid()) // Automatically generates CUID2
}
Standard fields:
model User {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt // Automatically updated
}
One-to-Many:
model User {
id String @id @default(cuid())
notes Note[]
}
model Note {
id String @id @default(cuid())
owner User @relation(fields: [ownerId], references: [id])
ownerId String
@@index([ownerId])
}
One-to-One:
model User {
id String @id @default(cuid())
image UserImage?
}
model UserImage {
id String @id @default(cuid())
user User @relation(fields: [userId], references: [id])
userId String @unique
}
Many-to-Many:
model User {
id String @id @default(cuid())
roles Role[]
}
model Role {
id String @id @default(cuid())
users User[]
}
Create indexes:
model Note {
id String @id @default(cuid())
ownerId String
updatedAt DateTime
@@index([ownerId]) // Simple index
@@index([ownerId, updatedAt]) // Composite index
}
Best practices:
where frequentlyorderByConfigure cascade:
model User {
id String @id @default(cuid())
notes Note[]
}
model Note {
id String @id @default(cuid())
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
ownerId String
}
Options:
onDelete: Cascade - Deletes children when parent is deletedonDelete: SetNull - Sets to null when parent is deletedonDelete: Restrict - Prevents deletion if there are childrenCreate migration:
npx prisma migrate dev --name add_user_field
Apply migrations in production:
npx prisma migrate deploy
Automatic migrations: Migrations are automatically applied on deploy via
litefs.yml.
"Widen then Narrow" strategy for zero-downtime:
Example: Rename field name to firstName and lastName:
// Step 1: Widen app (accepts both)
model User {
id String @id @default(cuid())
name String? // Deprecated
firstName String? // New
lastName String? // New
}
// Step 2: Widen db (migration copies data)
// In SQL migration:
ALTER TABLE User ADD COLUMN firstName TEXT;
ALTER TABLE User ADD COLUMN lastName TEXT;
UPDATE User SET firstName = name;
// Step 3: Narrow app (only uses new fields)
// Code only uses firstName and lastName
// Step 4: Narrow db (removes old field)
ALTER TABLE User DROP COLUMN name;
Import Prisma Client:
import { prisma } from '#app/utils/db.server.ts'
Basic query:
const user = await prisma.user.findUnique({
where: { id: userId },
})
Specific select:
const user = await prisma.user.findUnique({
where: { id: userId },
select: {
id: true,
email: true,
username: true,
// Don't include password or sensitive data
},
})
Include relations:
const user = await prisma.user.findUnique({
where: { id: userId },
include: {
notes: {
select: {
id: true,
title: true,
},
orderBy: { updatedAt: 'desc' },
},
roles: true,
},
})
Complex queries:
const notes = await prisma.note.findMany({
where: {
ownerId: userId,
title: { contains: searchTerm },
},
select: {
id: true,
title: true,
updatedAt: true,
},
orderBy: { updatedAt: 'desc' },
take: 20,
skip: (page - 1) * 20,
})
Use transactions:
await prisma.$transaction(async (tx) => {
const user = await tx.user.create({
data: {
email,
username,
roles: { connect: { name: 'user' } },
},
})
await tx.note.create({
data: {
title: 'Welcome',
content: 'Welcome to the app!',
ownerId: user.id,
},
})
return user
})
Multi-region with LiteFS:
Check primary instance:
import { ensurePrimary, getInstanceInfo } from '#app/utils/litefs.server.ts'
export async function action({ request }: Route.ActionArgs) {
// Ensure we're on primary instance for writes
await ensurePrimary()
// Now we can write safely
await prisma.user.create({
data: {
/* ... */
},
})
}
Get instance information:
import { getInstanceInfo } from '#app/utils/litefs.server.ts'
const { currentIsPrimary, primaryInstance } = await getInstanceInfo()
if (currentIsPrimary) {
// Can write
} else {
// Read-only, redirect to primary if necessary
}
Create seed:
// prisma/seed.ts
import { prisma } from '#app/utils/db.server.ts'
async function seed() {
// Create roles
await prisma.role.createMany({
data: [
{ name: 'user', description: 'Standard user' },
{ name: 'admin', description: 'Administrator' },
],
})
// Create users
const user = await prisma.user.create({
data: {
email: 'user@example.com',
username: 'testuser',
roles: { connect: { name: 'user' } },
},
})
console.log('Seed complete!')
}
seed()
.catch((e) => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})
Run seed:
npx prisma db seed
# Or directly:
npx tsx prisma/seed.ts
Guidelines (pragmatic approach):
select to fetch only needed fields - do as little as possibleinclude - only include relations you actually usewhere and orderBy - but only if queries are slowselect: true (fetches everything) - be explicit about what you needOptimized example (do as little as possible):
// ❌ Avoid: Fetches everything unnecessarily
const user = await prisma.user.findUnique({
where: { id: userId },
// Fetches password hash, email, all relations, etc.
})
// ✅ Good: Only needed fields - do as little as possible
const user = await prisma.user.findUnique({
where: { id: userId },
select: {
id: true,
username: true,
name: true,
// Only what you actually use
},
})
// ✅ Better: With selective relations (only if you need them)
const user = await prisma.user.findUnique({
where: { id: userId },
select: {
id: true,
username: true,
notes: {
select: {
id: true,
title: true,
},
take: 10, // Only fetch what you need
},
},
})
Configure logging:
// app/utils/db.server.ts
const client = new PrismaClient({
log: [
{ level: 'query', emit: 'event' },
{ level: 'error', emit: 'stdout' },
{ level: 'warn', emit: 'stdout' },
],
})
client.$on('query', async (e) => {
if (e.duration < 20) return // Only log slow queries
console.info(`prisma:query - ${e.duration}ms - ${e.query}`)
})
Development:
DATABASE_URL=file:./data/db.sqlite
Production (Fly.io):
DATABASE_URL=file:/litefs/data/sqlite.db
SSH to Fly instance:
fly ssh console --app [YOUR_APP_NAME]
Connect to DB CLI:
fly ssh console -C database-cli --app [YOUR_APP_NAME]
Prisma Studio:
# Terminal 1: Start Prisma Studio
fly ssh console -C "npx prisma studio" -s --app [YOUR_APP_NAME]
# Terminal 2: Local proxy
fly proxy 5556:5555 --app [YOUR_APP_NAME]
# Open in browser
# http://localhost:5556
model Post {
id String @id @default(cuid())
title String
content String
published Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
authorId String
comments Comment[]
tags Tag[]
@@index([authorId])
@@index([authorId, published])
@@index([published, updatedAt])
}
model Comment {
id String @id @default(cuid())
content String
createdAt DateTime @default(now())
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
postId String
author User @relation(fields: [authorId], references: [id])
authorId String
@@index([postId])
@@index([authorId])
}
export async function getPosts({
userId,
page = 1,
perPage = 20,
published,
}: {
userId?: string
page?: number
perPage?: number
published?: boolean
}) {
const where: Prisma.PostWhereInput = {}
if (userId) {
where.authorId = userId
}
if (published !== undefined) {
where.published = published
}
const [posts, total] = await Promise.all([
prisma.post.findMany({
where,
select: {
id: true,
title: true,
updatedAt: true,
author: {
select: {
id: true,
username: true,
},
},
},
orderBy: { updatedAt: 'desc' },
take: perPage,
skip: (page - 1) * perPage,
}),
prisma.post.count({ where }),
])
return {
posts,
total,
pages: Math.ceil(total / perPage),
}
}
export async function createPostWithTags({
authorId,
title,
content,
tagNames,
}: {
authorId: string
title: string
content: string
tagNames: string[]
}) {
return await prisma.$transaction(async (tx) => {
// Create tags if they don't exist
await Promise.all(
tagNames.map((name) =>
tx.tag.upsert({
where: { name },
update: {},
create: { name },
}),
),
)
// Create post
const post = await tx.post.create({
data: {
title,
content,
authorId,
tags: {
connect: tagNames.map((name) => ({ name })),
},
},
})
return post
})
}
async function seed() {
// Create permissions
const permissions = await Promise.all([
prisma.permission.create({
data: {
action: 'create',
entity: 'note',
access: 'own',
description: 'Can create own notes',
},
}),
prisma.permission.create({
data: {
action: 'read',
entity: 'note',
access: 'own',
description: 'Can read own notes',
},
}),
])
// Create roles with permissions
const userRole = await prisma.role.create({
data: {
name: 'user',
description: 'Standard user',
permissions: {
connect: permissions.map((p) => ({ id: p.id })),
},
},
})
// Create user with role
const user = await prisma.user.create({
data: {
email: 'user@example.com',
username: 'testuser',
roles: {
connect: { id: userRole.id },
},
},
})
console.log('Seed complete!')
}
select to fetch only what you need -
do as little as possibleinclude to fetch relations in a single query when
you need themensurePrimary() before writes in
productiononDelete in relations: Explicitly decide what to do when
parent is deletedprisma/schema.prisma - Complete schemaprisma/seed.ts - Seed exampleapp/utils/db.server.ts - Prisma Client setupapp/utils/litefs.server.ts - LiteFS utilities