1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
|
# Drizzle ORM Development Assistant
You are an expert in Drizzle ORM with deep knowledge of schema management, migrations, type safety, and modern database development patterns.
## Memory Integration
This CLAUDE.md file follows official Claude Code memory management patterns:
- **Project memory** - Shared with team via source control
- **Hierarchical loading** - Builds upon user and enterprise memory
- **Import support** - Can reference additional files with @path/to/file
- **Auto-discovery** - Loaded when Claude reads files in this subtree
## Available Commands
Use these project-specific slash commands:
- `/drizzle-schema [table-name]` - Generate type-safe schema
- `/drizzle-migrate [action]` - Handle migrations
- `/drizzle-query [type]` - Create optimized queries
- `/drizzle-seed [table]` - Generate seed data
## Project Context
This project uses **Drizzle ORM** for type-safe database operations with:
- **TypeScript-first** approach with full type inference
- **SQL-like syntax** that's familiar and powerful
- **Multiple database support** - PostgreSQL, MySQL, SQLite
- **Automatic migrations** with schema versioning
- **Performance optimized** with prepared statements
- **Edge runtime compatible** - Works with serverless
## Core Drizzle Principles
### 1. Schema Definition
- **Define schemas declaratively** using Drizzle's schema builders
- **Use proper types** for each column with validation
- **Establish relationships** with foreign keys and references
- **Index strategically** for query performance
- **Version schemas** with proper migration patterns
### 2. Type Safety
- **Full TypeScript inference** from schema to queries
- **Compile-time validation** of SQL operations
- **IntelliSense support** for table columns and relations
- **Runtime validation** with Drizzle's built-in validators
- **Type-safe joins** and complex queries
### 3. Migration Management
- **Generate migrations** automatically from schema changes
- **Version control migrations** with proper naming
- **Run migrations safely** in development and production
- **Rollback support** for schema changes
- **Seed data management** for consistent environments
## Database Setup
### PostgreSQL with Neon
```typescript
// lib/db.ts
import { drizzle } from 'drizzle-orm/neon-http';
import { neon, neonConfig } from '@neondatabase/serverless';
neonConfig.fetchConnectionCache = true;
const sql = neon(process.env.DATABASE_URL!);
export const db = drizzle(sql);
```
### SQLite for Local Development
```typescript
// lib/db.ts
import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';
const sqlite = new Database('./dev.db');
export const db = drizzle(sqlite);
```
### MySQL with PlanetScale
```typescript
// lib/db.ts
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
const connection = mysql.createPool({
uri: process.env.DATABASE_URL,
});
export const db = drizzle(connection);
```
## Schema Patterns
### User Management Schema
```typescript
// schema/users.ts
import { pgTable, serial, text, timestamp, boolean } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
email: text('email').notNull().unique(),
name: text('name').notNull(),
avatar: text('avatar'),
emailVerified: boolean('email_verified').default(false),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});
export type User = typeof users.$inferSelect;
export type NewUser = typeof users.$inferInsert;
```
### Content with Relations
```typescript
// schema/posts.ts
import { pgTable, serial, text, timestamp, integer } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
import { users } from './users';
export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
title: text('title').notNull(),
content: text('content').notNull(),
slug: text('slug').notNull().unique(),
published: boolean('published').default(false),
authorId: integer('author_id').references(() => users.id),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});
export const postsRelations = relations(posts, ({ one }) => ({
author: one(users, {
fields: [posts.authorId],
references: [users.id],
}),
}));
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));
export type Post = typeof posts.$inferSelect;
export type NewPost = typeof posts.$inferInsert;
```
### E-commerce Schema
```typescript
// schema/ecommerce.ts
import { pgTable, serial, text, integer, decimal, timestamp } from 'drizzle-orm/pg-core';
export const products = pgTable('products', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
description: text('description'),
price: decimal('price', { precision: 10, scale: 2 }).notNull(),
stock: integer('stock').default(0),
sku: text('sku').notNull().unique(),
categoryId: integer('category_id').references(() => categories.id),
createdAt: timestamp('created_at').defaultNow(),
});
export const categories = pgTable('categories', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
slug: text('slug').notNull().unique(),
description: text('description'),
});
export const orders = pgTable('orders', {
id: serial('id').primaryKey(),
userId: integer('user_id').references(() => users.id),
total: decimal('total', { precision: 10, scale: 2 }).notNull(),
status: text('status', { enum: ['pending', 'processing', 'shipped', 'delivered', 'cancelled'] }).default('pending'),
createdAt: timestamp('created_at').defaultNow(),
});
export const orderItems = pgTable('order_items', {
id: serial('id').primaryKey(),
orderId: integer('order_id').references(() => orders.id),
productId: integer('product_id').references(() => products.id),
quantity: integer('quantity').notNull(),
price: decimal('price', { precision: 10, scale: 2 }).notNull(),
});
```
## Query Patterns
### Basic CRUD Operations
```typescript
// lib/queries/users.ts
import { db } from '@/lib/db';
import { users } from '@/schema/users';
import { eq, desc, count } from 'drizzle-orm';
// Create user
export async function createUser(userData: NewUser) {
const [user] = await db.insert(users).values(userData).returning();
return user;
}
// Get user by ID
export async function getUserById(id: number) {
const user = await db.select().from(users).where(eq(users.id, id));
return user[0];
}
// Get user by email
export async function getUserByEmail(email: string) {
const user = await db.select().from(users).where(eq(users.email, email));
return user[0];
}
// Update user
export async function updateUser(id: number, userData: Partial<NewUser>) {
const [user] = await db
.update(users)
.set(userData)
.where(eq(users.id, id))
.returning();
return user;
}
// Delete user
export async function deleteUser(id: number) {
await db.delete(users).where(eq(users.id, id));
}
// Get paginated users
export async function getPaginatedUsers(page = 1, limit = 10) {
const offset = (page - 1) * limit;
const [userList, totalCount] = await Promise.all([
db.select().from(users).limit(limit).offset(offset).orderBy(desc(users.createdAt)),
db.select({ count: count() }).from(users),
]);
return {
users: userList,
total: totalCount[0].count,
page,
totalPages: Math.ceil(totalCount[0].count / limit),
};
}
```
### Complex Relations
```typescript
// lib/queries/posts.ts
import { db } from '@/lib/db';
import { posts, users } from '@/schema';
import { eq, desc, and, ilike } from 'drizzle-orm';
// Get posts with authors
export async function getPostsWithAuthors() {
return await db
.select({
id: posts.id,
title: posts.title,
content: posts.content,
published: posts.published,
createdAt: posts.createdAt,
author: {
id: users.id,
name: users.name,
email: users.email,
},
})
.from(posts)
.innerJoin(users, eq(posts.authorId, users.id))
.where(eq(posts.published, true))
.orderBy(desc(posts.createdAt));
}
// Search posts
export async function searchPosts(query: string) {
return await db
.select()
.from(posts)
.where(
and(
eq(posts.published, true),
ilike(posts.title, `%${query}%`)
)
)
.orderBy(desc(posts.createdAt));
}
// Get user's posts
export async function getUserPosts(userId: number) {
return await db
.select()
.from(posts)
.where(eq(posts.authorId, userId))
.orderBy(desc(posts.createdAt));
}
```
### Advanced Queries
```typescript
// lib/queries/analytics.ts
import { db } from '@/lib/db';
import { orders, orderItems, products, users } from '@/schema';
import { sum, count, avg, desc, gte } from 'drizzle-orm';
// Sales analytics
export async function getSalesAnalytics(startDate: Date, endDate: Date) {
return await db
.select({
totalRevenue: sum(orders.total),
totalOrders: count(orders.id),
averageOrderValue: avg(orders.total),
})
.from(orders)
.where(
and(
gte(orders.createdAt, startDate),
lte(orders.createdAt, endDate)
)
);
}
// Top selling products
export async function getTopSellingProducts(limit = 10) {
return await db
.select({
productId: products.id,
productName: products.name,
totalSold: sum(orderItems.quantity),
revenue: sum(orderItems.price),
})
.from(orderItems)
.innerJoin(products, eq(orderItems.productId, products.id))
.groupBy(products.id, products.name)
.orderBy(desc(sum(orderItems.quantity)))
.limit(limit);
}
```
## Migration Management
### Drizzle Config
```typescript
// drizzle.config.ts
import type { Config } from 'drizzle-kit';
export default {
schema: './src/schema/*',
out: './drizzle',
driver: 'pg',
dbCredentials: {
connectionString: process.env.DATABASE_URL!,
},
verbose: true,
strict: true,
} satisfies Config;
```
### Common Commands
```bash
# Generate migration
npx drizzle-kit generate:pg
# Run migrations
npx drizzle-kit push:pg
# Introspect existing database
npx drizzle-kit introspect:pg
# View migration status
npx drizzle-kit up:pg
# Studio (database browser)
npx drizzle-kit studio
```
### Migration Scripts
```typescript
// scripts/migrate.ts
import { drizzle } from 'drizzle-orm/neon-http';
import { migrate } from 'drizzle-orm/neon-http/migrator';
import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL!);
const db = drizzle(sql);
async function runMigrations() {
console.log('Running migrations...');
await migrate(db, { migrationsFolder: 'drizzle' });
console.log('Migrations completed!');
process.exit(0);
}
runMigrations().catch((err) => {
console.error('Migration failed!', err);
process.exit(1);
});
```
### Seed Data
```typescript
// scripts/seed.ts
import { db } from '@/lib/db';
import { users, posts, categories } from '@/schema';
async function seedDatabase() {
console.log('Seeding database...');
// Create users
const userIds = await db.insert(users).values([
{ email: 'admin@example.com', name: 'Admin User' },
{ email: 'user@example.com', name: 'Regular User' },
]).returning({ id: users.id });
// Create categories
const categoryIds = await db.insert(categories).values([
{ name: 'Technology', slug: 'technology' },
{ name: 'Design', slug: 'design' },
]).returning({ id: categories.id });
// Create posts
await db.insert(posts).values([
{
title: 'Getting Started with Drizzle',
content: 'Learn how to use Drizzle ORM...',
slug: 'getting-started-drizzle',
authorId: userIds[0].id,
published: true,
},
{
title: 'Database Design Best Practices',
content: 'Tips for designing scalable databases...',
slug: 'database-design-best-practices',
authorId: userIds[1].id,
published: true,
},
]);
console.log('Seeding completed!');
}
seedDatabase().catch(console.error);
```
## Performance Optimization
### Prepared Statements
```typescript
// lib/prepared-statements.ts
import { db } from '@/lib/db';
import { users } from '@/schema/users';
import { eq } from 'drizzle-orm';
// Prepare frequently used queries
export const getUserByIdPrepared = db
.select()
.from(users)
.where(eq(users.id, placeholder('id')))
.prepare();
export const getUserByEmailPrepared = db
.select()
.from(users)
.where(eq(users.email, placeholder('email')))
.prepare();
// Usage
const user = await getUserByIdPrepared.execute({ id: 123 });
```
### Indexes and Constraints
```typescript
// schema/optimized.ts
import { pgTable, serial, text, timestamp, index, uniqueIndex } from 'drizzle-orm/pg-core';
export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
title: text('title').notNull(),
slug: text('slug').notNull(),
content: text('content').notNull(),
authorId: integer('author_id').notNull(),
published: boolean('published').default(false),
createdAt: timestamp('created_at').defaultNow(),
}, (table) => ({
// Create indexes for better query performance
slugIdx: uniqueIndex('posts_slug_idx').on(table.slug),
authorIdx: index('posts_author_idx').on(table.authorId),
publishedIdx: index('posts_published_idx').on(table.published),
createdAtIdx: index('posts_created_at_idx').on(table.createdAt),
}));
```
### Connection Pooling
```typescript
// lib/db-pool.ts
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
const poolConnection = mysql.createPool({
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT || '3306'),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
export const db = drizzle(poolConnection);
```
## Testing Strategies
### Test Database Setup
```typescript
// tests/setup.ts
import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
export function createTestDb() {
const sqlite = new Database(':memory:');
const db = drizzle(sqlite);
// Run migrations
migrate(db, { migrationsFolder: 'drizzle' });
return db;
}
```
### Query Testing
```typescript
// tests/queries.test.ts
import { describe, it, expect, beforeEach } from 'vitest';
import { createTestDb } from './setup';
import { users } from '@/schema/users';
import { createUser, getUserByEmail } from '@/lib/queries/users';
describe('User Queries', () => {
let db: ReturnType<typeof createTestDb>;
beforeEach(() => {
db = createTestDb();
});
it('should create and retrieve user', async () => {
const userData = {
email: 'test@example.com',
name: 'Test User',
};
const user = await createUser(userData);
expect(user.email).toBe(userData.email);
const retrievedUser = await getUserByEmail(userData.email);
expect(retrievedUser).toEqual(user);
});
});
```
## Environment Configuration
```env
# Database URLs for different environments
DATABASE_URL=postgresql://username:password@localhost:5432/myapp_development
DATABASE_URL_TEST=postgresql://username:password@localhost:5432/myapp_test
DATABASE_URL_PRODUCTION=postgresql://username:password@host:5432/myapp_production
# For Neon (serverless PostgreSQL)
DATABASE_URL=postgresql://username:password@ep-cool-darkness-123456.us-east-1.aws.neon.tech/neondb?sslmode=require
# For PlanetScale (serverless MySQL)
DATABASE_URL=mysql://username:password@host.connect.psdb.cloud/database?sslmode=require
# For local SQLite
DATABASE_URL=file:./dev.db
```
## Common Patterns
### Repository Pattern
```typescript
// lib/repositories/user-repository.ts
import { db } from '@/lib/db';
import { users, User, NewUser } from '@/schema/users';
import { eq } from 'drizzle-orm';
export class UserRepository {
async create(userData: NewUser): Promise<User> {
const [user] = await db.insert(users).values(userData).returning();
return user;
}
async findById(id: number): Promise<User | undefined> {
const user = await db.select().from(users).where(eq(users.id, id));
return user[0];
}
async findByEmail(email: string): Promise<User | undefined> {
const user = await db.select().from(users).where(eq(users.email, email));
return user[0];
}
async update(id: number, userData: Partial<NewUser>): Promise<User> {
const [user] = await db
.update(users)
.set(userData)
.where(eq(users.id, id))
.returning();
return user;
}
async delete(id: number): Promise<void> {
await db.delete(users).where(eq(users.id, id));
}
}
export const userRepository = new UserRepository();
```
### Transaction Handling
```typescript
// lib/services/order-service.ts
import { db } from '@/lib/db';
import { orders, orderItems, products } from '@/schema';
import { eq, sql } from 'drizzle-orm';
export async function createOrderWithItems(
orderData: NewOrder,
items: Array<{ productId: number; quantity: number }>
) {
return await db.transaction(async (tx) => {
// Create order
const [order] = await tx.insert(orders).values(orderData).returning();
// Create order items and update product stock
for (const item of items) {
// Get product price
const product = await tx
.select({ price: products.price, stock: products.stock })
.from(products)
.where(eq(products.id, item.productId));
if (product[0].stock < item.quantity) {
throw new Error(`Insufficient stock for product ${item.productId}`);
}
// Create order item
await tx.insert(orderItems).values({
orderId: order.id,
productId: item.productId,
quantity: item.quantity,
price: product[0].price,
});
// Update product stock
await tx
.update(products)
.set({
stock: sql`${products.stock} - ${item.quantity}`,
})
.where(eq(products.id, item.productId));
}
return order;
});
}
```
## Resources
- [Drizzle ORM Documentation](https://orm.drizzle.team)
- [Drizzle Kit CLI](https://orm.drizzle.team/kit-docs/overview)
- [Schema Reference](https://orm.drizzle.team/docs/sql-schema-declaration)
- [Query Reference](https://orm.drizzle.team/docs/rqb)
- [Migration Guide](https://orm.drizzle.team/docs/migrations)
Remember: **Type safety first, optimize with indexes, use transactions for consistency, and prepare statements for performance!**
|