Initial commit
This commit is contained in:
58
commands/create-query.md
Normal file
58
commands/create-query.md
Normal file
@@ -0,0 +1,58 @@
|
||||
---
|
||||
description: Create type-safe Drizzle queries for database operations
|
||||
---
|
||||
|
||||
# Create Drizzle Query
|
||||
|
||||
Generate type-safe Drizzle ORM queries for CRUD operations.
|
||||
|
||||
## Instructions
|
||||
|
||||
1. Ask the user what type of query they need:
|
||||
- SELECT (find, findFirst, findMany)
|
||||
- INSERT (single or batch)
|
||||
- UPDATE
|
||||
- DELETE
|
||||
- Complex queries with joins
|
||||
- Transactions
|
||||
2. Check which tables/schemas are involved
|
||||
3. 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
|
||||
4. For Hono route handlers, include proper wrapping with response formatting
|
||||
5. Suggest adding proper indexes for frequently queried columns
|
||||
|
||||
## Query Patterns
|
||||
|
||||
### Select with Relations
|
||||
|
||||
```typescript
|
||||
const result = await db.query.users.findFirst({
|
||||
where: eq(users.id, userId),
|
||||
with: {
|
||||
posts: true,
|
||||
profile: true,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Insert with Return
|
||||
|
||||
```typescript
|
||||
const [newUser] = await db.insert(users).values({ name, email }).returning();
|
||||
```
|
||||
|
||||
### Transaction
|
||||
|
||||
```typescript
|
||||
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.
|
||||
41
commands/create-schema.md
Normal file
41
commands/create-schema.md
Normal file
@@ -0,0 +1,41 @@
|
||||
---
|
||||
description: Create a new Drizzle schema file with table definitions
|
||||
---
|
||||
|
||||
# Create Drizzle Schema
|
||||
|
||||
Generate a new Drizzle ORM schema file with proper TypeScript types and Postgres column definitions.
|
||||
|
||||
## Instructions
|
||||
|
||||
1. Ask the user for the table/entity name (e.g., "users", "products", "posts")
|
||||
2. Create a schema file in the appropriate location (usually `src/db/schema/` or `lib/db/schema/`)
|
||||
3. Generate the schema with:
|
||||
- Import necessary Drizzle types (pgTable, serial, text, timestamp, etc.)
|
||||
- Proper table definition with appropriate column types
|
||||
- Primary keys and indexes
|
||||
- Timestamps (createdAt, updatedAt) where appropriate
|
||||
- Foreign key relationships if needed
|
||||
- Unique constraints
|
||||
- Default values
|
||||
4. Export the table and infer TypeScript types
|
||||
5. Suggest running `drizzle-kit generate:pg` to create migrations
|
||||
|
||||
## Example Structure
|
||||
|
||||
```typescript
|
||||
import { pgTable, serial, text, timestamp, varchar } from "drizzle-orm/pg-core";
|
||||
|
||||
export const users = pgTable("users", {
|
||||
id: serial("id").primaryKey(),
|
||||
name: varchar("name", { length: 255 }).notNull(),
|
||||
email: varchar("email", { length: 255 }).notNull().unique(),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
updatedAt: timestamp("updated_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
export type User = typeof users.$inferSelect;
|
||||
export type NewUser = typeof users.$inferInsert;
|
||||
```
|
||||
|
||||
Ensure proper column types, constraints, and TypeScript type inference.
|
||||
40
commands/generate-migration.md
Normal file
40
commands/generate-migration.md
Normal file
@@ -0,0 +1,40 @@
|
||||
---
|
||||
description: Generate a Drizzle migration from schema changes
|
||||
---
|
||||
|
||||
# Generate Database Migration
|
||||
|
||||
Generate a new Drizzle migration file based on schema changes.
|
||||
|
||||
## Instructions
|
||||
|
||||
1. Check if drizzle-kit is configured (look for `drizzle.config.ts`)
|
||||
2. If not configured, offer to create the config file with proper Postgres settings
|
||||
3. Run the migration generation command:
|
||||
- `bunx drizzle-kit generate:pg` or `bun run db:generate`
|
||||
4. Review the generated migration file in the migrations directory
|
||||
5. Remind the user to:
|
||||
- Review the migration SQL before applying
|
||||
- Run `bun run db:push` or the migration apply command
|
||||
- Update the database
|
||||
|
||||
## Drizzle Config Example
|
||||
|
||||
```typescript
|
||||
import type { Config } from 'drizzle-kit'
|
||||
|
||||
export default {
|
||||
schema: './src/db/schema/*',
|
||||
out: './drizzle',
|
||||
driver: 'pg',
|
||||
dbCredentials: {
|
||||
connectionString: process.env.DATABASE_URL!,
|
||||
},
|
||||
} satisfies Config
|
||||
```
|
||||
|
||||
## Safety Checks
|
||||
|
||||
- Warn if migration includes destructive operations (DROP, ALTER with data loss)
|
||||
- Suggest backing up production databases before applying
|
||||
- Check for proper environment variable configuration
|
||||
Reference in New Issue
Block a user