secondsky/claude-skills / plugins/drizzle-orm-d1/skills/drizzle-orm-d1
drizzle-orm-d1
Verified| Type-safe ORM for Cloudflare D1 databases using Drizzle. Use when: building D1 database schemas, writing type-safe SQL queries, managing migrations with Drizzle Kit, defining table relations, implementing prepared statements, using D1 batch API, or encountering D1_ERROR, transaction errors, foreign key constraint failures, or schema inference issues.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 9.2 KB |
| references/common-errors.md | markdown | 5.3 KB |
| references/error-catalog.md | markdown | 7.9 KB |
| references/links-to-official-docs.md | markdown | 1.8 KB |
| references/migration-workflow.md | markdown | 2.7 KB |
| references/query-builder-api.md | markdown | 3.9 KB |
| references/schema-patterns.md | markdown | 3.4 KB |
| references/wrangler-setup.md | markdown | 2.7 KB |
| scripts/check-versions.shexecutable | script | 1017 B |
| templates/basic-queries.ts | script | 7.5 KB |
| templates/basic-schema.ts | script | 2.9 KB |
| templates/client.ts | script | 2.2 KB |
| templates/cloudflare-worker-integration.ts | script | 7.7 KB |
| templates/drizzle.config.ts | script | 2.2 KB |
| templates/migrations/0001_example.sql | other | 2.1 KB |
| templates/package.json | manifest | 1.1 KB |
| templates/prepared-statements.ts | script | 7.3 KB |
| templates/relations-queries.ts | script | 6.8 KB |
| templates/schema.ts | script | 6.5 KB |
| templates/transactions.ts | script | 6.8 KB |
references/query-builder-api.md
View on GitHub# Query Builder API Reference
Complete reference for Drizzle's query builder.
---
## SELECT
```typescript
// All columns
db.select().from(users)
// Specific columns
db.select({ id: users.id, name: users.name }).from(users)
// With WHERE
db.select().from(users).where(eq(users.id, 1))
// With multiple conditions
db.select().from(users).where(and(
eq(users.role, 'admin'),
gte(users.createdAt, new Date('2024-01-01'))
))
// With ORDER BY
db.select().from(users).orderBy(users.name)
db.select().from(users).orderBy(desc(users.createdAt))
// With LIMIT and OFFSET
db.select().from(users).limit(10).offset(20)
// Execution
.all() // Returns array
.get() // Returns first or undefined
```
---
## INSERT
```typescript
// Single insert
db.insert(users).values({ email: 'test@example.com', name: 'Test' })
// Multiple insert
db.insert(users).values([
{ email: 'user1@example.com', name: 'User 1' },
{ email: 'user2@example.com', name: 'User 2' },
])
// With RETURNING
db.insert(users).values({ ... }).returning()
// Execution
.run() // No return value
.returning() // Returns inserted rows
```
---
## UPDATE
```typescript
// Update with WHERE
db.update(users)
.set({ name: 'New Name', updatedAt: new Date() })
.where(eq(users.id, 1))
// With RETURNING
db.update(users)
.set({ ... })
.where(eq(users.id, 1))
.returning()
// Execution
.run() // No return value
.returning() // Returns updated rows
```
---
## DELETE
```typescript
// Delete with WHERE
db.delete(users).where(eq(users.id, 1))
// With RETURNING
db.delete(users).where(eq(users.id, 1)).returning()
// Execution
.run() // No return value
.returning() // Returns deleted rows
```
---
## Operators
### Comparison
- `eq(column, value)` - Equal
- `ne(column, value)` - Not equal
- `gt(column, value)` - Greater than
- `gte(column, value)` - Greater than or equal
- `lt(column, value)` - Less than
- `lte(column, value)` - Less than or equal
### Logical
- `and(...conditions)` - AND
- `or(...conditions)` - OR
- `not(condition)` - NOT
### Pattern Matching
- `like(column, pattern)` - LIKE
- `notLike(column, pattern)` - NOT LIKE
### NULL
- `isNull(column)` - IS NULL
- `isNotNull(column)` - IS NOT NULL
### Arrays
- `inArray(column, values)` - IN
- `notInArray(column, values)` - NOT IN
### Between
- `between(column, min, max)` - BETWEEN
- `notBetween(column, min, max)` - NOT BETWEEN
---
## JOINs
```typescript
// LEFT JOIN
db.select()
.from(users)
.leftJoin(posts, eq(posts.authorId, users.id))
// INNER JOIN
db.select()
.from(users)
.innerJoin(posts, eq(posts.authorId, users.id))
// Multiple joins
db.select()
.from(comments)
.innerJoin(posts, eq(comments.postId, posts.id))
.innerJoin(users, eq(comments.authorId, users.id))
```
---
## Aggregations
```typescript
import { sql } from 'drizzle-orm';
// COUNT
db.select({ count: sql<number>`count(*)` }).from(users)
// SUM
db.select({ total: sql<number>`sum(${posts.views})` }).from(posts)
// AVG
db.select({ avg: sql<number>`avg(${posts.views})` }).from(posts)
// GROUP BY
db.select({
role: users.role,
count: sql<number>`count(*)`
}).from(users).groupBy(users.role)
```
---
## Relational Queries
```typescript
// Must pass schema to drizzle()
const db = drizzle(env.DB, { schema });
// Find many
db.query.users.findMany()
// Find first
db.query.users.findFirst({
where: eq(users.id, 1)
})
// With relations
db.query.users.findFirst({
with: {
posts: true
}
})
// Nested relations
db.query.users.findFirst({
with: {
posts: {
with: {
comments: true
}
}
}
})
```
---
## Batch Operations
```typescript
db.batch([
db.insert(users).values({ ... }),
db.update(posts).set({ ... }).where(eq(posts.id, 1)),
db.delete(comments).where(eq(comments.id, 1)),
])
```
---
## Prepared Statements
```typescript
const getUserById = db
.select()
.from(users)
.where(eq(users.id, sql.placeholder('id')))
.prepare();
// Execute
const user = await getUserById.get({ id: 1 });
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| Agent Cost Reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |
| Agent Cost Reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |