name: add-entity description: "Use when creating a new database table, entity, or data model. ALWAYS use for any new TypeORM EntitySchema creation."
Add Database Entity
Create entity for $ARGUMENTS.
Steps
Read the pattern: Open
packages/server/api/src/app/tables/table/table.entity.tsas reference.Create entity file at
packages/server/api/src/app/{module}/{name}.entity.ts:- Use
EntitySchema(NOT decorators) - Include
...BaseColumnSchemaPart(id, created, updated) - Use
ApIdSchemafor foreign key columns ({ ...ApIdSchema, nullable: false }) - Add
projectIdcolumn + relation to project (CASCADE delete) - Add
foreignKeyConstraintNameon all join columns - Array columns:
{ type: String, array: true, nullable: false }
- Use
Register entity: Import and add to
getEntities()array inpackages/server/api/src/app/database/database-connection.ts. This is REQUIRED — TypeORM does NOT auto-discover.Create migration:
- Read playbook
- Name:
{Timestamp}{PascalCaseDescription}(e.g.,AddMyFeature1774500000000) - Import in
packages/server/api/src/app/database/postgres-connection.ts - Add to
getMigrations()array (chronological order) - PGlite:
CREATE INDEX(notCONCURRENTLY). Settransaction = falseforCONCURRENTLY.
Create repository:
const myRepo = repoFactory(MyEntity)— call asmyRepo()ormyRepo(entityManager)for transactions.Verify:
npm run lint-dev