ORMs
Learn about Prisma, Drizzle, and Mongoose support in kodkod.
ORMs
kodkod supports three modern ORMs: Prisma, Drizzle, and Mongoose. The available options depend on your database choice:
- PostgreSQL/MySQL: Prisma or Drizzle
- MongoDB: Mongoose or Prisma
Prisma
Prisma is a next-generation ORM with auto-generated types, a visual database browser, and an intuitive schema language.
Best for:
- Rapid development with auto-generated types
- Teams who prefer declarative schema definitions
- Projects needing Prisma Studio for database visualization
npx kodkod-stack@latest my-app --orm prismaSchema Example
// prisma/schema.prisma
model User {
id String @id @default(cuid())
email String @unique
name String?
createdAt DateTime @default(now())
}Usage
import { db } from '../db';
// Create a user
const user = await db.user.create({
data: { email: 'hello@example.com', name: 'Alice' }
});
// Find a user
const found = await db.user.findUnique({ where: { id: user.id } });Commands
| Command | Description |
|---|---|
npx prisma migrate dev | Run migrations in development |
npx prisma generate | Generate Prisma client |
npx prisma studio | Open visual database browser |
Drizzle
Drizzle is a lightweight, SQL-like ORM that gives you full control over your queries while maintaining type safety.
Best for:
- Developers who prefer writing SQL-like syntax
- Projects requiring fine-grained query control
- Lightweight deployments
npx kodkod-stack@latest my-app --orm drizzleSchema Example
// src/db/schema.ts
import { pgTable, text, timestamp } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: text('id').primaryKey(),
email: text('email').notNull().unique(),
name: text('name'),
createdAt: timestamp('created_at').defaultNow(),
});Usage
import { db } from '../db';
import { users } from '../db/schema';
import { eq } from 'drizzle-orm';
// Create a user
await db.insert(users).values({ id: '1', email: 'hello@example.com' });
// Find a user
const user = await db.select().from(users).where(eq(users.id, '1'));Commands
| Command | Description |
|---|---|
npx drizzle-kit push | Push schema changes to database |
npx drizzle-kit generate | Generate migration files |
npx drizzle-kit studio | Open visual database browser |
Mongoose
Mongoose is the most popular MongoDB ODM for Node.js, providing schema validation and a rich query API.
Best for:
- MongoDB-native development
- Teams familiar with the MongoDB ecosystem
- Projects needing schema validation and middleware
npx kodkod-stack@latest my-app --database mongodb --orm mongooseSchema Example
// src/models/user.model.ts
import mongoose, { Schema, Document } from 'mongoose';
export interface IUser extends Document {
email: string;
name?: string;
}
const UserSchema = new Schema<IUser>({
email: { type: String, required: true, unique: true },
name: { type: String },
}, { timestamps: true });
export const User = mongoose.model<IUser>('User', UserSchema);Usage
import { User } from '../models/user.model';
// Create a user
const user = await User.create({ email: 'hello@example.com', name: 'Alice' });
// Find a user
const found = await User.findById(userId);
// Update a user
await User.findByIdAndUpdate(userId, { name: 'Bob' });Comparison
| Feature | Prisma | Drizzle | Mongoose |
|---|---|---|---|
| Query Style | Method chaining | SQL-like | Document-based |
| Database Support | SQL + MongoDB | SQL only | MongoDB only |
| Bundle Size | Larger | Smaller | Medium |
| Type Safety | Excellent | Excellent | Good |
| Learning Curve | Easier | Moderate | Easier |
