Setup Prisma 7 with TypeScript (Full ESM support)
A modern, fully-typed CRUD application built with Prisma 7, TypeScript, and PostgreSQL. This project demonstrates best practices for setting up Prisma with full ESM (ECMAScript Module) support, including Docker containerization for database management.
- β Prisma 7 - Latest ORM for Node.js and TypeScript
- β Full ESM Support - Native ES modules throughout
- β TypeScript - Complete type safety and excellent IDE support
- β PostgreSQL - Robust relational database
- β Docker Compose - Easy database setup and management
- β
Type-safe Adapter - Using
@prisma/adapter-pgfor PostgreSQL
- Node.js (v18 or higher recommended)
- npm or yarn package manager
- Docker and Docker Compose (for PostgreSQL)
git clone https://github.com/surojcodes/prisma-crud.git
cd prisma-crudnpm installCopy the example environment file:
cp .env.example .envThe .env file should contain:
DATABASE_URL="postgresql://postgres:password@localhost:5432/my_prisma_app?schema=public"docker-compose up -dThis will start a PostgreSQL 16 database container with the following credentials:
- User:
postgres - Password:
password - Database:
my_prisma_app - Port:
5432
Run Prisma migrations:
npx prisma migrate devThis will:
- Create your database schema from the Prisma schema
- Generate Prisma Client
Build the TypeScript code:
npm run buildStart the application:
npm startprisma-crud/
βββ src/ # TypeScript source code
βββ prisma/ # Prisma configuration and migrations
β βββ schema.prisma # Database schema definition
β βββ migrations/ # Database migration files
βββ dist/ # Compiled JavaScript output
βββ docker-compose.yml # Docker database setup
βββ prisma.config.ts # Prisma CLI configuration
βββ package.json # Project dependencies and scripts
βββ tsconfig.json # TypeScript configuration
βββ .env.example # Environment variables template
βββ .gitignore # Git ignore rules
- @prisma/client
^7.4.1- Prisma Client for database queries - @prisma/adapter-pg
^7.4.1- PostgreSQL adapter for Prisma - pg
^8.18.0- PostgreSQL client library
- prisma
^7.4.1- Prisma CLI and tooling - typescript
^5.9.3- TypeScript compiler - @types/node
^25.3.0- Node.js type definitions - @types/pg
^8.16.0- PostgreSQL type definitions
# Build TypeScript to JavaScript
npm run build
# Start the application
npm start
# Run Prisma migrations
npx prisma migrate dev
# Open Prisma Studio (GUI for database)
npx prisma studio
# Generate Prisma Client
npx prisma generate
# Reset database (be careful!)
npx prisma migrate resetdocker-compose up -ddocker-compose downdocker-compose logs -f dbdocker-compose exec db psql -U postgres -d my_prisma_appTypeScript compiler configuration with ESM support:
- Target: ES2020
- Module: ESNext
- Module Resolution: Node
Prisma CLI configuration for development:
- Schema location:
prisma/schema.prisma - Migrations path:
prisma/migrations - Database URL from environment variables
PostgreSQL service configuration:
- Image: PostgreSQL 16
- Container name:
my-prisma-db - Credentials:
postgres/password - Database:
my_prisma_app - Data persistence: Named volume
postgres_data
After setting up the database, you can use Prisma Client in your TypeScript code:
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main() {
// Create
const user = await prisma.user.create({
data: {
email: 'user@example.com',
name: 'John Doe',
},
})
// Read
const allUsers = await prisma.user.findMany()
// Update
const updated = await prisma.user.update({
where: { id: user.id },
data: { name: 'Jane Doe' },
})
// Delete
await prisma.user.delete({
where: { id: user.id },
})
}
main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})ISC - See LICENSE file for details
Suroj M - GitHub
Contributions are welcome! Feel free to open issues and pull requests.
For questions or issues, please open an issue in the GitHub repository.