Skip to content

@joakimbugge/typeorm-seeder


@joakimbugge/typeorm-seeder / Seed

Variable: Seed ​

ts
const Seed: {
  (): PropertyDecorator;
  (options): PropertyDecorator;
<TEntity>  (factory): PropertyDecorator;
<TEntity>  (factory, options): PropertyDecorator;
};

Defined in: packages/typeorm-seeder/src/seed/decorator.ts:28

TypeORM-flavoured @Seed decorator. Identical API to the base @Seed but factory callbacks receive a SeedContext that includes dataSource.

Re-exported as a type-cast of the base implementation — zero runtime overhead.

Call Signature ​

ts
(): PropertyDecorator;

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
(options): PropertyDecorator;

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
<TEntity>(factory): PropertyDecorator;

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 extends object = object

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
<TEntity>(factory, options): PropertyDecorator;

Marks a property with a factory callback and additional options.

Type Parameters ​

TEntity ​

TEntity extends object = object

Parameters ​

factory ​

SeedFactory<unknown, TEntity>

options ​

SeedOptions

Returns ​

PropertyDecorator

Released under the MIT License.