yarn db:generate
yarn db:generate executes saasframe db generate, iterating through every enabled module and creating MikroORM migrations when entity metadata changes.
Usage
# Using yarn script
yarn db:generate
# Using CLI directly
yarn saasframe db generate
The command requires a valid DATABASE_URL pointing to the Postgres database used for diffing.
What Happens
- Loads enabled modules via the CLI resolver (
packages/cli/src/lib/resolver.ts). - For each module (in alphabetical order), resolves entity definitions (
data/entities.tsorschema.ts) and spins up a temporary MikroORM instance. - Writes migrations into
packages/<module>/src/modules/<module>/migrations. App overrides fall back tosrc/modules/<module>/migrations. - Renames generated files and migration classes to include the module suffix (e.g.
_auth) to keep names unique across modules. - Updates the module snapshot file (
.snapshot-<project>.json) used by MikroORM as the diff baseline for the next generation run. - Prints a summary such as
• auth: generated Migration20240214_author• directory: no changes.
Typical Workflow
- Run
yarn db:generateafter modifying entity definitions. - Inspect the generated TypeScript migration and adjust it if necessary.
- Keep the generated snapshot JSON files committed. They are migration metadata, not runtime artifacts, and avoid database-name-specific snapshot churn between environments.
- Commit the migration alongside your entity changes.
- Apply migrations with
yarn db:migrateoryarn saasframe db migrate.
Troubleshooting
- Missing
DATABASE_URL— set it in your shell or.envbefore running the command. - No changes detected — ensure you saved the entity file and that MikroORM metadata can resolve it from the module registry.
- Permission errors writing migrations — confirm the module directory is writable (especially when working inside app overlays or read-only package installs).