Express with Prisma Project Structure
Express integrated with Prisma ORM for type-safe database operations with auto-generated types.
Project Directory
myproject/
src/
Application source
index.ts
Server startup
app.ts
Express app setup
config/
index.ts
database.ts
Prisma client init
routes/
index.ts
user.routes.ts
post.routes.ts
controllers/
user.controller.ts
post.controller.ts
services/
Business logic
user.service.ts
Uses Prisma client
post.service.ts
middleware/
error-handler.ts
Prisma errors
validation.ts
types/
index.ts
prisma/
Prisma configuration
schema.prisma
Database schema
seed.ts
Seed script
migrations/
.gitkeep
tests/
setup.ts
user.test.ts
package.json
tsconfig.json
.env.example
DATABASE_URL here
.gitignore
Why This Structure?
Prisma generates TypeScript types from schema.prisma—no manual interface definitions. Services inject the Prisma client and get full autocompletion. Migrations version-control your schema changes.
Key Directories
- prisma/schema.prisma-Single source of truth for models
- prisma/migrations/-Database version history
- src/config/database.ts-Prisma client singleton
- src/services/-Business logic uses Prisma directly
Getting Started
npm installcp .env.example .env(set DATABASE_URL)npx prisma migrate devnpx prisma db seednpm run dev
Best Practices
- Export a singleton Prisma client from
config/database.ts - Use
selectandincludeto avoid over-fetching - Handle
PrismaClientKnownRequestErrorin error middleware - Run
prisma generatein CI after migrations - Use transactions for multi-table writes
Trade-offs
- No repository abstraction-Services use Prisma directly—simpler but coupled
- Prisma schema lock-in-Schema syntax specific to Prisma
- Complex queries-May need
$queryRawfor advanced SQL