Skip to main content
Seeding fills your database with initial data — dev fixtures, test scenarios, or production defaults like roles and categories. Vertz recommends using the entity API (db.create, db.createMany) for seeding, not raw SQL.

Why use the entity API

Raw SQL bypasses validation, type checking, and default generation. The entity API gives you:
  • Type safety — seed data is checked against your schema at compile time
  • Default handling — auto-generated IDs, timestamps, and computed defaults work automatically
  • Consistency — the same API you use in your application code

When to seed

Seeding runs after migrations. In development, call your seed function after autoMigrate() completes or after vertz db migrate:

Conditional seeding

Avoid inserting duplicates by checking if data already exists:
For idempotent seeding, upsert inserts or updates in one call:

Seed file structure

Keep seed logic in a dedicated file. A common pattern:
A typical seed.ts:

Handling relations

Insert records in dependency order — parents before children. Foreign key constraints require the referenced record to exist:
For bulk seeding with relations, extract IDs from parent inserts:

Dev seed vs test seed vs production fixtures

Different environments need different seed strategies.

Development seed

Generates realistic data for local development. Run on server startup with a guard:
Dev seeds can be generous — many records, varied states, edge cases. This helps you test UI pagination, empty states, and error scenarios.

Test seed

Creates minimal, predictable data for a specific test. Define seed helpers alongside your tests:
Use unique values (timestamps, counters) in test seeds to avoid collisions when tests run in parallel.

Production fixtures

Production seeding is for data your application requires to function — roles, categories, permission sets, default settings. Keep it minimal and idempotent:
Never seed user accounts, API keys, or sensitive data in production. Use environment variables and admin tooling for those.

Tips

  • Check results — entity API operations return Result<T, Error>. Always check .ok before using .data, especially when later inserts depend on parent IDs.
  • Use createMany for bulk data — it’s a single query, much faster than looping over create.
  • Use createManyAndReturn when you need IDs — returns the created records so you can reference them in child inserts.
  • Keep seeds fast — if your dev seed takes more than a few seconds, you have too much data.
  • Version control your seeds — seed files are code. They should be reviewed, typed, and committed.