Skip to content

CLI Commands

fastify-admin ships with a CLI available as npx fastify-admin <command> once the package is installed.

The CLI automatically loads .env from your current working directory, so no extra setup is needed.


Project Setup

init

Terminal window
npx fastify-admin init

Scaffolds starter files in your project:

  • .env — database and auth config template
  • mikro-orm.config.ts — ORM config importing the built-in entities
  • src/entities/post.entity.ts — example entity
  • src/views/post.view.ts — example EntityView
  • src/index.ts — example Fastify server entry

Safe to run multiple times — existing files are never overwritten.

generate:resource

Terminal window
npx fastify-admin generate:resource Product

Scaffolds a new entity + EntityView pair:

  • src/entities/product.entity.ts — MikroORM entity with id, name, createdAt
  • src/views/product.view.tsProductView extending EntityView with sensible defaults

After generating, register the view in your server:

import { ProductView } from './views/product.view.js'
await app.register(fastifyAdmin, {
orm,
views: {
product: new ProductView(),
},
})

User Management

create-admin

Terminal window
npx fastify-admin create-admin

Creates a new user and assigns them the Admin role. Prompts interactively for email, username, full name, and password.

reset-password

Terminal window
npx fastify-admin reset-password

Resets the password for an existing user. Prompts for the user’s email and new password (with confirmation).


Migrations

migrate:up

Terminal window
npx fastify-admin migrate:up

Applies all pending migrations. Run this after installing the package to set up the admin tables.

migrate:down

Terminal window
npx fastify-admin migrate:down

Rolls back the last applied migration.

migrate:create

Terminal window
npx fastify-admin migrate:create [name]

Creates a new migration file based on current entity changes.

schema:update

Terminal window
npx fastify-admin schema:update

Syncs the database schema directly without creating a migration. Development only — use migrate:up in production.


Environment Variables

The CLI reads database config from env vars (or .env in your project root):

VariableDefaultDescription
DATABASE_URLFull connection URL (overrides all below)
DB_NAMEfastifyadminDatabase name
DB_HOSTlocalhostDatabase host
DB_PORT5432Database port
DB_USERpostgresDatabase user
DB_PASSWORDpasswordDatabase password

Monorepo / Contributing

If you’re working in the fastify-admin monorepo itself, use the pnpm scripts instead:

Terminal window
pnpm create-admin
pnpm reset-password
pnpm migration:create
pnpm migration:up
pnpm migration:down

See Contributing for full monorepo setup.