Database & Migrations
Overview
The project uses PostgreSQL as the database and MikroORM as the ORM (Object-Relational Mapper). An ORM lets you work with database records as TypeScript classes instead of writing raw SQL.
Running the Database
During development, PostgreSQL runs in Docker:
docker compose up postgres -dThe database is accessible at localhost:5432 with these defaults:
- Database:
fastifyadmin - Username:
postgres - Password:
password
Migrations
A migration is a file that describes a change to the database schema. For example, “add a published column to the post table”.
Migrations are stored in fastify-admin/src/migrations/.
Automatic migrations on startup
The dev server automatically runs any pending migrations when it starts. You don’t need to run them manually during development.
Creating a migration
After you change or add an entity, create a migration:
pnpm migration:createMikroORM compares your entity definitions to the current database state and generates the SQL automatically. Review the generated file to make sure it looks right.
Running migrations manually
pnpm migration:up # apply all pending migrationspnpm migration:down # undo the last migrationDatabase Configuration
Connection settings are in fastify-admin/src/mikro-orm.config.ts and read from environment variables:
DB_HOST=localhostDB_PORT=5432DB_NAME=fastifyadminDB_USER=postgresDB_PASSWORD=passwordResetting the Database
To completely wipe the database and start over:
docker compose down -v # -v removes the data volumedocker compose up postgres -dThe next time you start the server it will re-run all migrations and re-seed the roles/permissions.
MikroORM CLI
You can use the MikroORM CLI directly from the fastify-admin directory:
cd fastify-adminnpx mikro-orm migration:create # create migrationnpx mikro-orm migration:up # run migrationsnpx mikro-orm migration:down # rollbacknpx mikro-orm debug # check connection and entity discoveryOr use the root-level shortcuts:
pnpm migration:createpnpm migration:uppnpm migration:down