database_layer_rules

A set of rules for the database code in a Python backend, covering data models, create/read/update/delete operations, transactions, and errors.

In plain words
What is it for?
It guides developers when defining models, opening database sessions, writing queries, updating audit fields, and deleting records safely.
Why use it?
It keeps database code consistent and protects shared conventions such as audit fields and soft deletion, where records are marked deleted instead of removed.

Cursor rule for Cursor

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/modelengine-group/nexent/database_layer_rules
Clone the repo
git clone --depth 1 https://github.com/ModelEngine-Group/nexent

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 973 The whole file, excluding the scripts and references it only reads on demand.
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.00000 $0.00973
Opus 5 $0.00000 $0.00487
Sonnet 5 $0.00000 $0.00195
Haiku 4.5 $0.00000 $0.00097

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

Security

Grade A, and why

database_layer_rules 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.

.cursor/rules/backend/database_layer_rules.mdc · 92 lines

How it starts

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

Database Layer Standards

Scope: all Python under backend/database/**/*.py. Concise standards for models, CRUD, transactions, and exceptions.

1) Models and audit fields

  • Inherit all models from TableBase.
  • Shared fields: create_time, update_time, created_by, updated_by, delete_flag (Y/N).
  • Never re-declare shared fields; add only table-specific columns.

2) CRUD and audit

  • Create: set created_by, updated_by, default delete_flag='N'; timestamps are server-managed.
  • Update: set updated_by; do not change create_time/created_by.
  • Delete: soft-delete only (delete_flag='Y', set updated_by). Cascade by soft-deleting children in same transaction when needed.
  • Read: exclude soft-deleted rows by default (delete_flag='N').

3) Transactions and sessions

  • Always use with get_db_session() as session:.
  • Never call commit(), rollback(), or close() in DB-layer code.
  • The context manager centrally handles commit/rollback/close.

4) Exceptions

  • Do not catch DB exceptions in backend/database/**; let them propagate.
  • Central handling occurs in get_db_session().
  • Services that must proceed non-blockingly may catch a shared type (e.g., DatabaseOperationError).

5) Exception flow (inside get_db_session)

  • On exception: rollback → re-raise → close → propagate to callers.

6) Reference patterns (Core; no explicit commit/rollback)

from sqlalchemy import insert, update, select
from database.client import get_db_session, as_dict

def create_entity(data: dict):
    with get_db_session() as session:
        return session.execute(
            insert(SomeModel).values(**data).returning(SomeModel.id)
        ).scalar_one()

def update_entity(entity_id: int, updates: dict, actor: str):
    with get_db_session() as session:
        session.execute(
            update(SomeModel)
            .where(SomeModel.id == entity_id, SomeModel.delete_flag == 'N')
            .values(**updates, updated_by=actor)
        )

def soft_delete_entity(entity_id: int, actor: str):
    with get_db_session() as session:
        session.execute(
            update(SomeModel)
            .where(SomeModel.id == entity_id, SomeModel.delete_flag == 'N')
            .values(delete_flag='Y', updated_by=actor)
        )

def read_active_entity(entity_id: int):
    with get_db_session() as session:
        record = session.scalars(
            select(SomeModel).where(
                SomeModel.id == entity_id,
                SomeModel.delete_flag == 'N',
            )
        ).first()
        return None if record is None else as_dict(record)

Read the full file on GitHub · 92 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 · 92 lines · 0 tokens per session scan A dfd062607f6b

Subscribe to this mod's changes

database_layer_rules is a cursor rule published in the GitHub repository ModelEngine-Group/nexent (5,841 stars, last pushed 3d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 973 tokens. 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.