data-access

A set of rules for organizing database reads and changes into query and mutation modules. These modules act as the single source of truth for database operations.

In plain words
What is it for?
Use it to structure lib/data/queries/ for reads and lib/data/mutations/ for changes, with files and functions grouped by entity.
Why use it?
It prevents database logic from being scattered across the application and encourages consistent results and error handling.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/squirrelogic/cursor-rules/data-access
Clone the repo
git clone --depth 1 https://github.com/squirrelogic/cursor-rules
Per session 1,790 This file is loaded in full into every session.
When invoked 1,790 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.01790 $0.01790
Opus 5 $0.00895 $0.00895
Sonnet 5 $0.00358 $0.00358
Haiku 4.5 $0.00179 $0.00179

Measured yesterday against content hash 158100544465, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

data-access scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

nextjs/data-access.mdc · 257 lines

How it starts

The opening of the file, as written. The whole thing — 257 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Data Access Layer Structure

Overview

Best practices for implementing data access through query and mutation modules. These modules serve as the single source of truth for database operations and should be used by server actions.

  • File Organization

    lib/data/
    ├── mutations/
    │   ├── organizations.ts
    │   ├── users.ts
    │   └── comments.ts
    └── queries/
        ├── organizations.ts
        ├── users.ts
        └── comments.ts
    
  • Query Module Structure

    • Place all read operations in queries/ directory
    • Name files after the primary entity they query
    • Return consistent data structures
    // ✅ DO: Consistent error handling and return types
    import { organizations, organizationMembers } from '@/db/schema/organizations'
    import { eq, or } from 'drizzle-orm'
    import { createDrizzleSupabaseClient } from '@/db'
    
    export async function getUserOrganization(userId: string) {
      try {
        const db = await createDrizzleSupabaseClient()
        const orgs = await db.rls((tx) =>
          tx
            .select()
            .from(organizations)
            .innerJoin(organizationMembers, eq(organizations.id, organizationMembers.organization_id))
            .where(or(eq(organizations.owner_id, userId), eq(organizationMembers.user_id, userId)))
            .limit(1),
        )
        return { data: orgs[0] || null, error: null }
      } catch (error) {
        return {
          data: null,
          error: error instanceof Error ? error : new Error('Failed to fetch organization')
        }
      }
    }
    
    // ❌ DON'T: Inconsistent error handling or return types
    export async function getUserOrganization(userId: string) {
      const db = await createDrizzleSupabaseClient()
      const orgs = await db.rls((tx) =>
        tx.select().from(organizations).where(eq(organizations.owner_id, userId))
      )
      return orgs[0] // ❌ No error handling, inconsistent return type
    }
    
  • Mutation Module Structure

    • Place all write operations in mutations/ directory
    • Name files after the primary entity they modify
    • Handle transactions and related updates
    // ✅ DO: Handle transactions and related updates
    import { organizations, organizationMembers } from '@/db/schema/organizations'
    import { createDrizzleSupabaseClient } from '@/db'
    import { eq } from 'drizzle-orm'
    
    export async function createOrganization(name: string, logoUrl: string | null, ownerId: string) {
      try {
        const db = await createDrizzleSupabaseClient()
        const org = await db.rls((tx) =>
          tx
            .insert(organizations)
            .values({
              name,
              logo_url: logoUrl,
              owner_id: ownerId,
            })
            .returning(),
        )
    
        // Related update in the same transaction
        await db.rls((tx) =>
          tx.insert(organizationMembers).values({
            organization_id: org[0].id,
            user_id: ownerId,
            role: 'owner',
          }),
        )
    
        return { data: org[0], error: null }
      } catch (error) {
        return {
          data: null,
          error: error instanceof Error ? error : new Error('Failed to create organization')
        }
      }
    }
    

Read the full file on GitHub · 257 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 257 lines · 1,790 tokens per session scan A 158100544465

Subscribe to this mod's changes

data-access is a cursor rule published in the GitHub repository squirrelogic/cursor-rules (20 stars, last pushed 1y ago), licensed MIT. It adds 1,790 tokens to every session, about $0.0089 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.