agentleFS
dafthunk-comdafthunk-com/dafthunk/.cursor/rules/07-database-development.mdc

dafthunk / rules

Cursor rule133 starsChanged 9 months agoMITView on GitHub

The file

We split this 07-database-development.mdc into the rules it gives an agent

12 rules. About 0 are common, 6 are distinctive, and 6 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈6

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use Drizzle's SQLite helpers (integer(), text(), sqliteTable(), etc.).

    line 10
  2. Very specific:

    Use .references() in Drizzle to define relationships explicitly.

    line 23
  3. Distinctive:

    Instead, constrain values manually in your application or through custom Zod validation.

    line 35
  4. Very specific:

    Use camelCase in TypeScript with InferModel.

    line 39
  5. Very specific:

    Use SQL-level defaults like CURRENT_TIMESTAMP to avoid time zone mismatches.

    line 48
  6. Distinctive:

    Avoid setting defaults in TypeScript for critical database fields.

    line 49
  7. Very specific:

    Always use Drizzle's InferModel to export consistent types.

    line 52
  8. Very specific:

    Use drizzle-kit to generate and run migrations.

    line 60
  9. Distinctive:

    Keep migrations version-controlled and consistent across environments.

    line 61
  10. Distinctive:

    Don't mutate tables manually in production.

    line 62
  11. Distinctive:

    Prefer strict types to nullable columns.

    line 65
  12. Distinctive:

    Avoid relying on rapid writes followed immediately by reads (i.e., eventual read-after-write delays).

    line 70

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
07-database-development.mdc
---
description: 
globs: apps/api/src/db/**
alwaysApply: false
---
# Database Design Best Practices

## Use SQL-Compatible Types
- Cloudflare D1 uses SQLite under the hood.
- Use Drizzle's SQLite helpers (`integer()`, `text()`, `sqliteTable()`, etc.).
- Be mindful of SQLite's flexible typing and ensure type safety using Drizzle.

```ts
export const users = sqliteTable("users", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  email: text("email").notNull().unique(),
  createdAt: text("created_at").default(sql`CURRENT_TIMESTAMP`),
});
```

## Model Relationships Carefully
- D1 (SQLite) does support foreign keys, but indexing is crucial for performance.
- Use `.references()` in Drizzle to define relationships explicitly.

```ts
export const posts = sqliteTable("posts", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  authorId: integer("author_id").references(() => users.id),
  title: text("title").notNull(),
});
```

## Use Enums Where Needed (with Caution)
- SQLite does not have native enum support.
- Instead, constrain values manually in your application or through custom Zod validation.

## Use snake_case for SQL Fields
- Stick to `snake_case` in table and column names.
- Use `camelCase` in TypeScript with `InferModel`.

```ts
export const tasks = sqliteTable("tasks", {
  createdAt: text("created_at"),
});
```

## Timestamps & Defaults
- Use SQL-level defaults like `CURRENT_TIMESTAMP` to avoid time zone mismatches.
- Avoid setting defaults in TypeScript for critical database fields.

## Type Inference
- Always use Drizzle's `InferModel` to export consistent types.

```ts
export type User = InferModel<typeof users>;
export type NewUser = InferModel<typeof users, "insert">;
```

## Migrations
- Use `drizzle-kit` to generate and run migrations.
- Keep migrations version-controlled and consistent across environments.
- Don't mutate tables manually in production.

## Avoid Nullable Unless Necessary
- Prefer strict types to nullable columns.
- Design schema around clear assumptions; nulls can be a source of bugs.

## D1 Considerations
- Cloudflare D1 is a distributed SQLite; consider its eventual consistency.
- Avoid relying on rapid writes followed immediately by reads (i.e., eventual read-after-write delays).
- Be cautious with large data writes or transactions—D1 is optimized for light, edge-ready workloads.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.