Skip to main content

Migrations & Seed Data

Once your models are defined, you need to create the corresponding database tables and optionally populate them with initial data.

Create migrations

Migrations allow you to evolve your database schema over time. You can generate migration files using the ormed CLI.

dart run ormed_cli:ormed make --name create_genres --create --table genres
dart run ormed_cli:ormed make --name create_movies --create --table movies

These commands generate Dart files in lib/src/database/migrations/. You can then define your columns using a fluent schema builder.

    schema.create('genres', (table) {
table.id();
table.string('name');
table.text('description').nullable();
table.timestamps();
});

Seed with DTOs

Seeding is the process of populating your database with initial data, which is useful for development and testing. In Ormed, we use the generated DTOs to ensure our seed data matches our model definitions.

Individual Seeders

We create separate seeders for each model to keep things organized.

    final repo = connection.context.repository<Genre>();
await repo.insertMany([
GenreInsertDto(
name: 'Drama',
description: 'Character-driven storytelling with emotional stakes.',
),
GenreInsertDto(
name: 'Science Fiction',
description: 'Speculative worlds, future tech, and big ideas.',
),
GenreInsertDto(
name: 'Mystery',
description: 'Twists, clues, and investigative tension.',
),
]);

The Root Seeder

The DatabaseSeeder is the entry point that coordinates all other seeders.

    await call([GenreSeeder.new, MovieSeeder.new]);

Run migrations + seeds

Finally, apply the migrations to create the tables and run the seeders to populate them.

# Apply all pending migrations
dart run ormed_cli:ormed migrate

# Run the database seeders
dart run ormed_cli:ormed seed