Skip to content

@joakimbugge/seeder


@joakimbugge/seeder / SeederRunContext

Interface: SeederRunContext

Defined in: packages/seeder/src/seeder/context.ts:34

Context passed to a seeder's run method by runSeeders. Extends SeedContext with results — a live map of return values from seeders that have already completed.

Use this type (rather than SeedContext) when typing a seeder's run parameter, so that ctx.results is available with type safety.

Example

ts
@Seeder({ dependencies: [UserSeeder] })
class BookingSeeder implements SeederInterface {
  async run(ctx: SeederRunContext) {
    const users = ctx.results?.get(UserSeeder) // User[]
  }
}

Extends

Properties

previous?

ts
optional previous?: ReadonlyMap<Function, readonly any[]>;

Defined in: packages/seeder/src/seed/registry.ts:38

Instances created earlier in the current createMany batch, keyed by entity class. When creating instance i, previous.get(EntityClass) contains instances 0..i-1 in order.

Each entry is a snapshot taken just before the corresponding instance is created, so the array length always equals the number of completed instances of that type so far.

Child entities inherit the parent's map, so a child factory can also read parent-batch entries. The child's own type starts with an empty array when its batch begins — instances from an unrelated sibling batch of the same type are never visible.

Example

ts
// Each booking starts the day after the previous one ends.
@Seed((ctx, self: Booking) => {
  const last = (ctx.previous?.get(Booking) as Booking[] | undefined)?.at(-1)
  return last ? last.to.plus({ days: 1 }) : DateTime.now()
})
from!: DateTime

Inherited from

SeedContext.previous


relations?

ts
optional relations?: boolean;

Defined in: packages/seeder/src/seed/registry.ts:16

Set to false to skip automatic relation seeding. Scalar and embedded properties are still seeded; only relation properties decorated with a bare @Seed() are skipped.

Default

ts
true

Inherited from

SeedContext.relations


results?

ts
optional results?: SeederResultMap;

Defined in: packages/seeder/src/seeder/context.ts:54

Return values of seeders that have already completed, keyed by seeder constructor. Populated automatically by runSeeders and forwarded to every seeder's run method.

When spreading ctx into createMany or saveMany options, results is also available inside @Seed factory callbacks via the context argument.

Example

ts
@Seeder({ dependencies: [UserSeeder] })
class BookingSeeder implements SeederInterface {
  async run(ctx: SeederRunContext) {
    const users = ctx.results?.get(UserSeeder) // User[]
    return seed(Booking).createMany(10, {
      ...ctx,
      values: { user: () => faker.helpers.arrayElement(users) },
    })
  }
}

Released under the MIT License.