cursor-handbook / database
Cursor rule31 starsChanged 3 months agoMITView on GitHub
Database query patterns, optimization, and safety rules. Apply when writing or editing queries.
Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.
Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository30 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .cursor/rules/architecture/core-principles.mdcCursor rule
- .cursor/rules/architecture/dependency-management.mdcCursor rule
- .cursor/rules/architecture/token-efficiency.mdcCursor rule
- .cursor/rules/backend/api-design.mdcCursor rule
- .cursor/rules/backend/code-organization.mdcCursor rule
- .cursor/rules/backend/error-handling.mdcCursor rule
- .cursor/rules/backend/handler-patterns.mdcCursor rule
- .cursor/rules/backend/nodejs.mdcCursor rule
- .cursor/rules/backend/typescript.mdcCursor rule
- .cursor/rules/cloud/containerization.mdcCursor rule
- .cursor/rules/cloud/infrastructure.mdcCursor rule
- .cursor/rules/cloud/serverless.mdcCursor rule
- .cursor/rules/database/migration-rules.mdcCursor rule
- .cursor/rules/database/schema-design.mdcCursor rule
- .cursor/rules/devops/ci-cd.mdcCursor rule
- .cursor/rules/devops/documentation-standards.mdcCursor rule
- .cursor/rules/devops/monitoring.mdcCursor rule
- .cursor/rules/frontend/accessibility.mdcCursor rule
- .cursor/rules/frontend/component-patterns.mdcCursor rule
- .cursor/rules/frontend/performance.mdcCursor rule
- .cursor/rules/frontend/state-management.mdcCursor rule
- .cursor/rules/main-rules.mdcCursor rule
- .cursor/rules/security/guardrails.mdcCursor rule
- .cursor/rules/security/secrets-rules.mdcCursor rule
- .cursor/rules/security/security-compliance.mdcCursor rule
- .cursor/rules/testing/integration-testing.mdcCursor rule
- .cursor/rules/testing/mock-patterns.mdcCursor rule
- .cursor/rules/testing/testing-standards.mdcCursor rule
The file
Show its rules and text
We split this query-patterns.mdc into the rules it gives an agent
21 rules. About 0 are common, 12 are distinctive, and 9 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈12
Distinctive · estimate
This author's own choices. Worth a look.
9
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 12
NEVER: use DELETE FROM statements
- Very specific: line 13
ALWAYS: use soft delete: UPDATE table SET {{CONFIG.database.softDeleteField}} = false WHERE id = $1
- Very specific: line 14
All queries MUST filter by {{CONFIG.database.softDeleteField}} = true unless explicitly recovering deleted records
- Distinctive: line 27
Every table MUST include:
- Distinctive: line 40
Use composite indexes for multi-column queries
- Distinctive: line 41
Consider partial indexes for filtered queries
- Very specific: line 46
Use LIMIT + OFFSET only for small datasets
- Very specific: line 47
Always include ORDER BY with pagination
- Distinctive: line 66
Use JOINs or batch queries instead of loops
- Distinctive: line 67
Use DataLoader pattern for GraphQL resolvers
- Distinctive: line 72
Every schema change MUST be a migration
- Distinctive: line 74
Test migrations against a copy of production data
- Distinctive: line 75
Never modify existing migrations — create new ones
- Very specific: line 79
Don't use SELECT * in production — list columns explicitly
- Distinctive: line 80
Don't use OFFSET for large tables — use cursor-based pagination
- Distinctive: line 81
Don't run queries in loops — batch or JOIN
- Very specific: line 82
Don't skip {{CONFIG.database.softDeleteField}} filter — all reads must filter
- Distinctive: line 83
Don't use raw strings in queries — always parameterize
- Distinctive: line 95
NULL in unique index: Use partial index or COALESCE for "one active per user"
- Very specific: line 97
Timezone in timestamps: Always use TIMESTAMP WITH TIME ZONE
- Very specific: line 100
Use @create-migration skill for new migrations
≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
---
description: "Database query patterns, optimization, and safety rules. Apply when writing or editing queries."
alwaysApply: false
globs: "**/*.sql,**/*.model.ts,**/*.repository.ts,**/migrations/**/*"
---
# Database Query Patterns
## Absolute Rules
### 1. Soft Delete Only
- **NEVER** use `DELETE FROM` statements
- **ALWAYS** use soft delete: `UPDATE table SET {{CONFIG.database.softDeleteField}} = false WHERE id = $1`
- All queries MUST filter by `{{CONFIG.database.softDeleteField}} = true` unless explicitly recovering deleted records
- Column naming convention: `{{CONFIG.database.naming}}`
### 2. Parameterized Queries Only
```sql
-- CORRECT: Parameterized
SELECT * FROM orders WHERE id = $1 AND {{CONFIG.database.softDeleteField}} = true;
-- WRONG: String concatenation (SQL injection risk)
SELECT * FROM orders WHERE id = '${orderId}';
```
### 3. Required Columns
Every table MUST include:
| Column | Type | Description |
|--------|------|-------------|
| `id` | UUID / BIGINT | Primary key |
| `{{CONFIG.database.timestampFields.created}}` | TIMESTAMP | Record creation time |
| `{{CONFIG.database.timestampFields.updated}}` | TIMESTAMP | Last update time |
| `{{CONFIG.database.softDeleteField}}` | BOOLEAN | Soft delete flag (default: true) |
## Query Optimization
### Indexing Strategy
- Index all foreign key columns
- Index columns used in WHERE clauses frequently
- Use composite indexes for multi-column queries
- Consider partial indexes for filtered queries
- Monitor slow query logs and add indexes accordingly
### Pagination
- **Prefer cursor-based pagination** for large datasets
- Use `LIMIT` + `OFFSET` only for small datasets
- Always include `ORDER BY` with pagination
- Return `hasNextPage` and `cursor` in pagination metadata
```sql
-- Cursor-based pagination (preferred)
SELECT * FROM orders
WHERE {{CONFIG.database.softDeleteField}} = true
AND {{CONFIG.database.timestampFields.created}} < $1
ORDER BY {{CONFIG.database.timestampFields.created}} DESC
LIMIT 20;
-- Offset pagination (simple cases only)
SELECT * FROM orders
WHERE {{CONFIG.database.softDeleteField}} = true
ORDER BY {{CONFIG.database.timestampFields.created}} DESC
LIMIT $1 OFFSET $2;
```
### N+1 Prevention
- Use JOINs or batch queries instead of loops
- Use DataLoader pattern for GraphQL resolvers
- Preload associations when you know they'll be needed
- Monitor query counts per request in development
## Migration Rules
- Every schema change MUST be a migration
- Migrations MUST be reversible (up and down)
- Test migrations against a copy of production data
- Never modify existing migrations — create new ones
- Name format: `YYYYMMDDHHMMSS_descriptive_name`
## Anti-patterns
- Don't use `SELECT *` in production — list columns explicitly
- Don't use OFFSET for large tables — use cursor-based pagination
- Don't run queries in loops — batch or JOIN
- Don't skip `{{CONFIG.database.softDeleteField}}` filter — all reads must filter
- Don't use raw strings in queries — always parameterize
## Examples
```sql
-- BAD: String concatenation
WHERE id = '${id}'
-- GOOD: Parameterized
WHERE id = $1 AND active_indicator = true
```
## Edge Cases
- **NULL in unique index**: Use partial index or COALESCE for "one active per user"
- **Large IN clauses**: Batch into chunks of 100–500; or use temp table
- **Timezone in timestamps**: Always use `TIMESTAMP WITH TIME ZONE`
## Related
- Use `@create-migration` skill for new migrations
- See `@schema-design` for table structure
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

