Seeders
Seeders populate your database with initial or test data. The @stratal/seeders package provides a Seeder base class and a CLI for running seeders against your Cloudflare Workers bindings.
Installation
Section titled “Installation”yarn add -D @stratal/seedersCreating a seeder
Section titled “Creating a seeder”Extend the Seeder base class and implement the run() method:
import { Seeder } from '@stratal/seeders'import { inject, DI_TOKENS } from 'stratal/di'import type { DatabaseService } from '@stratal/framework/database'import { UserFactory } from './user.factory'
export class UserSeeder extends Seeder { constructor( @inject(DI_TOKENS.Database) private readonly db: DatabaseService, ) { super() }
async run(): Promise<void> { await new UserFactory().count(10).createMany(this.db) }}Register the seeder in a module’s providers array:
@Module({ providers: [UserSeeder],})export class UsersModule {}Naming convention
Section titled “Naming convention”Seeder names are derived from the class name by removing the Seeder suffix and converting to kebab-case:
| Class name | CLI name |
|---|---|
UserSeeder | user |
RolePermissionsSeeder | role-permissions |
InitialDataSeeder | initial-data |
CLI commands
Section titled “CLI commands”The stratal-seed CLI discovers seeders from your module tree and runs them.
Run a specific seeder
Section titled “Run a specific seeder”npx stratal-seed run userRun all seeders
Section titled “Run all seeders”npx stratal-seed run --allList available seeders
Section titled “List available seeders”npx stratal-seed listDry run
Section titled “Dry run”Preview which seeders would run without executing them:
npx stratal-seed run --all --dry-runHow it works
Section titled “How it works”The CLI uses Wrangler’s getPlatformProxy() to obtain your Cloudflare bindings (D1, KV, R2, etc.) locally. It then bootstraps your module tree, discovers all classes that extend Seeder, and executes their run() methods inside a request scope.
Auto-discovery
Section titled “Auto-discovery”Seeders are automatically discovered from the module tree. Any class that extends Seeder and is listed in a module’s providers array will be found by the CLI. Only bare class providers are scanned — value, factory, and existing providers are skipped.
Using with factories
Section titled “Using with factories”Seeders pair naturally with factories for generating realistic data:
import { Seeder } from '@stratal/seeders'import { inject, DI_TOKENS } from 'stratal/di'import type { DatabaseService } from '@stratal/framework/database'import { UserFactory } from './user.factory'import { PostFactory } from './post.factory'
export class DemoDataSeeder extends Seeder { constructor( @inject(DI_TOKENS.Database) private readonly db: DatabaseService, ) { super() }
async run(): Promise<void> { const users = await new UserFactory().count(5).createManyAndReturn(this.db)
for (const user of users) { await new PostFactory() .state(attrs => ({ ...attrs, authorId: user.id })) .count(3) .createMany(this.db) } }}Next steps
Section titled “Next steps”- Factories for generating test data with Faker.js.
- Database for configuring the database connection.
- Testing Overview for setting up your test environment.