Configure ORMs like Prisma, TypeORM, or Sequelize for optimal database access.
You are an ORM expert. Help me configure my ORM properly.
## Project Details
ORM choice: ${{ORM}}
Database: ${{DATABASE}}
Complexity: ${{COMPLEXITY}}
Performance needs: ${{PERFORMANCE}}
## Please Configure:
1. **Prisma Setup**
// schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch"]
}
model User {
id Int @id @default(autoincrement())
email String @unique
posts Post[]
createdAt DateTime @default(now())
}
2. **TypeORM Configuration**
// data-source.ts
export const AppDataSource = new DataSource({
type: "postgres",
host: process.env.DB_HOST,
port: 5432,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
synchronize: false,
logging: ["error", "warn"],
entities: ["src/entities/**/*.ts"],
migrations: ["src/migrations/**/*.ts"],
poolSize: 10,
});
3. **Entity Definitions**
// TypeORM entity:
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ unique: true })
email: string;
@OneToMany(() => Post, post => post.author)
posts: Post[];
@CreateDateColumn()
createdAt: Date;
}
4. **Relations**
// Prisma relations:
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
}
// TypeORM relations:
@ManyToOne(() => User, user => user.posts)
@JoinColumn({ name: 'author_id' })
author: User;
5. **Query Optimization**
// Eager loading:
const users = await prisma.user.findMany({
include: { posts: true }
});
// Select specific fields:
const users = await prisma.user.findMany({
select: { id: true, email: true }
});
// Raw queries when needed:
await prisma.$queryRaw`SELECT * FROM users WHERE...`;
6. **Migrations**
# Prisma migrations:
npx prisma migrate dev --name init
npx prisma migrate deploy
# TypeORM migrations:
npx typeorm migration:generate -n Init
npx typeorm migration:run
7. **Connection Pooling**
// Pool configuration:
const prisma = new PrismaClient({
datasources: {
db: {
url: process.env.DATABASE_URL + "?pool_timeout=10&connection_limit=10"
}
}
});
8. **Testing**
// Test setup:
// - Test database
// - Transaction rollback
// - Mocking strategies
Or press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[{ORM][{DATABASE][{COMPLEXITY][{PERFORMANCE]{
provider = "postgresql"
url = env("DATABASE_URL")
}{
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch"]
}{
id Int @id @default(autoincrement())
email String @unique
posts Post[]
createdAt DateTime @default(now())
}{
type: "postgres",
host: process.env.DB_HOST,
port: 5432,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
synchronize: false,
logging: ["error", "warn"],
entities: ["src/entities/**/*.ts"],
migrations: ["src/migrations/**/*.ts"],
poolSize: 10,
}{
@PrimaryGeneratedColumn()
id: number;
@Column({ unique: true }{
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
}{ name: 'author_id' }{
include: { posts: true }{
select: { id: true, email: true }{
datasources: {
db: {
url: process.env.DATABASE_URL + "?pool_timeout=10&connection_limit=10"
}