Skip to content

@joakimbugge/typeorm-seeder


@joakimbugge/typeorm-seeder / Seed

Function: Seed()

Call Signature

ts
function Seed(): PropertyDecorator;

Defined in: seed/decorator.ts:14

Marks a relation property for auto-seeding.

The related entity class is inferred from TypeORM metadata. One instance is created and recursively seeded (including its own @Seed properties).

Circular back-references are broken automatically: if the related class is already being seeded higher up in the same call chain, the property is left undefined. TypeORM treats undefined as "don't touch this column" rather than setting it to null.

Returns

PropertyDecorator

Call Signature

ts
function Seed(options): PropertyDecorator;

Defined in: seed/decorator.ts:26

Marks a relation property for auto-seeding with options.

Use count on one-to-many and many-to-many properties to control how many related entities are created. Ignored for one-to-one and many-to-one.

Parameters

options

SeedOptions

Returns

PropertyDecorator

Example

ts
@Seed({ count: 3 })
@OneToMany(() => Book, (b) => b.author)
books!: Book[]

Call Signature

ts
function Seed<TEntity>(factory): PropertyDecorator;

Defined in: seed/decorator.ts:43

Marks a property with a factory callback.

The factory receives the current SeedContext and can return any value, including a Promise. Use this for scalar properties or when you need full control over how a related entity is resolved.

Type Parameters

TEntity

TEntity = any

Parameters

factory

SeedFactory<unknown, TEntity>

Returns

PropertyDecorator

Examples

ts
@Seed(() => faker.internet.email())
email!: string
ts
// Look up an existing entity instead of creating a new one
@Seed(async ({ dataSource }) => dataSource.getRepository(Role).findOneByOrFail({ name: 'admin' }))
role!: Role

Call Signature

ts
function Seed<TEntity>(factory, options): PropertyDecorator;

Defined in: seed/decorator.ts:45

Marks a property with a factory callback and additional options.

Type Parameters

TEntity

TEntity = any

Parameters

factory

SeedFactory<unknown, TEntity>

options

SeedOptions

Returns

PropertyDecorator

Released under the MIT License.