Skip to content

Seeders ​

This guide covers seeders:

  • Writing a seeder
  • Environment gating
  • Running seeders programmatically
  • Console commands

Seeders populate the database with default, reference, or test data. Atlas provides a seeder API — a base class you extend, a directory runner, and two console commands (make:seeder / db:seed).

Atlas has no global config registry: you always pass your own paths and connection, so nothing is discovered implicitly.

Writing a seeder ​

Extend BaseSeeder and implement run(). The database connection is injected through the constructor and exposed as both this.db and this.client (an alias), so you can build repositories inside run() without reaching for globals.

Make the body idempotent — upsert keyed on a unique column is the recommended pattern so a seeder is safe to re-run.

ts
// database/seeders/1700000000000_CountrySeeder.ts
import { BaseSeeder, BaseRepository } from '@c9up/atlas'
import { Country } from '#models/country'

export default class CountrySeeder extends BaseSeeder {
  async run() {
    const countries = new BaseRepository(Country, this.db)
    // Conflict on isoCode → update name. Safe to re-run.
    await countries.upsert(
      [
        { isoCode: 'FR', name: 'France' },
        { isoCode: 'IN', name: 'India' },
      ],
      ['isoCode'],
      ['name'],
    )
  }
}

run() may return void or a Promise — async bodies are awaited.

Every seeder file must export default a class extending BaseSeeder; the runner throws E_SEEDER_INVALID otherwise.

Seeder is a legacy alias of BaseSeeder kept for older code. New seeders should extend BaseSeeder.

Environment gating ​

Set the static environment array to restrict a seeder to specific environments. When the runner is given the current environment, it skips any seeder whose list excludes it. Leaving environment unset runs the seeder everywhere.

Names are compared through the alias table: prod is production, dev is development, test is testing. db:seed always has a current environment. Without --environment, it reads the application's environment, else REAM_ENV / NODE_ENV (production when either says so). With neither variable set, the environment is unknown, and a seeder with an environment list does not run.

ts
export default class DemoUsersSeeder extends BaseSeeder {
  static environment = ['development', 'testing']

  async run() {
    // only runs in development / testing
  }
}

Running seeders programmatically ​

runSeeders(seeders) ​

Run a pre-built list of seeder instances in order. Each run() is awaited sequentially, so ordering is deterministic and one seeder's side effects are visible to the next.

ts
import { runSeeders } from '@c9up/atlas'

await runSeeders([
  new CountrySeeder(db),
  new CurrencySeeder(db),
])

runSeederDirectory(dir, db, options?) ​

Discover the .ts / .js seeder files under dir, import them in sorted order, build an instance of each with db, and run them sequentially. Returns the list of executed seeder names (file names without extension).

ts
import { runSeederDirectory } from '@c9up/atlas'

const executed = await runSeederDirectory('database/seeders', db)
// → ['1700000000000_CountrySeeder', '1700000000001_CurrencySeeder']

Options:

  • files — run only these seeders, matched by base name (CountrySeeder) or by a full / relative file path (database/seeders/CountrySeeder.ts); the runner compares the basename without extension.
  • naturalSort — sort files numerically (2_x before 10_x) instead of the default lexicographic order.
  • environment — the current environment; skips any seeder whose static environment excludes it.

If dir does not exist the runner throws E_SEEDER_DIR_NOT_FOUND.

ts
await runSeederDirectory('database/seeders', db, {
  files: ['CountrySeeder'],
  naturalSort: true,
  environment: 'development',
})

Console commands ​

make:seeder and db:seed ship with the package — registering @c9up/atlas/commands in reamrc.commands is all an application does, and the directory comes from seeders.paths in config/database.ts. See Console commands.

The factories below are for the cases the config cannot express — a second seeder directory, a different environment default. Each takes the seedersDir (and, for db:seed, optional naturalSort / environment defaults).

ts
export interface SeederCommandOptions {
  seedersDir: string
  naturalSort?: boolean
  environment?: string
}

make:seeder ​

Scaffold a new seeder file in seedersDir. The file is prefixed with Date.now() so it keeps creation order under the runner's lexicographic sort (the same convention as migrations), and it is written with the wx flag so an existing seeder is never overwritten. The name is validated — path separators and traversal are rejected.

ts
// commands/make-seeder.ts
import { makeSeederCommand } from '@c9up/atlas'

export default makeSeederCommand({ seedersDir: 'database/seeders' })
sh
ream make:seeder CountrySeeder
# → Created database/seeders/1700000000000_CountrySeeder.ts

The scaffold extends BaseSeeder with an empty async run() and a commented upsert example to fill in.

db:seed ​

Run the seeders in seedersDir.

ts
// commands/seed.ts
import { dbSeedCommand } from '@c9up/atlas'

export default dbSeedCommand({ seedersDir: 'database/seeders' })
sh
ream db:seed
# → Seeded: 1700000000000_CountrySeeder, 1700000000001_CurrencySeeder

Flags:

  • --files=A,B — run only the named seeders (comma-separated).
  • --connection=name — run against a specific registered connection instead of the default.
  • --environment=name — override the environment used for static environment gating; falls back to the factory's environment, then the application's environment, then REAM_ENV / NODE_ENV (unknown when neither is set).
  • --compact-output — print a single summary line (Seeded N seeder(s)) instead of listing each seeder.
  • --interactive — accepted, but has no effect. The console kernel is non-interactive, so it prints a warning and runs every selected seeder without prompting.
sh
ream db:seed --files=CountrySeeder,CurrencySeeder --connection=pg

If no connection is available (or the named --connection is not registered), db:seed prints an error and exits with a non-zero code — check that AtlasProvider is registered.