| name | database-skill |
| description | Implement database-related changes in this repository, including schema changes via entities, repository/query patterns, transactions, and data migrations. Use when working on migrations, DB schema updates, or app logic that touches the database. Use when this capability is needed. |
| metadata | {"author":"6529-collections"} |
Database Workflow
Apply this skill any time work involves database schema, queries, repositories, or migrations.
Environment Assumptions
- Assume MySQL 8+ in all target environments.
- Assume Aurora MySQL in staging/production.
- Assume Docker MySQL locally.
Schema Change Rules
- Implement schema changes through TypeORM entities in
src/entities (sometimes documented as src/entitites).
- Keep entity file naming pattern:
- File name prefixed with
I, example: IMyThing.ts
- Class name without
I and suffixed with Entity, example: MyThingEntity
- Treat edits to existing entities as high risk and check for possible data loss before changing types or columns.
- Never use foreign keys.
- Prefer UUID primary keys unless sequential IDs are strictly required.
- Prefer
bigint Unix epoch milliseconds for time fields over SQL datetime/date.
- For every new
@Entity(TABLE_NAME), add/use a table constant in src/constants/db-tables.ts (example: MY_THINGS_TABLE).
- Keep entity class/file names singular; keep table names plural (example table:
my_things).
Query and Repository Rules
- Isolate DB access inside repository-style classes (usually
*Repository, sometimes *Db).
- Use caller-level transactions only when work must span multiple repositories:
await sqlExecutor.executeNativeQueriesInTransaction(async (connection) => {
});
- Make
ctx: RequestContext the last argument of repository functions.
- Time every repository function with this pattern:
try {
ctx.timer?.start(`${this.constructor.name}->nameOfTheCalledRepositoryFunction`);
} finally {
ctx.timer?.stop(`${this.constructor.name}->nameOfTheCalledRepositoryFunction`);
}
- Use
ctx.connection when present, so operations participate in caller-provided transactions.
- Never use generated
Api* classes in repositories.
- Allow callers (services/routes/etc.) to use entity classes and repository-defined types.
- Use constants from
src/constants/db-tables.ts instead of hardcoded table names whenever possible.
- Prefer typed queries via
execute<T>() and oneOrNull<T>().
Data Migration Rules
- Use db-migrate only for data migration.
- Never use db-migrate for schema changes.
- Create migrations with:
npm run migrate:new migration-name
- Edit files created under
migrations/.
- Delete the
down migration path; do not implement revert logic.
Practical Checklist
Converted and distributed by TomeVault — claim your Tome and manage your conversions.