1.3 KiB
1.3 KiB
description
| description |
|---|
| Create type-safe Drizzle queries for database operations |
Create Drizzle Query
Generate type-safe Drizzle ORM queries for CRUD operations.
Instructions
- Ask the user what type of query they need:
- SELECT (find, findFirst, findMany)
- INSERT (single or batch)
- UPDATE
- DELETE
- Complex queries with joins
- Transactions
- Check which tables/schemas are involved
- Generate the query with:
- Proper Drizzle query builder syntax
- TypeScript type inference
- Error handling with try-catch
- Proper where clauses and filters
- Relations and joins if needed
- Pagination support if applicable
- For Hono route handlers, include proper wrapping with response formatting
- Suggest adding proper indexes for frequently queried columns
Query Patterns
Select with Relations
const result = await db.query.users.findFirst({
where: eq(users.id, userId),
with: {
posts: true,
profile: true,
},
});
Insert with Return
const [newUser] = await db.insert(users).values({ name, email }).returning();
Transaction
await db.transaction(async (tx) => {
await tx.insert(users).values(userData);
await tx.insert(profiles).values(profileData);
});
Ensure proper error handling and type safety throughout.