Add database development

From brabos-ai/code-addiction · View interactive version

Add database development is an AI agent skill from the GitHub repository brabos-ai/code-addiction. Database layer: entities, repos, migrations, multi-tenancy. Stack-agnostic. Follow project conventions (wiki) for the ORM.

How to use this skill

Skill file

Database Development

Skill for implementing the database layer following universal data architecture principles.

Use for: Entities, Migrations, Repositories, Enums, Database types

Do not use for: Controllers/DTOs (backend-development), Frontend (ux-design), API contracts, query optimization tuning

Stack orientation: Project conventions: follow {{addpath:wiki/conventions.md}} → ## Stack Conventions (+ {{addpath:wiki/domains/<area>.md}}); a one-line critical rule may live in AGENTS.md. If absent, apply the agnostic defaults and follow the nearest existing code; never impose tenancy, DI registration or barrels unless the project uses them. The defaults are written once in the backend development skill (backend-development) → ## Project Conventions First. Apply these principles using the project's ORM API (row ORM / data access).


Entities

Domain objects in the project's language, in the form neighbouring entity files use (row Entity language). The block below is one stack's illustration (TypeScript), not a mandate.

export interface User {
  id: string;
  accountId: string;  // only when row `Tenancy` = yes
  email: string;
  role: UserRole;
  status: EntityStatus;
  createdAt: Date;
  updatedAt: Date;
}

Rules:

Export in the folder's barrel file only when row Barrels = yes.


Enums

export enum UserRole {
  OWNER = 'owner',
  ADMIN = 'admin',
  MEMBER = 'member',
}

Rules:


Naming Convention

Follow the project's naming. When persistence naming differs from domain naming (a common case, shown here), convert between the two with mapper functions (e.g. toEntity / toPersistence); when it does not differ, no mapper is needed.

| Layer | Casing | Example |

|---|---|---|

| Database | snake_case | user_id, created_at |

| Application | camelCase | userId, createdAt |

Defaults for column types, unless the project differs:


Migrations

Naming: follow the existing migration files. When there are none, the default is YYYYMMDDNNN_description_snake_case.[ext] — e.g. 20251221001_create_invites_table.

Principles — apply using your project's migration tool syntax:

  1. One migration per change — do not bundle unrelated schema changes
  2. Never edit an existing migration — create a new one
  3. Always implement both up and down — every migration must be reversible
  4. Descriptive names — the filename should explain the change
  5. Relationships — explicit FK constraints with intentional cascade behavior; prefer CASCADE for child records that cannot exist without parent (e.g., invites → account), RESTRICT or SET NULL when child has independent value; document WHERE and WHY cascade is used
  6. Indexes on frequently queried columns (lookup fields, composite indexes for multi-column queries; the tenant key too when row Tenancy = yes)
  7. Default values for id (UUID generation), created_at, updated_at

Standard columns (lookup):

| Column | Definition |

|---|---|

| id | UUID, primary key, auto-generated |

| created_at | timestamp, default now() |

| updated_at | timestamp, default now() |

When row Tenancy = yes, add the tenant key column (e.g. account_id: UUID, NOT NULL, FK to accounts, CASCADE delete).


Repository Pattern

Use a repository (and mapper) only when the project already uses them or persistence naming differs from domain naming (row ORM / data access). Otherwise follow the project's data-access style.

Interface (contract)

export interface IInviteRepository {
  findById(id: string): Promise<Invite | null>;
  findByAccountId(accountId: string): Promise<Invite[]>;
  create(data: Omit<Invite, 'id' | 'createdAt' | 'updatedAt'>): Promise<Invite>;
  update(id: string, data: Partial<Invite>): Promise<Invite>;
  delete(id: string): Promise<void>;
}

Implementation

The implementation uses the project's ORM. Key rules:

// Mapper: snake_case (database) → camelCase (domain)
private toEntity(row: any): Invite {
  return {
    id: row.id,
    accountId: row.account_id,
    email: row.email,
    // ...
  };
}

Rules:


Barrel Exports

Applied only when row Barrels = yes (do not create new barrels otherwise). Maintain organized exports for every new entity, enum, repository, and interface:

// One barrel per directory: entities/, enums/, repositories/, interfaces/
export * from './User';
export * from './Invite';
// ...add every new file

Multi-Tenancy

Only when row Tenancy = yes. Without it, do not add tenancy; raise it as a planning question.

EVERY query MUST filter by the recorded tenant key (e.g. account_id). No exceptions.

async findAll(): Promise<User[]> { /* WRONG — leaks data across tenants */ }
async findByAccountId(accountId: string): Promise<User[]> { /* CORRECT — scoped */ }

Rules:


Validation Checklist

Entities

Enums

Migration

Repository

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.